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)

Joe.Hocker 2009-01-26 01:06 PM

Working with the Text of a Shape in OmniGraffle
 
I'm have trouble understanding how to work with text of an object.
I have made a sample and then copied as AppleScript.
I would like to break down the properties so that I can work with them.
I'd like to set the text and maybe even change the text of an existing shape.

Here is what the copied AppleScript looks like:

tell application "OmniGraffle Professional"
tell canvas of front window
make new shape at end of graphics with properties {name: "RoundRect", textSize: {0.800000, 1.000000}, user data: {myFirst: "1", myThird: "3", myforth: "4", mySecond: "2"}, fill: no fill, textPosition: {0.100000, 0.000000}, origin: {109.000000, 177.794006}, vertical padding: 0, size: {231.242004, 26.227800}, text: {text: "test", text: "test", alignment: center}, side padding: 0, draws shadow: false}
end tell
end tell

Below is the the modified AppleScript. As you can see I have commented out the text property because it didn't work.
I can only seem to set the text property when I create the shape.
I would really like to know how to work with the text of a shape.


tell application "OmniGraffle Professional 5"
tell canvas of front window
make new shape
set lastGraphicID to (get id of result)
tell graphic id lastGraphicID
make new user data item with properties {name:"myFirst", value:"1"}
make new user data item with properties {name:"mySecond", value:"2"}
make new user data item with properties {name:"myThird", value:"3"}
make new user data item with properties {name:"myforth", value:"4"}
set name to "RoundRect"
set textSize to {0.8, 1.0}
set fill to no fill
set textPosition to {0.1, 0.0}
set origin to {15.0, 204.397003}
set vertical padding to 0
set size to {231.242004, 26.2278}
--set text to {text: "test", text: "test", alignment: center}
set side padding to 0
set draws shadow to false
end tell
end tell
end tell


Thanks,

cwingrav 2010-05-03 10:42 AM

I would too. This requires some developer help because I can't figure out how to set text without destroying existing text properties (such as color, size, font, etc). I can't even figure out how to get existing color, size and font from text.


in case this helps some (g is a shape):

[CODE]set text of g to { text: "My new text", size: 8, font: "arial", alignment: center, color:{65535,65535,65535}}[/CODE]

RobTrew 2010-05-03 02:22 PM

You can assign a nested property list to reset the [B][COLOR="Blue"]text[/COLOR][/B] property of an existing shape, and apply different formats to different parts of the string ...

[CODE]tell application "OmniGraffle Professional 5"
tell canvas of front window
set text of front shape to {{text:"text in two "}, {text:"fonts", font:"HoeflerText-Regular", size:24}}
end tell
end tell[/CODE]

[UPDATE] To read the formatting properties of different attribute runs, you need to avoid coercing the text property to a string variable. Examples of using [I]references[/I] further down this thread.

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

cwingrav 2010-05-04 04:49 AM

How do I get it to change just the color of the text without replacing the entire text and its properties? Something like:
[CODE]tell application "OmniGraffle Professional 5"
tell canvas of front window
set color of properties of front shape to {0, 0, 65535}
end tell
end tell[/CODE]

Or, how do I get the color of the existing text? Where are the properties for text held as I can't find them in the existing DOM. How would I say the following:
[CODE]tell application "OmniGraffle Professional 5"
tell canvas of front window
set p to color of properties of front shape
-- or
set p to color of second text of properties of front shape
end tell
end tell[/CODE]

RobTrew 2010-05-04 07:52 AM

[QUOTE=cwingrav;76749]How do I get it to change just the color of the text without replacing the entire text and its properties?

Or, how do I get the color of the existing text? Where are the properties for text held as I can't find them in the existing DOM[/QUOTE]

Both impossible, I believe.

My understanding is that Apple Events don't speak the language of "Rich Text", so there is no obvious way for Omni developers (or any other developers) to enable you to fetch rich text properties through applescript ...

There is some recent technical chat between developers grappling with these issues at [URL="http://bit.ly/ce3E3O"]http://bit.ly/ce3E3O[/URL]

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

cwingrav 2010-05-04 08:38 AM

Ah, so it stores this information in a rich text format that is not accessible to us scripts. Any chance a future version of OmniGraffle will just use regular text and store the text attributes as entries in the DOM? This would be quite nice for us scripter end users.

BTW, this post, had I read it earlier, would have saved me hours over the several months I have looked for this answer.

Thanks for the answer. Wish it were a happier one though... argh.

snarke 2010-05-12 12:22 PM

[QUOTE=cwingrav;76757]Thanks for the answer. Wish it were a happier one though... argh.[/QUOTE]

I hate to disagree with the previous posters, but I have no trouble creating a box with (say) green text in it. I believe the following code should be equivalent to what you originally posted....

[CODE]tell application "OmniGraffle 5"
tell canvas of front window
set lastGraphicID to id of (make new shape at end of graphics with properties {name:"RoundRect", fill:no fill, draws shadow:false, size:{231.242004, 26.2278}, vertical padding:0, autosizing:full, origin:{15.0, 204.397003}, textSize:{0.8, 1.0}, textPosition:{0.1, 0.0}, rotation:0, text:{text:"test", font:"Helvetica", size:30, alignment:center, color:{0, 65535, 0}}, draws stroke:true, side padding:0})
tell graphic id lastGraphicID
make new user data item with properties {name:"myFirst", value:"1"}
make new user data item with properties {name:"mySecond", value:"2"}
make new user data item with properties {name:"myThird", value:"3"}
make new user data item with properties {name:"myforth", value:"4"}
end tell
end tell
end tell

[/CODE]

cwingrav 2010-05-12 12:36 PM

[QUOTE=snarke;77096]I hate to disagree with the previous posters, but I have no trouble creating a box with (say) green text in it. I believe the following code should be equivalent to what you originally posted.... [/QUOTE]

**snip**

Creating from scratch is not the problem. There is existing code for that. The problem is querying a graphic for its current properties which the above poster says isn't possible, a limitation of Applescript.

Thanks for the sample code though as someone else will probably find it useful.

ericscheid 2012-02-02 02:49 PM

You can get the styling properties of the text of a graphic with this applescript:

[CODE]
tell application "OmniGraffle Professional 5"
tell canvas of front window
set p to properties of text of shape 1
p
end tell
end tell[/CODE]

you will get something like this
{superscript:missing value, underlined:false, color:{0, 0, 0}, font:"Tahoma Bold", size:12.0, text:"some text", class:text, alignment:center, baseline offset:missing value}

whpalmer4 2012-02-02 03:58 PM

Well, you can get styling properties that apply to the whole thing, yes. However, if you make some different styling to part of the text, your technique doesn't work.

[URL=http://imageshack.us/photo/my-images/440/screenshot20120202at450.png/][IMG]http://img440.imageshack.us/img440/2546/screenshot20120202at450.png[/IMG][/URL]

Copy As Applescript gives:
[code]
tell application "OmniGraffle Professional"
tell canvas of front window
make new shape at end of graphics with properties {fill: no fill, draws shadow: false, size: {131.000000, 23.000000}, side padding: 0, autosizing: full, origin: {99.000000, 180.500000}, text: {{text: "This is a bit of ", text: "This is a bit of ", size: 11}, {text: "colored", text: "colored", size: 11, color: {1.000000, 0.000000, 0.000000}}, {text: " text.", text: " text.", size: 11}}, draws stroke: false}
end tell
end tell

[/code]

Your script fragment gives:

[URL=http://imageshack.us/photo/my-images/716/screenshot20120202at452.png/][IMG]http://img716.imageshack.us/img716/4011/screenshot20120202at452.png[/IMG][/URL]

I believe that shows the problem RobTrew is referring to above.

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 06:00 PM.

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