AppleScript_ The Definitive Guide - Matt Neuburg [267]
end tell
end tell
end tell
The result is this:
{"Chapter 2"}
That's the right answer! It's a list because I asked for all such paragraphs, but it's a list of just one item because there is only one such paragraph, and the string "Chapter 2" provides the chapter number for this chapter. Now, of course, I need to extract just the "2" from this string, but that's easy, because AppleScript understands the concept of a word:
tell application "FrameMaker 7.0"
tell document "gromit:Users:matt2:extra:astdg:ch02"
tell text flow 1
set chapNum to (get paragraph number of paragraphs ¬
whose paragraph tag is "ChapterLabel")
end tell
end tell
end tell
set chapNum to word -1 of item 1 of chapNum
chapNum
The element item 1 extracts the single string "Chapter 2" from the list, and the element word -1 extracts the last word of that. The result is "2", which is perfect.
I want this number formatted to have two digits. Now, this is a piece of functionality I'm going to need more than once, so I write a little handler (a subroutine). This handler's job is to accept a string and pad it with zero at the front until it is two characters long. Having written the handler, I add some code to call the handler and test it, twice, because I want to make sure it works for a string that is either one or two characters long.
on pad(s)
repeat while length of s < 2
set s to ("0" & s)
end repeat
return s
end pad
log pad("2")
log pad("22")
The pad handler makes use of concatenation (via the ampersand operator) to assemble the desired string. The last two lines do two things:
The pad command calls the pad handler that appears earlier in the code.
The log command puts the result of the pad command into the Event Log History window. The Event Log History window must be opened manually before running the script.
Logging like this is a good approach when you want to test more than one thing in a single running of a script. The result looks good:
(*02*)
(*22*)
Those parentheses and asterisks are comment delimiters; I don't quite understand why the log window uses them, but it doesn't matter.
Another problem is that the new name of each illustration is going to be based partly on its old name. I need to break up the illustration file's pathname into its components, and I need to break up the last component, the actual name of the file, into the name itself and the file-type suffix, because the only part of the pathname I want to change is the name itself.
The typical AppleScript way to break up a string into fields based on some delimiter is to set the special variable text item delimiters to that delimiter and then ask for the string's text items. So I'll do that twice, once with colon as the delimiter and again with period as the delimiter. Once again, I'll make this a handler, just because it makes the script so much neater. I'll have the handler return both results, the list of pathname components together with the list of filename components, combined as a single list. That way, when I call this handler, I will have all the pieces and can reassemble them the way I want:
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
bust("disk:folder:folder:file.suffix")
The result shows that the right thing is happening:
{{"disk", "folder", "folder", "file.suffix"}, {"file", "suffix"}}
Now I'm ready to practice renaming an illustration file. I'll write a handler that takes two numbers and the current pathname of the illustration file and generates the new pathname for that file:
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