AppleScript InDesign CS 1


Indesign CS 2 | Indesign CS 1 | Indesign ver. 2 | Miscellaneous |




InDesign CS has introduced a large number of changes in scripting.
Shane Stanley make a list of changes and explanations: InDesign CS Scripting Changes.




Index






Bookmark - make Nov. 2, 2004
tell application "InDesign CS"
   
activate
   if (count documents) = 0 then
      display dialog "Please, open a document" buttons " OK " default button 1 with icon caution
   else
      set
myDocument to active document

      tell myDocument
         activate
         set ListOfParagraphStyle to every paragraph style whose name is not "[No Paragraph Style]"
         
set ListOfNameOfParagraphStyle to name of every paragraph style whose name is not "[No Paragraph Style]"
      
end tell

      set
 myDialog to make dialog with properties {name:"Make bookmarks"}
      
tell myDialog
         tell (make dialog column)

            
tell (make border panel) -- Layer
               tell (make dialog column)
                  
make static text with properties {static label:"Style to use :"}
               
end tell
               tell (make dialog column)
                  
set MyStyleToBookmark to make dropdown with properties {string list:ListOfNameOfParagraphStyle, selected index:1}
               
end tell
            end tell
         end tell
      end tell


      -- Display the dialog box.
      set myResult to show myDialog

      if myResult is true then
         -- Get the values from the dialog box controls
         set ParaStyleSelected to (item ((selected index of MyStyleToBookmark) + 1)) of ListOfParagraphStyle

         set properties to {find preferences:nothing, change preferences:nothing}

         
tell myDocument
            set theFinds to search with find attributes {applied paragraph style:ParaStyleSelected}

            
repeat with I from (count of theFinds) to 1 by -1
               
set theContents to contents of (contents of item I of theFinds)
               
set theDest to make hyperlink text destination with properties {destination text:insertion point 1 of (item I of theFinds)}
               
make bookmark with properties {destination:theDest, name:theContents}
            
end repeat
         end tell
         set
 properties to {find preferences:nothing, change preferences:nothing}

      
end if
      -- Remove the dialog box from memory.
      destroy myDialog
   end if
end tell


Top of the script | back to top of this page




Image: reveal in finder Nov. 2, 2004
tell application "InDesign CS"
   set theSelection to item 1 of selection
   set theClass to class of theSelection
   if theClass is in {image, PDF} then
      -- if the image is selected by direct tools
      set theSelection to parent of theSelection
   end if

   -- try to get the file path of the item:
   set theFilePathOfImage to file path of item link of graphic 1 of theSelection
end tell

tell
 application "Finder"
   activate
   reveal the theFilePathOfImage
end tell

Top of the script | back to top of this page


Selection: step and repeat Nov. 2, 2004
set myStep to 30

tell application "InDesign CS"
   
activate
   set myDocument to active document
   tell myDocument
      set mySelection to item 1 of selection
      set {y, x, l, h} to geometric bounds of mySelection

      duplicate mySelection
      move mySelection to {x + 100, y + 0}

      
repeat with I from myStep to (myStep * 9) by myStep
         duplicate mySelection
         move mySelection to {x + 0, y + I}

         
duplicate mySelection
         move mySelection to {x + 100, y + I}
      
end repeat
   end tell
end tell


Top of the script | back to top of this page




Change layer of images Nov. 2, 2004
tell application "InDesign CS"
   
activate
   if (count documents) = 0 then
      display dialog "Please open a document and try again."
   
else
      set
 myTextInfoLayerName to "Images"
      
set myDocument to active document
      tell myDocument
         try
            set myLayer to layer myTextInfoLayerName of myDocument
         on error
            -- Create the layer if it didn't already exist.
            tell myDocument
               set myLayer to make layer
               set name of myLayer to myTextInfoLayerName
            end tell
         end
 try

         -- Search document for linked images
         set IDLinks to links
      
         
repeat with myCounter from 1 to (count IDLinks)
            
set theFrame to parent of parent of item myCounter of IDLinks
            try
               set properties of theFrame to {item layer:myLayer}
            
end try
         end repeat
      end tell
   end if
end tell


beep
Top of the script | back to top of this page




Insert unicode Nov. 2, 2004
tell application "InDesign CS"
   
activate
   if (count documents) = 0 then
      display dialog "Please, open a document" buttons " OK " default button 1 with icon caution
   else
      set
 mySelection to selection
      if (mySelection = {}) or (class of item 1 of mySelection is not insertion point) then
         display dialog "Please, put your cursor in the place you want the character." buttons " OK " default button 1 with icon caution
      else
         set myDialog to make dialog with properties {name:"Insert the unicode"}
         
tell myDialog
            tell (make dialog column)
               
tell (make border panel)
                  
tell (make dialog column)
                     
make static text with properties {static label:"Unicode :"}
                  
end tell
                  tell (make dialog column)
                     
set myUnicode to make text editbox with properties {edit contents:"012B"}
                  
end tell
               end tell
            end tell
         end tell


         -- Display the dialog box.
         set myResult to show myDialog
         if myResult is true then
            set myCode to edit contents of myUnicode
            if (length of myCode) ≠ 4 then
               display dialog "Error" buttons " OK " default button 1 with icon caution
            else
               set myCodeToInsert to "<" & myCode & ">"
               
set insertion point 1 of selection to myCodeToInsert
            end if
         end if

         destroy myDialog
      end if
   end if

end tell

Top of the script | back to top of this page




Images: some properties Nov. 2, 2004
tell application "InDesign CS"
   set mydoc to active document
   tell mydoc
      set PageItemOne to page item 1

      set PropOfPageItem to properties of PageItemOne
      set myImage to graphic 1 of PageItemOne
      -- instead of :
      -- set myImage to image 1 of PageItemOne
      -- in Indesign 2

      set ClassOfImage to class of myImage
      set itemLinkOfImage to item link of graphic 1 of PageItemOne
      set propOfLink to properties of item link of graphic 1 of PageItemOne
      set propImage to properties of graphic 1 of PageItemOne
      set FilePathOfImage to file path of item link of graphic 1 of PageItemOne
   end tell

end tell

Top of the script | back to top of this page




Change style of every number Nov. 16, 2004
This script change the character style of every number of a document.
tell application "InDesign CS"
   
activate

   set find preferences to nothing
   set change preferences to nothing

   tell document 1
      
set theStyle to character style "Number"
      
search with find attributes {find text:"^9"} with change attributes {applied character style:theStyle}
   
end tell

   set
 find preferences to nothing
   set change preferences to nothing
end tell
Top of the script | back to top of this page




Set Caps First Line by Gary Cosimini, Adobe Systems Inc. -- Nov. 19, 2004
tell application "InDesign CS"
   if (count documents) > 0 then
      set mySelection to selection
      if (count mySelection) = 1 then
         if class of item 1 of mySelection is rectangle then -- must select or cursor in text frame
            display dialog "You must select a text frame before running script." buttons "Oh"
         
else
            try-- if  selection is a text frame, then

                (*
               optional segment:
               comment out if you must have the Adobe Paragraph Composer
               but hand work may be required more often after the script is run.

               Note from Olivier:
                  These lines works only if your Indesign is in English.
               *)

               get composer of selection
               set composer of paragraph 1 of selection to "Adobe Single-line Composer"
               
-- end optional segment

               select line 1 of parent story of item 1 of mySelection
               set capitalization of selection to all caps
               select line 2 of parent story of item 1 of mySelection
               set capitalization of selection to normal
            on error
               beep
            end try
         end if
      else

         display dialog "Select a single text frame before running script." buttons "Doh"
          -- the error message "Doh" only makes sense if you watch the cartoon show "The Simpsons" on TV!
      
end if
   else

      display dialog "Please open a document, select a text frame, and try again." buttons "What was I thinking"
   
end if
end tell

Top of the script | back to top of this page




Print Folder Items by Olav Kvern -- Nov. 23, 2004
(*
PrintFolderItems.scpt
An InDesign CS AppleScript
Prints the InDesign documents dropped into a folder using a specified printer preset.
Edit the name of the preset ("TabloidExtra" is the example preset name) to change
the preset used.
*)

on adding folder items to this_folder after receiving myAddedItems
   repeat with myCounter from 1 to (count myAddedItems)
      
tell application "InDesign CS"
         
set myDocument to open item myCounter of myAddedItems
         print myDocument using "TabloidExtra" print dialog no
      end tell
   end repeat
end
 adding folder items to
Top of the script | back to top of this page




Every Text To Layer Nov. 25, 2004
This script "moves" every text frame to a layer named "Text Layer".

tell application "InDesign CS"
   
activate
   if (count documents) = 0 then
      display dialog "Please, open a document" buttons " OK " default button 1 with icon caution
   else
      set
 LayerName to "Text Layer"

      
set myDocument to active document

      
tell myDocument
         try
            -- Does this layer exist ?
             set myLayer to layer LayerName
         on error
            -- No? -> create it
             set myLayer to make layer with properties {name:LayerName}
         
end try
      
         
set properties of every text frame to {item layer:myLayer}
      
end tell
   end if
end tell

Top of the script | back to top of this page




Image proportionnal maxi Jan. 3, 2005
tell application "InDesign CS"
   
tell document 1
      
activate
      set theSelection to selection
      if theSelection is not {} then
         set theClass to class of item 1 of theSelection
         if theClass is in {oval, rectangle, polygon} then
            set theTarget to theSelection
         else if theClass is in {PDF, EPS, image} then
            try
               set theTarget to parent of theSelection
            on error
               try
                  set theTarget to parent of item 1 of theSelection
               end try
            end try
         else

            error number -128
            -- do your stuff here
            -- the selection is not oval, rectangle, polygon
         end if

         tell item 1 of theTarget
            try
               set myProp to properties of item 1 of theTarget
               fit theTarget given content to frame
               set horScale to (horizontal scale of graphic 1) + 0.5
               set vertScale to (vertical scale of graphic 1) + 0.5
               if horScale is greater than vertScale then
                  set vertical scale of graphic 1 to horScale
                  set horizontal scale of graphic 1 to horScale
               else
                  set horizontal scale of graphic 1 to vertScale
                  set vertical scale of graphic 1 to vertScale
               end if
               fit theTarget given center content

            on error ErrorMessage number ErrorNumber
               display dialog ("Error : " & return & ErrorMessage & return & ErrorNumber) buttons "OK" default button 1 with icon 0
            
end try
         end tell
      end if
   end tell
end tell
Top of the script | back to top of this page


Image: search & replace Jan. 11, 2005
This script changes all the hit of an image (selected) by an other image.
global NewImage, theType

tell application "InDesign CS"
   -- activate
   if (count documents) = 0 then
      display dialog "Please, open a document" buttons "OK" default button 1 with icon caution
   else
      set
myDocument to active document

      tell myDocument
         set ImageSelected to selection
         if ImageSelected is {} then
            display dialog "Select the image which you want to change in all the document" buttons "OK" default button 1 with icon caution
            return -128
         
end if
         set ImageSelected to item 1 of ImageSelected
         set theClass to class of ImageSelected
         if theClass is in {image, PDF} then
            -- if the image is selected by direct tools
            set ImageSelected to parent of ImageSelected
         end if
         set PropertiesOfImage to properties of item link of graphic 1 of ImageSelected
         set NameOfImageToChange to name of PropertiesOfImage
         
-- store every "link" in the Indesign document whose the name is the same that the selected image
         set ImagesToChange to every page item whose name of item link of graphic 1 is NameOfImageToChange
      end tell

      
-- select an image in the finder
      tell me to SelectAnImage()

      
if NewImage is not "" then
         copy (display dialog "Are you sure to replace every " & return & ¬
            "'" &
NameOfImageToChange & "'" & return & ¬
            "by" &
return & ¬
            "'" &
NewImage & "' ?" buttons {"OK", "NO"} default button 1 with icon caution) to dlog1

         if button returned of dlog1 ≠ "OK" then
            display dialog "Cancel by the user" buttons " OK " default button 1 with icon caution
            return -128
         
end if

         tell myDocument
            repeat with i from 1 to count of ImagesToChange
               set ItemToChange to item i of ImagesToChange
               set thelink to item link of graphic 1 of ItemToChange
               set ItemLink to properties of thelink
               set {DateOfImage, LinkType, SizeOfImage} to {date, link type, size} of ItemLink
               if (DateOfImage = date of PropertiesOfImage) and (LinkType = link type of PropertiesOfImage) and (SizeOfImage = size of PropertiesOfImage) then
                  try
                     relink item link of graphic 1 of ItemToChange to NewImage
                     update thelink
                  end try
               end if
            end repeat
         end tell

         beep 2
      
end if
   end if
end tell


on SelectAnImage()
   
set theType to ""

   
tell application "InDesign CS"
      
set NewImage to choose file with prompt "Select an image"
   
end tell

   tell
application "Finder"
      
try
         set
theType to file type of NewImage
      end try
      if
theType is not in {"8BIM", "8BPS", "PDF ", "PICT", "PNTG", "TIFF", "EPS ", "EPSF", "JPG ", "JPEG", "PSD "} then
         display dialog "Incorrect type" buttons "OK" default button 1 with icon caution
         set NewImage to ""
      
end if
   end tell
end
SelectAnImage
Top | back to top of this page


Dialog June 10, 2005
set myListOfAngle to {"30", "45", "90", "180"}
set myListOfNames1 to {"John", "George", "Paul", "Ringo"}
set myListOfNames2 to {"Belgium", "France", "Luxembourg", "The Netherlands", "Deutschland", "Canada"}
set myListOfNumber to {"10", "20", "30", "40", "50", "60", "70", "80", "90", "100"}

tell application "InDesign CS"
   activate

   set myDialog to make dialog with properties {name:"*TITLE* of the dialog"}
   tell myDialog
      tell (make dialog column)
         set myEnablingGroup to make enabling group with properties {static label:"Enabling Group", checked state:true}
         tell myEnablingGroup
            tell (make dialog column)
               tell (make dialog row)
                  tell (make dialog column)
                     make static text with properties {static label:"Button Group:", min width:150}
                  
end tell
                  tell (make dialog column)
                     set myFirstButtons to make radiobutton group
                     tell myFirstButtons
                        make radiobutton control with properties {static label:"First Option", checked state:true}
                        make radiobutton control with properties {static label:"Second Option"}
                        make radiobutton control with properties {static label:"Third Option"}
                        make radiobutton control with properties {static label:"Fourth Option"}
                     
end tell
                  end tell
               end tell
               tell (make border panel)
                  tell (make dialog row)
                     tell (make dialog column)
                        make static text with properties {static label:"Group:", min width:150}
                        make static text with properties {static label:"Angle Combobox:", min width:150}
                        make static text with properties {static label:"Angle Editbox:", min width:150}
                        make static text with properties {static label:"Name:", min width:150}
                     
end tell
                     tell (make dialog column)
                        set myGroup1 to make text editboxes with properties {edit contents:"A Group", min width:200}
                        set myListAngleCombobox1 to make angle combobox with properties {string list:myListOfAngle, min width:200}
                        set myListAngleEditbox1 to make angle editbox with properties {edit value:12, min width:200}
                        set myNameDropdown1 to make dropdown with properties {string list:myListOfNames1, selected index:0, min width:100}
                     
end tell
                  end tell
               end tell
             end tell
         end tell -- myEnablingGroup
      
         tell (make border panel)
            tell (make dialog column)
               make static text with properties {static label:"Group:", min width:150}
               make static text with properties {static label:"Angle Combobox:", min width:150}
            
end tell
            tell (make dialog column) -- permet d'avoir des angles
               set myGroup2 to make text editboxes with properties {edit contents:"A Group", min width:175}
               set myListAngleCombobox2 to make angle combobox with properties {string list:myListOfAngle, min width:175}
            
end tell
         end tell
      
         tell (make dialog row)
            tell (make dialog column)
               make static text with properties {static label:"Angle Editbox:", min width:175}
               make static text with properties {static label:"Name:", min width:150}
               make static text with properties {static label:"Mesure:", min width:150}
               make static text with properties {static label:"Pourcent:", min width:150}
            
end tell
            tell (make dialog column)
               set myListAngleEditbox2 to make angle editbox with properties {edit value:12, min width:50}
               set myNameDropdown2 to make dropdown with properties {string list:myListOfNames2, min width:200, selected index:0}
               set myMeasureComboBox to make measurement combobox with properties {string list:myListOfNumber, min width:70}
               set myMeasureComboBox to make percent combobox with properties {string list:myListOfNumber, min width:70}
            
end tell
         end tell
      
         tell (make border panel)
            tell (make border panel) -- checkbox buttons
               set myTopButton1 to make checkbox control with properties {static label:"One", checked state:true, min width:60}
               set myCenterButton1 to make checkbox control with properties {static label:"Two", min width:60}
               set myRightButton1 to make checkbox control with properties {static label:"Three", min width:60}
            
end tell
            tell (make border panel)
               tell (make dialog column)
                  set myTopButton2 to make checkbox control with properties {static label:"Top", checked state:true, min width:45}
                  set myCenterButton2 to make checkbox control with properties {static label:"Center", min width:45}
                  set myBottomButton2 to make checkbox control with properties {static label:"Bottom", min width:45}
               
end tell
             end tell
         end tell
      
         tell (make border panel)
            tell (make radiobutton group) -- radio buttons
               set myTopButton3 to make radiobutton control with properties {static label:"Top", checked state:true, min width:150}
               set myCenterButton3 to make radiobutton control with properties {static label:"Center"}
               set myBottomButton3 to make radiobutton control with properties {static label:"Bottom"}
            
end tell
         end tell
      end tell

   end tell

   set myResult to show myDialog
   if myResult is true then

      
(*
         -- Get the settings from the dialog bow
         
         set myPointSize to edit contents of myPointSizeField as real
         set myString to edit contents of myTextEditField
         if selected index of myVerticalJustificationMenu is 0 then
            set myVerticalJustification to top align
         else if selected index of myVerticalJustificationMenu is 1 then
            set myVerticalJustification to center align
         else
            set myVerticalJustification to bottom align
         end if
         
         get properties of myParagraphAlignmentGroup
         if selected button of myParagraphAlignmentGroup is 0 then
            set myParagraphAlignment to left align
         else if selected button of myParagraphAlignmentGroup is 0 then
            set myParagraphAlignment to left align
         else if selected button of myParagraphAlignmentGroup is 1 then
            set myParagraphAlignment to center align
         else
            set myParagraphAlignment to right align
         end if
         
         
         
*)
   
end if
   destroy myDialog
end tell
Top | back to top of this page


Change language in styles and paragraphs June 10, 2005
tell application "InDesign CS"
   
activate
   if (count documents) = 0 then
      display dialog "Please, open a document" buttons "OK" default button 1 with icon caution
   else
      set
 myDocument to active document

      set listOfLanguage to name of every language with vendors

      set myDialog to make dialog with properties {name:"Change the langage in the document"}
      
tell myDialog
         tell (make dialog column)
            
tell (make border panel)
               
tell (make dialog column)
                  
make static text with properties {static label:"change by this langage :"}
               
end tell
               tell (make dialog column)
                  
set MyLanguage to make dropdown with properties {string list:listOfLanguage, selected index:1}
               
end tell
            end tell
         end tell
      end tell


      -- Display the dialog box.
      set myResult to show myDialog

      if myResult is true then
         -- Get the values from the dialog box controls
         set TargetLanguage to (item ((selected index of MyLanguage) + 1)) of listOfLanguage

         tell myDocument

            set ListOfParagraphStyle to every paragraph style whose name is not "[No Paragraph Style]"
            
set ListOfCharacterStyle to every character style whose name is not "[No Character Style]"
         
            
set myCounter to (count of ListOfParagraphStyle) + (count of ListOfCharacterStyle)
         
            
if myCounter is not equal to 0 then
               if (count of ListOfParagraphStyle) is not equal to 0 then
                  try
                     
set (properties of every paragraph style whose name is not "[No Paragraph Style]") to {applied language:TargetLanguage}
                  
end try
               end if
               if (count of ListOfCharacterStyle) is not equal to 0 then
                  repeat with anItem in ListOfCharacterStyle
                     
set properties of anItem to {applied language:TargetLanguage}
                  
end repeat
               end if
            end if



            try
               set applied language of every text of every text frame of every page to TargetLanguage
            end try

            try
               set applied language of every text of every cell of every table of every text frame of every page to TargetLanguage
            end try

         end tell
         display dialog "Done" buttons "OK" default button 1
      
      
end if

      -- Remove the dialog box from memory.
      destroy myDialog

   
end if
end tell

Top | back to top of this page


Un-anchor June 30, 2005 -- by Pete Kincaid
From Pete Kincaid: I have written this script which will un-Anchor text boxes and insert there
text into the string of text where the box was anchored.


tell application "InDesign CS"
   
tell document 1
      
set thetextframes to text frames
      repeat with aframe in thetextframes
         set theCount to count of (text frames of aframe)
         
tell aframe
             repeat with i from theCount to 1 by -1
               
set boxContents to paragraphs of text frame i of aframe as string
               set contents of first insertion point of parent of text frame i to boxContents
               delete text frame i of aframe
             end repeat
         end tell
      end repeat
   end tell
end tell

Top of the script | back to top of this page


Copy cell from Excel Oct. 27, 2005
tell application "Microsoft Excel"
   set x to valueof selection
   -- convert list of lists to single list
   set oldDelims to AppleScript's text item delimiters
   set AppleScript's text item delimiters to {"&˘ˇ˜"}
   
set x to x as string
   set x to text items of x
   set AppleScript's text item delimiters to oldDelims
end tell

tell
application "InDesign CS"
   
set contents of selection to x
end tell

Top of the script | back to top of this page




New layer Jan. 18, 2006
tell application "InDesign CS"
   tell document 1
      set newLayer to make layer with properties {name:"Bottom layer", layer color:teal}
      move newLayer to after layer -1
   end tell
end tell
Top of the script | back to top of this page




Index from character style Febr. 20, 2006
tell application "InDesign CS"
   set find preferences to nothing
   set change preferences to nothing

   set myDocument to active document

   tell myDocument
      set myIndex to make index
   end tell

   tell
myDocument
      set EveryCharacterStyle to every character style
      set NameOfEveryCharacterStyle to name of every character style

      set myIndex to make index
   end tell

   set
ChooseStyle to choose from list NameOfEveryCharacterStyle with prompt "Choose one (or more) character style(s)" with multiple selections allowed

   if ChooseStyle is not false then
      -- Search for the correspondance of Name of char style and object reference of it
      set ListOfCharStyleToIndex to {}
      tell myDocument
         repeat with i from 1 to count of ChooseStyle
            set end of ListOfCharStyleToIndex to character style (item i of ChooseStyle)
         
end repeat
      end tell


      -- Search for every occurence of text with the first applied character style in ListOfCharStyleToIndex
      set SearchStyle to item 1 of ListOfCharStyleToIndex
      set FoundObjects to (search myDocument with find attributes {applied character style:SearchStyle})
      
-- When multiple selections
      repeat with j from 2 to count of ListOfCharStyleToIndex
         set SearchStyle to item j of ListOfCharStyleToIndex
         set FoundObjects to FoundObjects & (search myDocument with find attributes {applied character style:SearchStyle})
      
end repeat

      -- Loop (reverse)
      repeat with i from (count FoundObjects) to 1 by -1
         tell myIndex
            -- Add the topic to the index if it does not already exist.
            try
               set
myTopicName to contents of item i of FoundObjects
               set myTopic to topic myTopicName of myIndex
            on error
               set
myTopic to make topic with data {name:myTopicName}
            
end try
         end tell
         set
myFoundItem to object reference of item i of FoundObjects
         set myStory to parent story of myFoundItem
         -- Create the page reference.
         tell myTopic
            set myPageReference to make page reference with properties {parent story:myStory, story offset:index of myFoundItem}
         
end tell
      end repeat

      display dialog "Done!" buttons "OK" default button 1 giving up after 2
   
end if

   set
find preferences to nothing
   set change preferences to nothing

end tell
Top of the script | back to top of this page







Last updated June 22, 2006


Valid HTML 4.01!