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

8 thoughts on “InDesign scripting : lesson 16

  1. How could i change all spot names in my swatch to the CMYK name. I have to currently go in a check the box (name with Colour Value) for each colour. Any help would be appreciated.

    • G’day Nilesh

      Here’s a solution based on one from Shane Stanley I found at macscripter.net

      tell application id “com.adobe.InDesign”
      set mgDefaults to {“None”, “Registration”, “Paper”, “Black”}
      tell active document
      set mgSwatches to every swatch
      repeat with mgSwatch in mgSwatches
      if name of mgSwatch is not in mgDefaults and space of mgSwatch is CMYK then
      set mgValues to color value of mgSwatch
      set mgC to “C=” & ((item 1 of mgValues) as integer)
      set mgM to ” M=” & ((item 2 of mgValues) as integer)
      set mgY to ” Y=” & ((item 3 of mgValues) as integer)
      set mgK to ” K=” & ((item 4 of mgValues) as integer)
      set mgName to (mgC & mgM & mgY & mgK) as string
      set properties of mgSwatch to {space:CMYK, model:process, name:mgName}
      end if
      end repeat
      end tell
      end tell

      m.

      • Hi, thank you so much for your time on this. Im creating this through indeign extend script toolkit cc. tell application is causing an error?

      • Hi Nilesh
        ExtendScript Toolkit is for Javascript.
        All the scripts on my site are Applescript.
        You need to open the script in Applescript Editor (or just Script Editor on some OSX)
        The editor will most likely be found in Applications > Utilities.
        Good luck
        m.

  2. Hi, is there a way to set geometric bounds somehow like (x1=page width/2, y1=page height/2, x2=x1+1px, y2=y1+1px)? I mean, is possible to place a rectangle in the centre of the page, whatever its size is?
    Thanks a lot.

    • G’day alescotto

      That’s cool, as long as you know the proportions of your rectangle.

      set {RecHght, RecWdth} to {40, 160}

      tell application id "com.adobe.InDesign"
      tell active document
      set {PgHght, PgWdth} to {(page height of document preferences), (page width of document preferences)}
      make rectangle with properties {geometric bounds:{(PgHght / 2 - RecHght / 2), (PgWdth / 2 - RecWdth / 2), (PgHght / 2 + RecHght / 2), (PgWdth / 2 + RecWdth / 2)}}
      end tell
      end tell

      Is that what you’re looking for?

      m.

    • absolutely.
      you can also set the colour of the paragraph style THROUGH applescript

      tell application id "com.adobe.InDesign"
      tell active document
      set fill color of paragraph style "ParaStyle" to swatch 1
      end tell
      end tell

      (where “ParaStyle” is the name you’ve given to the style)

      m.

thoughtful and respectful comments welcome

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s