View Single Post
In order not to have the script fail silently in fullscreen mode, I've borrowed and modified a function from Doug's iTunes applescripts. Below is the latest complete version of the script I'm using. It now shows an error if run in fullscreen mode.

Hope this is helpful to others.

Code:
-- ZOOM ALL CANVASES IN CURRENT OMNIGRAFFLE DOCUMENT TO FIT IN WINDOW

property pTitle : "Zoom All Canvases to Fit"
property pVer : "0.8"


on run
	-- Make sure "Enable access for assistive devices" is switched on in System Preferences
	if not GUIEnabled() then return
	
	-- Check whether app is running in full screen mode. If yes, display alert and upon dismissal of alert, quit script.
	if isFullScreen("OGfl") then
		delay 0.1
		set opt to (display alert "OmniGraffle Professional is in full screen mode." message "This script cannot run while OmniGraffle Professional is in full screen mode.
		
You can quit and re-launch this script after exiting full screen mode." buttons "OK" default button "OK" as warning giving up after 30)
		tell application id "OGfl" to activate
		return false
	end if
	
	-- Set the menu command to run on each canvas in the document
	set mnuZoomFit to GetMenuItem("OGfl", {"View", "Zoom", "Fit in Window"})
	
	-- Perform menu command on each canvas in the document
	tell application id "OGfl"
		activate
		tell front window
			try
				set oFrontCanvas to its canvas
			on error
				return
			end try
			
			repeat with oCanv in canvases of its document
				set canvas of it to oCanv
				-- ZOOM TO FIT
				tell application id "sevs" to click mnuZoomFit
			end repeat
			set its canvas to oFrontCanvas
		end tell
	end tell
end run

-- Checks whether Accessibility Preferences have been set to "Enable access for assistive devices"
on GUIEnabled()
	tell application id "sevs"
		if UI elements enabled then
			return true
		else
			activate
			display dialog "This script depends on enabling access for assistive devices in system preferences" buttons "OK" default button "OK" with title pTitle & "   " & pVer
			tell application "System Preferences"
				activate
				set current pane to pane id "com.apple.preference.universalaccess"
			end tell
			return false
		end if
	end tell
end GUIEnabled

-- Checks whether an application is in full screen mode in Lion
on isFullScreen(strAppCode)
	try
		tell application id "sevs"
			set lstApps to application processes where its creator type = strAppCode
			if length of lstApps < 1 then return missing value
			tell first item of lstApps
				return (get value of attribute "AXFullScreen" of window 1)
			end tell
		end tell
	on error
		return false
	end try
end isFullScreen

-- Returns a reference to a clickable menu item
-- E.G. set mnuZoomFit to GetMenuItem("OGfl", {"View", "Zoom", "Zoom to Selection"})
on GetMenuItem(strAppCode, lstMenu)
	set lngChain to length of lstMenu
	if lngChain < 2 then return missing value
	
	tell application id "sevs"
		set lstApps to application processes where its creator type = strAppCode
		if length of lstApps < 1 then return missing value
		tell first item of lstApps
			-- Get the top level menu
			set strMenu to item 1 of lstMenu
			set oMenu to menu strMenu of menu bar item strMenu of menu bar 1
			
			-- Travel down through any sub-menus
			repeat with i from 2 to (lngChain - 1)
				set strMenu to item i of lstMenu
				set oMenu to menu strMenu of menu item strMenu of oMenu
			end repeat
			
			-- And return the final menu item
			return menu item (item -1 of lstMenu) of oMenu
		end tell
	end tell
end GetMenuItem