InDesign scripting : lesson 16

this is the fourth of a series of lessons about how to script the contents of documents. so far we’ve looked at how to create and do things with page items. now let’s have a closer look at colours.

if you want to follow along — open a new document. then run this script from script editor (found in applications > applescript) — as always, update the script to your version of InDesign :

tell application "Adobe InDesign CS4"
  activate
  tell active document
    make rectangle with properties {fill color:"Black", geometric bounds:{5, 5, 10, 10}}
    make rectangle with properties {fill color:swatch 2, geometric bounds:{10, 10, 15, 15}}
    make rectangle with properties {fill color:"C=50 M=100 Y=0 K=10", geometric bounds:{15, 15, 20, 20}}
  end tell
end tell

now, it’s more than likely that the script will fail on that third rectangle. and this is the frustrating thing about colours — you can’t use a colour unless it already exists as a swatch. but more about that in a moment.

the script above shows two ways to reference a colour : by name or by index (ie. position in the swatches panel). note that if you had a swatch called ‘swatch 2’ you’d reference that with double quotes (… fill color:”swatch 2″ …) but you’d NEVER name a swatch that would you!? the other thing to note is that when the rectangle is first drawn it will have whatever fill and stroke attributes are set as defaults — exactly the same as if it were drawn using the rectangle tool (not to be confused with the rectangle frame tool).

ok. to get around the missing colour issue you have to create a new swatch. notice that you don’t actually make a new swatch, you make a new colour — which just happens to appear in your swatches panel (confusing? you bet) :

tell application "Adobe InDesign CS4"
  activate
  tell active document
    make color with properties {model:process, space:CMYK, color value:{50, 100, 0, 10}, name:"C=50 M=100 Y=0 K=10"}
    make color with properties {model:process, space:RGB, color value:{250, 255, 0}, name:"R=150 G=255 B=0"}
    make rectangle with properties {fill color:"Black", geometric bounds:{5, 5, 10, 10}}
    make rectangle with properties {fill color:swatch 2, geometric bounds:{10, 10, 15, 15}}
    make rectangle with properties {fill color:"C=50 M=100 Y=0 K=10", geometric bounds:{15, 15, 20, 20}}
    make rectangle with properties {fill color:"R=250 G=255 B=0", geometric bounds:{20, 20, 25, 25}}
  end tell
end tell

note : you should always specify the name — you can leave this out but you may get swatches with incorrect names (this happens if the “Name with Color Value” check box was not selected the previous time a swatch was created). the naming convention used here is a good one to follow in InDesign generally (not only when scripting) it saves confusion with duplicate or near-duplicate swatches. calling a swatch “purplish” is just going to lead to tears somewhere down the line.

now, if you try to run that script again on the same file it will fail because you cannot make a colour if it already exists — that is, you cannot have two colours with the same name. this next snippet will work because each swatch has a different name :

tell application "Adobe InDesign CS4"
  activate
  tell active document
    make color with properties {model:process, space:CMYK, color value:{50, 100, 0, 10}, name:"Blue"}
    make color with properties {model:process, space:CMYK, color value:{50, 100, 0, 10}, name:"Another Blue"}
    make color with properties {model:process, space:CMYK, color value:{50, 100, 0, 10}, name:"Yet Another Blue"}
  end tell
end tell

but that would be just silly. however, it does highlight why you should get into good swatch naming habits.

there are a few ways to get around the problem of the previous script failing — here’s a simple one :

tell application "Adobe InDesign CS4"
  activate
  tell active document
    try
      make color with properties {model:process, space:CMYK, color value:{50, 100, 0, 10}, name:"C=50 M=100 Y=0 K=10"}
    end try
    try
      make color with properties {model:process, space:RGB, color value:{250, 255, 0}, name:"R=150 G=255 B=0"}
    end try
    make rectangle with properties {fill color:"Black", geometric bounds:{5, 5, 10, 10}}
    make rectangle with properties {fill color:swatch 2, geometric bounds:{10, 10, 15, 15}}
    make rectangle with properties {fill color:"C=50 M=100 Y=0 K=10", geometric bounds:{15, 15, 20, 20}}
    make rectangle with properties {fill color:"R=250 G=255 B=0", geometric bounds:{20, 20, 25, 25}}
  end tell
end tell

try blocks are great. if the script CAN do the task it does — if it can’t, it just moves on to the next command.

well, that’s a quick and dirty introduction to colours. what’s up next? who knows. til then — keep grunting.

macgrunt icon

Advertisements