Sorry, it totally slipped my mind to reply to this post regarding your latest issues.
Yes, indeed there is. You can do it like this:
set fnt to "Opus Chords Std"
tell application "Pages" to tell the front document
repeat with P in (a reference to every page)
set _T to (a reference to P's body text)
-- replace hash with sharp
-- set (every character of T where it is "#") to "♯"
set (the font of every character of _T where it is "#") to fnt
-- replace b with flat
set (the font of the last character of ¬
(every word of _T where ¬
it is "Ab" or ¬
it is "Bb" or ¬
it is "Cb" or ¬
it is "Db" or ¬
it is "Eb" or ¬
it is "Fb" or ¬
it is "Gb")) to fnt
end repeat
end tell
That has to be a bug in Pages or AppleScript. When I tested the script above where the font of specific characters are changed, I did so on a slightly longer piece of sample text, and I encountered the same phenomenon. There's nothing wrong with the code, but in one instance, the script failed with an error code of 10000
and a message that said the AppleScript event handler had failed, which is always AppleScript not doing what it's supposed to do.
If this happens to you in any future scripts (it happens with System Events a fair bit), your two choices are to change nothing and just run the script again, and it'll probably work; or, if it persistently throws that error, find another means to achieve the same objective (which can be hard).
I ran the script again, and it was fine. I also noticed that in running the script again, some fo the characters that weren't replaced in the first or second run we then replaced successfully in the third; but not all, and I had to run the script four times to change one page of text.
It got there in the end, but it's pretty ridiculous that this is occurring. I don't have a fix for that right now, so I guess you might have to run the script a couple of hundred times until it's done (which Keyboard Maestro can help you do).
Ah, yes, it would do. I didn't know there'd be tables in the document.
This is where we really hit a bit of a wall with what your options are here.
There are two issues with tables in Pages documents. The first is another bug in AppleScript, which is unable to retrieve the object references of any table that was inserted manually into the document unless you first select each table manually and change its Object Placement
setting (found under Arrange
) from Move with Text
to Stay on Page
.
Again, it's ridiculous. But there's no workaround to this.
After that is done, the next irritation is that the text contained in cells of a table aren't retrieved as rich text
items by AppleScript, and instead the content of the text is stored in the value
property of each cell
of the table, which is just a simple unary string value with no way to manipulate the characters in the way we can with the rest of the document.
You can iterate through each cell in the table to get these string values like this:
tell application "Pages" to tell the front document
set _P to a reference to every page
set _T to a reference to every table of _P
set _C to a reference to every cell in _T
repeat with C in (_C whose value is not missing value)
set v to C's value
.
.
.
set C's value to ...
end repeat
end tell
However, what you do at that point it beyond me. You could do a search and replace manually using the menu in Pages. Whilst this will successfully find words in tables, it will only let you substitute words and letters with words and letters; you couldn't use it to change the font.