The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   AppleScripting Omni Apps (http://forums.omnigroup.com/forumdisplay.php?f=46)
-   -   Working with the Text of a Shape in OmniGraffle (http://forums.omnigroup.com/showthread.php?t=11161)

RobTrew 2012-02-03 01:16 AM

It has always been possible to [B]create [/B] styled text, but a couple of problems with [B]changing[/B] the color etc of a part of a text are:
[LIST=1][*]Automatic type conversion of text in Applescript,[*]buggy screen updating in OmniGraffle.[/LIST]
Some work-arounds are:
[LIST=1][*]Indirection (use of references rather than simple variable assignments),[*]micro zoom adjustments of the OG screen.[/LIST]
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[/CODE]

RobTrew 2012-02-03 01:44 AM

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

[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[/CODE]

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 [B]previous[/B] state of formatting ...

(A save and reopen will fix it).

[COLOR="White"]--[/COLOR]

RobTrew 2012-02-03 03:08 AM

Thus, to search and replace text formatting properties:

[CODE]-- 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[/CODE]

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[/CODE]

[COLOR="White"]--[/COLOR]

RobTrew 2012-02-09 02:00 AM

Generally, if you need to script text format changes to OmniGraffle, it is probably quite important that you are aware of [URL="http://forums.omnigroup.com/showthread.php?t=23246"]a pair of 'model lag' bugs, a Scylla and a Charybdis[/URL], 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 [URL="http://forums.omnigroup.com/showthread.php?p=107210"]contents of the text[/URL] in our documents ...

[COLOR="White"]--[/COLOR]

realbrick 2013-08-25 01:01 PM

[QUOTE=RobTrew;106949]Thus, to search and replace text formatting properties:

[CODE]-- 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[/CODE]

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[/CODE]

[COLOR="White"]--[/COLOR][/QUOTE]
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.

(I didn't read the whole thread - apologies if someone earlier offered this.)

-- create a list of all desired text properties EXCEPT for its text property:

property _labelTextProperties: {size:_fontSize, color:{1, 1, 1}}

-- to create an object with a caption using the style above:

set _label to make new shape at end of graphics with properties {text: _labelTextProperties & {text: _labelText}, [other object properties]}

-- to change the text of (the text of) the object:

set text of _label to _labelTextProperties & {text: _newLabelText}


All times are GMT -8. The time now is 03:43 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.