Pages and Bookman

While working on a script converting AppleWorks documents into Pages ones to export them as PDF I discovered an annoying feature.
When the document use the font named Bookman, the imported document looks well in Pages but in the final PDF I get only a rectangle for every character.

So I tried to identify parts of text using Bookman.

As I am curious, I try to identify the documents using the Bookman font.
if I extract the font name of a text, I get the attribute or its 1st character
If I extract the font name of every paragraph I get the attribute of the 1st character of all of them.
If I was able to extract the font name of every Attribute run I would get the attribute of every chunks that all have the same attributes.
At first look it seems that it may be better than grabbing the font name of every character.

Alas, Every attempt to use “attribute run” fails with infamous error -1728

(*
Pages v4.x must be running while Pages v5.x is not loaded
*)

if (get version of application id "com.apple.iWork.Pages") does not start with "4" then error "Oops, this script requires Pages v.4.x"

set cheminDocAW to choose file "choose an AppleWorks WP file" of type {"CWWP"}

tell application id "com.apple.iWork.Pages"
	open cheminDocAW
	tell document 1
		tell its paragraphs
			font name
			--> {"Palatino-Roman", "Palatino-Roman", "Palatino-Roman", "Palatino-Roman", "Palatino-Roman", "Palatino-Roman", "Palatino-Roman", "Palatino-Roman", "Palatino-Roman", "Palatino-Roman", "Bookman-Light", "Bookman-Light", "Bookman-Light", "Bookman-Light", "Bookman-Light", "Bookman-Light", "Bookman-Light", "Bookman-Light", "Bookman-Light", "Bookman-Light", "Bookman-Light", "Bookman-Light", "Bookman-Light", "Palatino-Roman", "Palatino-Roman", "Palatino-Roman", "Palatino-Roman", "Palatino-Roman", "Palatino-Roman", "Palatino-Roman"}
		end tell
		tell its paragraph 1
			--font name of attribute runs --> error number -1728 from every attribute run of paragraph 1 of document 1
		end tell
		tell its text
			font name of its attribute runs
			--> error "Erreur dans Pages 09 : Il est impossible d'obtenir every attribute run of every text of document 1." number -1728 from every attribute run of every text of document 1
		end tell
	end tell
end tell

Mac OS error -1728 (errAENoSuchObject): e.g.,: specifier asked for the 3rd, but there are only 2. Basically, this indicates a run-time resolution error.

As the dictionary describes an attribute named “font” which is in fact named “font name” I wonder if “attribute run” must be spelled an other way.

[format]attribute run‚n [inh. item] : This subdivides the text into chunks that all have the same attributes.
elements
contains attachments, attribute runs, characters, paragraphs, words; contained by attribute runs, characters, paragraphs, text, words.
properties
color (color) : The color of the first character.
font (text) : The name of the font of the first character.
size (integer) : The size in points of the first character.[/format]

Maybe one of you know the correct incantation.

Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) dimanche 4 septembre 2016 15:32:30

Hi Yvan.

I can confirm that, despite what it says in the dictionary, Pages '09 doesn’t understand attribute runs or attachments. The Text Suite tends to be a standard-issue affair that gets put into dictionaries whether its relevant or not. The text elements listed in Pages '09’s Pages Text Suite are just characters, charts, graphics, images, insertion points, lines, paragraphs, shapes, tables, text, text boxes, and words.

Edit: You could roll your own, I suppose. If you’re using an ASObjC-compatible system, this returns the font names for all the attribute runs in the text. It could be adapted to return other attribute-run properties at the same time:

use AppleScript version "2.3.1"
use framework "Foundation"
use framework "AppKit"

tell application "System Events"
	set frontmost of application process "Pages" to true
	keystroke "ac" using {command down}
end tell
delay 1

set |⌘| to current application
set thePasteboard to |⌘|'s class "NSPasteboard"'s generalPasteboard()
set theNSAttributedString to (thePasteboard's readObjectsForClasses:({|⌘|'s class "NSAttributedString"}) options:(missing value))'s firstObject()
set len to theNSAttributedString's |length|()
set attributeRunFontNames to |⌘|'s class "NSMutableArray"'s new()
set i to 0
repeat while (i < len)
	set {theseAttributes, thisRange} to theNSAttributedString's attributesAtIndex:(i) longestEffectiveRange:(reference) inRange:({location:i, |length|:len - i})
	set thisFontName to (theseAttributes's valueForKey:("NSFont"))'s fontName()
	tell attributeRunFontNames to addObject:(thisFontName)
	set i to i + (thisRange's |length|)
end repeat
attributeRunFontNames as list

Thanks Nigel.
It does the job.
So it gives a way to identify the documents containing some Bookman characters.

This afternoon I discovered that a whose filter is perfect to change the font of some characters.
It’s fair enough to take care of style attributes (at least if those applied upon Bookman characters are available in the replacement font).

(*
Pages v4.x must be running while Pages v5.x is not loaded
*)

if (get version of application id "com.apple.iWork.Pages") does not start with "4" then error "Oops, this script requires Pages v.4.x"

--set cheminDocAW to choose file "choose an AppleWorks WP file" of type {"CWWP"}

tell application id "com.apple.iWork.Pages"
	--open cheminDocAW
	tell document 1
		tell its text
			set font name of every character whose font name is "Bookman-Light" to "TimesNewRomanPSMT"
		end tell
	end tell
end tell

on the original text, your script returned :

[format]{“Palatino-Roman”, “Bookman-Demi”, “Bookman-Light”, “Bookman-Demi”, “Palatino-Roman”, “LucidaGrande”, “Palatino-Roman”, “Bookman-Light”, “Bookman-Demi”, “Bookman-Light”, “Palatino-Roman”}[/format]

After running the script above it returns :
[format]{“Palatino-Roman”, “TimesNewRomanPS-BoldMT”, “TimesNewRomanPSMT”, “TimesNewRomanPS-BoldMT”, “Palatino-Roman”, “LucidaGrande”, “Palatino-Roman”, “TimesNewRomanPSMT”, “TimesNewRomanPS-BoldMT”, “TimesNewRomanPSMT”, “Palatino-Roman”}[/format]

It’s exactly what I needed.

Time to find a font resembling more to Bookman than this brave Times which is more compact than Bookman so the document looks really differently.

Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) dimanche 4 septembre 2016 22:53:55