These forums are now read-only. Please visit our new forums to participate in discussion. A new account will be required to post in the new forums. For more info on the switch, see this post. Thank you!

It has always been possible to create styled text, but a couple of problems with changing the color etc of a part of a text are:

Automatic type conversion of text in Applescript,

buggy screen updating in OmniGraffle.

Some work-arounds are:

Indirection (use of references rather than simple variable assignments),

micro zoom adjustments of the OG screen.

You can change the color of the Nth word by applying that change to a reference (bypassing automatic conversion of the text to a string variable), but you won't see the result on the OG screen until you force a rewrite by making a small change to the zoom level:

Code:

tell application id "OGfl"
tell front window
tell its canvas
set oShape to item 1 of shapes
set refText to a reference to text of oShape
set color of word 3 of refText to {0, 0, 65535}
end tell
set zoom to zoom * 0.99
end tell
end tell

PS for existing formatting which does not correspond to predictable word or character boundaries, it may be useful to inspect/change properties of attribute runs.

Code:

tell application id "OGfl"
tell front window
tell its canvas
set oShape to item 1 of shapes
set refText to a reference to text of oShape
set color of attribute run 2 of refText to {0, 0, 65535}
end tell
set zoom to zoom * 0.99
end tell
end tell

PPS a peculiar quirk of the screen-updating bug: if you subseqently revert to the original zoom level, you will sometimes find that it still displays the previous state of formatting ...

-- Search and replace formatting properties
tell application id "OGfl"
tell front window
tell its canvas
set oShape to item 1 of shapes
set refText to a reference to text of oShape
tell refText
-- change anything redder to green
repeat with oRun in attribute runs
set {lngR, lngG, lngB} to color of oRun
if (lngR > lngG and lngR > lngB) then set color of oRun to {0, 65535, 0}
end repeat
end tell
end tell
set zoom to zoom * 0.99
refText
end tell
end tell

and to simply list them:

Code:

-- List formatting properties
tell application id "OGfl"
tell front window
tell its canvas
set oShape to item 1 of shapes
tell text of oShape -- still an uncoerced reference
set lstProps to properties of attribute runs
end tell
end tell
end tell
end tell

Generally, if you need to script text format changes to OmniGraffle, it is probably quite important that you are aware of a pair of 'model lag' bugs, a Scylla and a Charybdis, which can either mean that the screen is misrepresenting the state which your data will be in when you save it, or can mean that the Applescript interface will be misrepresenting that state.

It turns out that the OmniGraffle screen can even deceive us as to the contents of the text in our documents ...

-- Search and replace formatting properties
tell application id "OGfl"
tell front window
tell its canvas
set oShape to item 1 of shapes
set refText to a reference to text of oShape
tell refText
-- change anything redder to green
repeat with oRun in attribute runs
set {lngR, lngG, lngB} to color of oRun
if (lngR > lngG and lngR > lngB) then set color of oRun to {0, 65535, 0}
end repeat
end tell
end tell
set zoom to zoom * 0.99
refText
end tell
end tell

and to simply list them:

Code:

-- List formatting properties
tell application id "OGfl"
tell front window
tell its canvas
set oShape to item 1 of shapes
tell text of oShape -- still an uncoerced reference
set lstProps to properties of attribute runs
end tell
end tell
end tell
end tell

--

Hi Rob,

I ran into this very thing today.

I came up with the following solution: not as flexible as yours - you'd need to set a property for each text-style you want to use - but it works in my situation, and it's not very much code.