Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [268]

By Root 1532 0
to last item of item 2 of bothLists

set pathPart to items 1 thru -2 of item 1 of bothLists

set newFileName to "as_" & pad(n1) & pad(n2)

set newFileName to newFileName & "." & extension

set text item delimiters to ":"

return (pathPart as string) & ":" & newFileName

end rename

rename("2", "3", "disk:folder:folder:oldName.eps")

The expression as string when applied to a list, as in the very last line of the rename handler, assembles the items of the list, together with the text item delimiters between each pair of items, into a single string. And here's the result:

"disk:folder:folder:as_0203.eps"

Got it right the first time! It's hard to believe, but I am now ready for a practice run using an actual FrameMaker document.

Practice Makes Perfect


If I've learned one thing about programming over the years, it's to practice before doing anything drastic. I'm going to run through the document and pretend to change the illustration names. Instead of really changing them, I'll log a note telling myself what I would have changed the name to if this had been the real thing. So, putting it all together, here's my practice script:

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

return {pathParts, nameParts}

end bust

on rename(n1, n2, oldPath)

set bothLists to bust(oldPath)

set extension to last item of item 2 of bothLists

set pathPart to items 1 thru -2 of item 1 of bothLists

set newFileName to "as_" & pad(n1) & pad(n2)

set newFileName to newFileName & "." & extension

set text item delimiters to ":"

return (pathPart as string) & ":" & newFileName

end rename

tell application "FrameMaker 7.0"

tell document "gromit:Users:matt2:extra:astdg:ch02"

tell text flow 1

set howMany to count (get tables whose table tag is "Figure")

set chapNum to (get paragraph number of paragraphs ¬

whose paragraph tag is "ChapterLabel")

end tell

set chapNum to word -1 of item 1 of chapNum

set allPaths to {}

select paragraph 1 of text flow 1

set counter to 1

repeat howMany times

set oneTable to find table having tag with value "Figure" in it

set thisFile to inset file of inset 1 ¬

of anchored frame 1 of paragraph 1 of cell 1 of oneTable

set newName to ¬

my rename(chapNum, (counter as string), thisFile)

log "I found " & thisFile

log "I'm thinking of changing it to " & newName

select insertion point after selection

set counter to counter + 1

end repeat

end tell

end tell

Observe that I have put in the variable counter, which starts at 1 and is incremented in every loop; this is how I know how many times I've done the find, and therefore tells me the number of the current illustration. I must admit that it took me a couple of tries to get this script to run. When I first tried to run it, I got an error at the point where I call the rename handler. This was because I had forgotten to put the magic word my before it; this tells AppleScript that even though I'm talking to FrameMaker I want to call a handler in my own script. And then, after I made that change and ran the script again, I got another error: it appeared that the rename handler was called but was now choking. The reason was that the variable counter was a number, and the rename handler was passing this on to the pad handler, which was expecting a string. The phrase counter as string converts the number to a string for purposes of passing it to the rename handler.

Here are the first couple of relevant entries of the resulting log:

(*I found gromit:Users:matt2:extra:astdg:figs:scriptEditor.eps*)

(*I'm thinking of changing it to gromit:Users:matt2:extra:astdg:figs:as_0201.eps*)

(*I found gromit:Users:matt2:extra:astdg:figs:scriptEditorDict.eps*)

(*I'm thinking of changing it to gromit:Users:matt2:extra:astdg:figs:as_0202.eps*)

...

And so forth. That looks as good as I could wish. I'm rapidly becoming very confident that my script, when unleashed

Return Main Page Previous Page Next Page

®Online Book Reader