The Omni Group
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!

Go Back   The Omni Group Forums > OmniGraffle > OmniGraffle General
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
Applescript - 2 problems in linking two shapes with a line Thread Tools Search this Thread Display Modes
Not sure if others have found a solution to these two problems in scripting the creation of a line connecting two shapes.
  1. source and destination can apparently not be specified in the make new line property list.
  2. code-created links between horizontally aligned objects are not horizontal



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
 
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.

Last edited by john.gersh; 2011-05-05 at 07:29 AM..
 
Quote:
Originally Posted by john.gersh View Post
If you actually wanted the line to be center-pointing rather than horizontal, making {0.0, 0.0} magnets instead would do that.
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 :-)
 
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
 
Re problem 1 (setting source and destination of a line within the property list), I had, of course, overlooked the connect method, which does exactly what I wanted, without the visual distraction of repositioning lines.
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Odd behavior - line tool rearrances shapes? [A: template with auto-layout enabled was chosen] erickhill OmniPlan General 2 2012-06-27 03:02 PM
Problems with shapes and text invictus26 OmniGraffle General 1 2012-05-22 07:29 AM
Shapes with different line styles per side kelvSYC OmniGraffle General 0 2010-07-06 01:18 PM
Outliner line numbers in shapes? gandhi OmniGraffle General 0 2008-10-22 10:48 PM
Applescript first steps. Joining shapes with a curvy line ivanrostas OmniGraffle General 0 2007-10-31 04:08 AM


All times are GMT -8. The time now is 07:23 AM.


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