View Single Post
Hi, thanks for the advice Joel,

I am now getting my head around applescript and interpreting application's applescript dictionaries!

After a bit of applescript research and taking apart the address book chart example, I think I nearly have what I need.

There may be an easier or neater way but this works - I noticed the address book example creates a shape then sets an image as a fill and this is the way to 'import' an image rather than use the 'import' command. I use the 'image events' features - http://www.apple.com/applescript/imageevents/ - to get the size of the incoming image, and resize the shape.

One ongoing issue - I can't seem to solve: both the applescript export settings and the omnigraffle export settings for EPS when set to 'transparent background' do not export a transparent background. More accurately, if the imported graphics (a PNG) contains transparency, it is flattened (to white). If I draw a circle overlapping the edge in OG, then export to EPS, the canvas (outside of any shadowed area) is transparent. Maybe this is working as expected. I think I wont apply the drop shadow in the screen-capture application and add it during the automated process...

I post the full applescript code should this help anyone wondering how you can place a graphic in omnigraffle 4.2.3 using applescript and how the export business works.

Code:
-- convert image to EPS and GRAFFLE document applescript droplet
-- assembled by Ian Grant 2008
-- tested with OmniGraffle 4.2.3 and Mac OS 10.5.3
-- currently the script wont repeat over multiple dropped files

global myDocument

-- Some  configurable Settings
property templateName : "A4 with Grid (Metric)" -- Graffle template to use when creating a document - make sure you have it!
property exportFileType : "EPS" -- current workround use FILE extension to save as supported export types
property exportFileExtension : "eps"
property myScale : "1.0" --  1.0 = full size
property myResolution : "1.0" -- The number of pixels per point in the resulting exported image (1.0 for 72 DPI).
property shouldClose : "true"
property defaultName : "myEPSImage"
-- End of Configurable Settings

-- droplet routine
on open of target_files
	
	-- TODO Sanity Check - check all dropped files are images
	
	try
		tell application "OmniGraffle Professional 4"
			--activate
			set myDocument to (make new document at end of documents)
			set template of myDocument to templateName
			set myCanvas to canvas 1 of myDocument
			set originShape to my makeShape(myCanvas, target_files)
		end tell
	end try
	
	-- call the export and save as graffle routine
	my exportImageAsGraffleAndEPS(target_files)
	
end open

-- make the shape - I can't get the import construct to work - so I make a shape and set its fill with the image.
on makeShape(onCanvas, myFile)
	using terms from application "OmniGraffle Professional 4"
		tell onCanvas
			set theShape to make new shape at front of graphics with properties {origin:{0, 0}, thickness:0.0, fill:no fill, draws stroke:false, image scale:1, size:{20, 20}, draws shadow:false}
			my getPicture(theShape, myFile)
			return theShape
		end tell
	end using terms from -- OmniGraffle
end makeShape

-- Get the dropped image, get it's size and set it as a fill to the shape, and some (adjustable) settings.
on getPicture(theShape, myFile)
	set filePath to (myFile) as string
	set posixPath to POSIX path of file filePath
	try
		tell application "Image Events"
			-- start the Image Events application
			launch
			-- open the image file
			set this_image to open filePath
			-- extract the property value
			set the props_rec to the properties of this_image
			copy (dimensions of props_rec) to {myX, myY}
			-- purge the open image data
			close this_image
		end tell
	end try
	
	tell application "OmniGraffle Professional 4"
		ignoring application responses
			set image of theShape to posixPath
			set image sizing of theShape to manual
			set image scale of theShape to 1
			set image offset of theShape to {0, 0}
			set size of theShape to {myX, myY}
			select theShape
		end ignoring
	end tell
end getPicture


-- export EPS and GRAFFLE
on exportImageAsGraffleAndEPS(filename)
	tell application "OmniGraffle Professional 4"
		set myWindow to front window
		set myDoc to document of myWindow
		
		--prompt for save destination
		set export_folder to (choose folder with prompt "Pick the destination folder") as string
		
		--prompt for file name
		set myFileName to text returned of (display dialog "Enter new filename (no extension required)" buttons {"OK"} default button 1 default answer defaultName with icon 2)
		set exportFileName to export_folder & myFileName & "." & exportFileExtension
		set asSourceFileName to export_folder & myFileName & "." & "graffle"
		
		--  SANITY CHECK if chosen filename already exists 	-- TODO check validy of file name
		repeat
			tell application "Finder"
				activate
				if exists file asSourceFileName then
					--asSourceFileName Already exists
					set myFileName to text returned of (display dialog "File already exists. Enter new filename." buttons {"OK"} default button 1 default answer defaultName with icon 2)
					set exportFileName to export_folder & myFileName & "." & exportFileExtension
					set asSourceFileName to export_folder & myFileName & "." & "graffle"
					
				else if exists file exportFileName then
					--exportFileName Already exists
					set myFileName to text returned of (display dialog "File already exists. Enter new filename." buttons {"OK"} default button 1 default answer defaultName with icon 2)
					set exportFileName to export_folder & myFileName & "." & exportFileExtension
					set asSourceFileName to export_folder & myFileName & "." & "graffle"
				else
					exit repeat
				end if
			end tell
			
		end repeat
		
		
		set exportType to exportFileType
		set exportFileExtension to exportFileExtension
		
		set exportFileName to export_folder & myFileName & "." & exportFileExtension
		set asSourceFileName to export_folder & myFileName & "." & "graffle"
		
		
		
		
		-- export settings TODO make more of them configurable?
		set include border of current export settings to false
		set area type of current export settings to selected graphics
		set draws background of current export settings to false
		set export scale of current export settings to myScale
		set resolution of current export settings to myResolution
		
		-- do the export and save
		tell front document
			activate
			-- save export
			save myDoc in exportFileName
			-- safe graffle
			save myDoc in asSourceFileName
			
			-- close doc if configure to 
			if shouldClose is "true" then close
			
		end tell
		
		
	end tell
end exportImageAsGraffleAndEPS
Thanks again! I hope this helps!

Cheers, Ian