AppleScript_ The Definitive Guide - Matt Neuburg [269]
Finder's Keepers
I must not change only the file reference of each illustration within FrameMaker; I must change also the name of the actual illustration file. This involves speaking to the Finder. As a test, I create a file and run a little code to make sure I know how to tell the Finder how to change the name of a file.
tell application "Finder"
set name of file "feathers:Users:mattneub:Desktop:testing.txt" to "itWorked"
end tell
This works, and now I'm ready to run my original script for real. Before doing so, I make sure I have backup copies of everything involved, in case something goes wrong. Even with backups, it's a scary business making such possibly disastrous changes, both in a FrameMaker document and on disk, so I start by running the script against a document that has just one illustration—in fact, I run it against this very document, Appendix A.
To make the script work for real, I change it in two places. First, at the start I add another little handler to extract the final component from a pathname, so that I can obtain the new name that the Finder is to give to the illustration file:
on justName(s)
set text item delimiters to ":"
return last text item of s
end justName
Second, I replace the two "log" lines from the previous version with this:
set newShortName to my justName(newName)
tell application "Finder" ¬
to set name of file thisFile to newShortName
set inset file of inset 1 ¬
of anchored frame 1 of paragraph 1 of cell 1 of oneTable ¬
to newName
The first two lines are simply a rewrite of the Finder file-renaming code just tested a moment ago, with the values coming from variables in the script instead of being hard-coded as literal strings. The second line actually changes the name of the illustration file on disk. (Observe that I can talk to the Finder even inside code where I'm already talking to FrameMaker.) The last line is the only one that makes an actual change in the FrameMaker document—the crucial change, the one I came here to make, altering the illustration's file reference to match the new pathname of the illustration file. I run the script against Appendix A, and it works; the illustration file's name is changed, and the illustration's file reference in the FrameMaker document is changed to match.
I've Got a Little List
Recall that one of my purposes is to generate the figure list requested by the illustration department, as shown in Table A-1. I already know the chapter number, the illustration number, and the illustration file's name. The only missing piece of information is the illustration's caption. The FrameMaker dictionary shows that a table has a title property that looks like what I want. A quick test against a specific table shows that it is:
tell application "FrameMaker 7.0"
set theTitle to (get title of table 36 of document ¬
"gromit:Users:matt2:extra:astdg:appa")
end tell
This works, but because of the way the template is constructed, it includes an unwanted return character at the start of the result. To eliminate this, I use an AppleScript expression that extracts all but the first character of a string:
tell application "FrameMaker 7.0"
set theTitle to text from character 2 to -1 of ¬
(get title of table 36 of document ¬
"gromit:Users:matt2:extra:astdg:appa")
end tell
That works; the result is this:
"FrameMaker dictionary"
That is indeed the caption of the first illustration of this chapter. AppleScript can write to a file, so all I need now is a handler that appends to a file, nicely formatted, a line containing the information for the illustration currently being processed. Here, then, is the final version of the script, including this handler and a call to it:
on pad(s)
repeat while length of s < 2
set s to ("0" & s)
end repeat
return s
end pad
on bust(s)
set text item delimiters to ":"
set pathParts to text items of s
set text item delimiters to "."
set nameParts to text items of last item of pathParts