Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [270]

By Root 1603 0

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

on justName(s)

set text item delimiters to ":"

return last text item of s

end justName

on writeInfo(n1, n2, theName, theTitle)

set s to return & n1 & "-" & n2 & tab & theName & tab & theTitle & return

set f to open for access ¬

file "feathers:Users:mattneub:figs" with write permission

write s to f starting at (get eof of f)

close access f

end writeInfo

tell application "FrameMaker 7.0"

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

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)

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

set theTitle to text from character 2 to -1 of (get title of oneTable)

my writeInfo(chapNum, (counter as string), newShortName, theTitle)

select insertion point after selection

set counter to counter + 1

end repeat

end tell

end tell

There is just one thing I don't like about that script, namely this line:

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

That line hard-codes the pathname of the document file. This works, but it means that I have to change the script manually for each file I process. That's not so terrible, as only a few chapters of this book have any illustrations at all, but it would be nice not to have to do it at all, if only because it seems a possible source of error. Nevertheless, I think we can save this matter for some future round of refinements, and for now at least, consider the problem solved.

Conclusions, Lessons, and Advice


You'll no doubt have noticed that most of my time and effort working on this problem was spent wrestling with the particular scriptable application I was trying to automate. In general, that's how it is with AppleScript. AppleScript itself is a very small language; it is extended in different ways by different scriptable applications. Trying to work out what a particular scriptable application will let you say and how it will respond when you say it constitutes much of the battle of working with AppleScript.

Another feature of the struggle is that AppleScript's error messages aren't very helpful, and it lacks a debugging environment (unless you use Script Debugger as your script editor application), so it's important to proceed with caution and patience. When you try to execute a script, all you really know is that it worked or it didn't; if it didn't, finding out why isn't easy. You can see that I developed my final script slowly and in stages, testing each piece as I went along. I knew that the pieces worked before I put them into place; that way I could be pretty confident that I knew what the script as a whole would do.

Here, to conclude, are a few apophthegms to live by, derived from the foregoing. I hope you'll find this advice helpful in your own AppleScript adventures:

Use the dictionary.

The biggest problem you face as you approach driving a scriptable application is that you don't know the application's "object model"—what sorts of thing it thinks of itself as knowing about, what it calls these things, and how the things relate to one another. In

Return Main Page Previous Page Next Page

®Online Book Reader