Get Colors
I have found that the best way to work with colors is to place them in global variables. This bit set the default swatches into variable for easy application later. We want this to fail silently.
try
set noColor to swatch "None" of CurrentDocument
set blackColor to swatch "Black" of CurrentDocument
set blueColor to swatch "C=100 M=0 Y=0 K=0" of CurrentDocument
set whiteColor to swatch "Paper" of CurrentDocument
end try
Get and Set User Preferences
The following bit of code shows how to get the unit preference for a given install, store it, set it to points (for easy layout scripting), and set the unit back to the user's preference. This way work can be done and leave the user's install "untouched" when completed.
tell application "Adobe InDesign CS2"
--Get the user's unit preferences.
--All initial layout must be done in points.
set mViewPrefs to view preferences
set mViewPrefsObject to object reference of mViewPrefs
set mViewPrefsHUnitOrig to horizontal measurement units of mViewPrefsObject
set mViewPrefsVUnitOrig to vertical measurement units of mViewPrefsObject
set horizontal measurement units of mViewPrefsObject to points
set vertical measurement units of mViewPrefsObject to points
--reset user's preferences back to original settings
set dViewPrefs to view preferences
set dViewPrefsObject to object reference of dViewPrefs
set horizontal measurement units of dViewPrefsObject to mViewPrefsHUnitOrig
set vertical measurement units of dViewPrefsObject to mViewPrefsVUnitOrig
end tell
Find and replace a preset
I use this for when I am creating a layout entirely from a script. For example, creating a template and using it multiple times to pour in XML files. This subroutine ensures that I am using the latest version of the layout as determined by the script and not what was left from a previous, and possibly out-of-date runtime.
--find the old preset, delete it, and replace it with the one in this script in case it's old.
set SLCTemplateName to "Contact Sheet"
set presetCount to (get count of document presets)
repeat with a from 1 to presetCount
set thisPreset to object reference of document preset a
if name of thisPreset is SLCTemplateName then
delete document preset a
exit repeat
end if
end repeat
set SLCDocumentPreset to make new document preset with properties {bottom:36, facing pages:false, left:36, name:"Contact Sheet", page height:792, page orientation:portrait, page width:612, right:36, top:36}
set ContactSheet to make new document with properties {document preset:"Contact Sheet"}
Package Document
tell application "Finder"
set thePackageFolderName to kTheXMLFileName & " Folder"
make new folder at (kTheXMLFolder as alias) with properties {name:thePackageFolderName, owner privileges:read write, group privileges:read write, everyones privileges:read write}
set kThePackageFolder to ((kTheXMLFolder as string) & thePackageFolderName) as string
end tell
tell application "Adobe InDesign CS3"
set thePackage to package kCurrentDocument to kThePackageFolder with copying fonts, copying linked graphics, copying profiles, updating graphics, ignore preflight errors and creating report
set theXMLFileNameBookcode to replace_chars(kTheXMLFileName, ".xml", "") of me
export document 1 format PDF type to ((kTheCompletedFolder & theXMLFileNameBookcode & ".pdf") as string) using kThePDFExportPref without showing options
try
close kCurrentDocument saving no
end try
end tell
Whole Script: Open a folder full of INDD files and package. Mac 10.5 or greater, CS4.
set sourceFolder to "Macintosh HD:Users:philipr:Desktop:Sort:CellsFilesToArchive:Lewin_Cells2_66641:indd:" as alias
set destinationFolder to "Macintosh HD:Users:philipr:Desktop:Cells_eBook:"
set kFileList to {}
set item_list to ""
tell application "System Events"
set item_list to get the name of every disk item of sourceFolder
end tell
set item_count to (get count of items in item_list)
repeat with i from 1 to item_count
set the_properties to ""
set the_item_name to item i of the item_list
set the_item to ((sourceFolder & the_item_name) as string) as alias
tell application "System Events"
set file_info to get info for the_item
end tell
if visible of file_info is true then
tell application "Finder"
set theNewFolder to make new folder at (destinationFolder as alias) with properties {name:the_item_name, owner privileges:read write, group privileges:read write, everyones privileges:read write}
end tell
tell application "Adobe InDesign CS4"
open the_item
set thePackage to package document 1 to (destinationFolder & the_item_name) with including hidden layers, copying fonts, copying linked graphics, copying profiles, updating graphics, ignore preflight errors and creating report
close document 1 saving no
end tell
end if
end repeat
Export to Interchange format
global sourceFolder
global destinationFolder
set sourceFolder to (choose folder with prompt "Please select a folder of InDesign CS4 documents.")
set destinationFolder to (choose folder with prompt "Please select a folder to save the converted documents.")
exportCS4ToAdobeInterchange(sourceFolder)
on exportCS4ToAdobeInterchange(theFolder)
set the the_items to list folder theFolder without invisibles
set theFolder to theFolder as string
repeat with i from 1 to number of items in the the_items
set the_item to item i of the the_items
set the_item to (theFolder & the_item) as alias
set this_info to info for the_item
set the item_name to the name of this_info
tell application "Adobe InDesign CS3"
open the_item
set fileOpened to open the_item
export document 1 format InDesign interchange to ((destinationFolder as text) & item_name & ".inx") without showing options
close document 1
end tell
end repeat
end exportCS4ToAdobeInterchange
Get character style sheet names (enclosed in character style groups)
property kCharacterStyleSheetNames : {}
tell application "Adobe InDesign CS4"
tell document 1
set characterStyleGroupCount to (get count of character style groups)
repeat with thisCharacterStyleGroup from 1 to characterStyleGroupCount
set theCharacterStyleGroup to character style group thisCharacterStyleGroup
my processCharacterStyleGroup(theCharacterStyleGroup)
end repeat
end tell
end tell
return kCharacterStyleSheetNames
on processCharacterStyleGroup(characterStyleGroup)
tell application "Adobe InDesign CS4"
tell characterStyleGroup
set characterStyleCount to (get count of character styles)
repeat with thisCharacterStyle from 1 to characterStyleCount
set theCharacterStyleSheet to character style thisCharacterStyle
set styleName to name of theCharacterStyleSheet
set end of kCharacterStyleSheetNames to styleName
end repeat
end tell
end tell
end processCharacterStyleGroup
Easier search and replace text
Just a simple wrapper for the search and replace tools in InDesign (continuing my efforts to unobfuscate the obfuscated).
on SearchAndReplaceText(originalText, replacementText) -- (string, string)
tell application "Adobe InDesign CS4"
-- search and replace (find and change) preferences need to be set *outside* of the document being worked on.
set find text preferences to nothing
set change text preferences to nothing
set find what of find text preferences to (originalText as string)
set change to of change text preferences to (replacementText as string)
tell kCurrentDocument -- this is a global object that is a remnant of the script this was pulled from.
set resultList to change text
end tell
end tell
end SearchAndReplaceText