The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   OmniGraffle General (http://forums.omnigroup.com/forumdisplay.php?f=10)
-   -   Applescript - 2 problems in linking two shapes with a line (http://forums.omnigroup.com/showthread.php?t=20944)

RobTrew 2011-05-04 09:06 PM

Applescript - 2 problems in linking two shapes with a line
 
Not sure if others have found a solution to these two problems in scripting the creation of a line connecting two shapes.
[LIST=1][*][I]source[/I] and [I]destination[/I] can apparently not be specified in the [I]make new line[/I] property list.[*]code-created links between horizontally aligned objects are not horizontal[/LIST]
[IMG]http://farm6.static.flickr.com/5263/5689489914_a4bf034a31.jpg[/IMG]

[CODE]property pstrArrow : "FilledArrow"
property plstRed : {65535, 0, 0}

tell application id "OGfl"
tell (make new document)
tell front canvas
-- DISABLE AUTOMATIC LAYOUT
set automatic layout of its layout info to false

-- GET RELATIVE UNITS
set {rX, rY} to page size
set rSize to rX / 10
set rDown to rY / 3

set shapeA to make new shape with properties {name:"Circle", origin:{rDown, rDown}, size:{rSize, rSize}}
set shapeB to make new shape with properties {name:"Circle", origin:{rDown + (rSize * 1.3), rDown}, size:{rSize, rSize}}

-- PROBLEM 1: The following line of code fails ("AppleEvent handler failed")

-- set oLine to make new line with properties {source:shapeA, destination:shapeB, stroke color:plstRed, head type:pstrArrow}

-- (source and destination can apparently not be specified in the initial property list, before the line has been created,
-- Instead we have to create the line and THEN specify source and destination):
set oLine to make new line with properties {stroke color:plstRed, head type:pstrArrow}
tell oLine
set source to shapeA
set destination to shapeB
end tell

-- PROBLEM 2: The above creates a line which does NOT take the shortest (i.e. horizontal) path between the two shapes

end tell
end tell
end tell[/CODE]

john.gersh 2011-05-05 06:12 AM

I don't know why Problem 1 is happening, but I think I can solve Problem 2:

I'm not sure of the exact cause, but you didn't specify magnet locations on the shapes. Perhaps there's a bug or rounding error when OG calculates the initial endpoint for the line on shapeB's edge. I believe the line should be pointing toward the shape's center in this case, and the error could be in calculating the intersection of the center-to-center line with the edge of shapeB. It's interesting to note that the amount of the error changes if you switch the order of the set source and set destination lines in the script, and that the error disappears the first time you move one of the shapes even the slightest amount (bug-finding clues for OG?).

In any case, adding, for example, the property magnets:{{0.5, 0.0}} when making shapeA and magnets:{{-0.5, 0.0}} when making shapeB fixes Problem2. If you actually wanted the line to be center-pointing rather than horizontal, making {0.0, 0.0} magnets instead would do that.

RobTrew 2011-05-05 02:03 PM

[QUOTE=john.gersh;96828]If you actually wanted the line to be center-pointing rather than horizontal, making {0.0, 0.0} magnets instead would do that.[/QUOTE]

Thank you ! That seems a good solution. I hadn't thought of magnets at the centre of shapes, and had been resorting to 'nudging' the shapes with code to make the lines snap to the shortest path - slower and visually disconcerting :-)

RobTrew 2011-05-06 02:13 AM

Just for the record - john.gersh's solution works well, producing a link along the shortest path between the shapes.

[CODE]property pstrArrow : "FilledArrow"
property plstRed : {65535, 0, 0}
property plstCenter : {0, 0}

tell application id "OGfl"
tell (make new document)
tell front canvas
-- DISABLE AUTOMATIC LAYOUT
set automatic layout of its layout info to false

-- GET RELATIVE UNITS
set {rX, rY} to page size
set rSize to rX / 10
set rDown to rY / 3

-- MAKE SHAPES WITH MAGNETS AT THEIR CENTERS
set shapeA to make new shape with properties {name:"Circle", origin:{rDown, rDown}, size:{rSize, rSize}, magnets:{plstCenter}}
set shapeB to make new shape with properties {name:"Circle", origin:{rDown + (rSize * 1.3), rDown}, size:{rSize, rSize}, magnets:{plstCenter}}

-- CONNECT THE SHAPES WITH A LINE
tell (make new line with properties {stroke color:plstRed, head type:pstrArrow}) to set {source, destination} to {shapeA, shapeB}
end tell
end tell
end tell[/CODE]

RobTrew 2011-06-09 10:38 AM

Re problem 1 (setting source and destination of a line within the property list), I had, of course, overlooked the [B]connect[/B] method, which does exactly what I wanted, without the visual distraction of repositioning lines.


All times are GMT -8. The time now is 02:22 PM.

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