Open all files, run a script, save all files

I have a short AScript that performs a few actions with InDesign on converted XPress documents. It works fine as is. I would like to take it to the next level.

I want to now have this script somehow affect multiple files in a given directory. Open all of those files, run the script, and save all of those files. I am terribly confused as to where to start with this. Here is the script as it is today… I believe I need to incorporate calls to Finder as well?.. I appreciate your help:

tell application “InDesign CS”
set superSize to “58.3” as number
set superPos to “33.3” as number
set subSize to “58.3” as number
set subPos to “33.3” as number
activate
tell document 1
set properties to {text preferences:{superscript size:superSize, superscript position:superPos, subscript size:subSize, subscript position:subPos}}
end tell
end tell

-Molly

It’s a simple script. Just use the standard addition, “choose folder” to return a list of files. Then you run through the list, repeating your script for every file.

set theFolder to choose folder --Folder with files you want to process 
set theFiles to list folder theFolder without invisibles 

repeat with i from 1 to number of items in the theFiles 
   set thisFile to item i of the theFiles 
     open thisFile
     --put your script here
    close thisFile saving yes
end repeat

Thanks, Dennis…
That is ALMOST working.
When I run, the “merged” script (put my snippet with yours appropriately), we’re getting an error. Here’s what the script looks like now:

set theFolder to choose folder --Folder with files you want to process
set theFiles to list folder theFolder without invisibles

repeat with i from 1 to number of items in the theFiles
set thisFile to item i of the theFiles
open thisFile
tell application “InDesign CS”
set superSize to “58.3” as number
set superPos to “33.3” as number
set subSize to “58.3” as number
set subPos to “33.3” as number
activate
tell document 1
set properties to {text preferences:{superscript size:superSize, superscript position:superPos, subscript size:subSize, subscript position:subPos}}
end tell
end tell
close thisFile saving yes
end repeat

When we get to the “tell document 1”, it’s hanging… probably because there is no document 1 in the log… but rather, the name of each individual document. Shouldn’t I set some sort of variable to hold that?

SO C L O S E

M

try replacing “tell document 1” with “tell active document”.

A better way, since all you are doing is opening the documents just to change prefs is to use the “showing window” parameter with the “set docName to open path:to:doc” verb. Then your script will be much faster, since windows won’t open and close, taking time. This will also set your file name to the variable docName for easy reference. You will also have to take out the activate verb (this would make the window visible). Here’s you script fixed:

set theFolder to choose folder --Folder with files you want to process 
set theFiles to list folder theFolder without invisibles
repeat with i from 1 to number of items in the theFiles
    set thisItem to item i of the theFiles
    set thisFile to theFolder & thisItem as string
    tell application "InDesign CS"
        try
          set myDocument to open thisFile without showing window
          set superSize to "58.3" as number
          set superPos to "33.3" as number
          set subSize to "58.3" as number
          set subPos to "33.3" as number
          tell document myDocument
              set properties to {text preferences:{superscript size:superSize, superscript position:superPos, subscript size:subSize, subscript position:subPos}}
          end tell
          close document myDocument saving yes
        end try
    end tell
end repeat

There are many undocumented (at least I can’t find them anywhere else) tips like this in this PDF (564Kb) by Olav Martin Kvern:
Working with Documents
Check it out.
I also put a try block in to catch any files that aren’t ID documents. If the open document succeeds, it will set the prefs, if not, it silently fails and tries the next doc. You can capture the failed docs like this:

set failedDocs to ""
--your code
try
     --your code
on error
   set failedDocs to failedDocs & (thisFile as string) & return
end try
--your code
display dialog failedDocs

I made one mistake in that script. myDocument is a reference to the document, so you don’t have to say “document myDocument”, just “myDocument” like so:

 tell myDocument 
   set properties to {text preferences:{superscript size:superSize, superscript position:superPos, subscript size:subSize, subscript position:subPos}} 
end tell 
close myDocument saving yes

Another AppleScript note, when a property is a number, you can just use the number without quotes and “as number”
set superSize to 58.3
is sufficient.

i’m having trouble scripting the preferences in InDesign CS… the dictionary and ref guides aren’t working.
I just want something that toggles on/off the typographer’s quotes… so when I do a find and replace I can be sure it’s putting in " as inch marks or " as smart quotes.