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

Calling the script of another object? Thread Tools Search this Thread Display Modes
Hi -

Is it possible to call handlers in script of another object?

For example, I'm working on a group that contains objects whose scripts run when clicked, and I'd like the scripts of these clicked objects to call handlers held in the script of their parent group object, rather than copy/pasting these handlers into each clicked object.

I see in the dictionary there is a "script" property, but I've tried about every way i can think of to access handlers in another object's script, and I'm coming up empty.

Anyone have any experience with this?
You can get the script of a shape as a string, and evaluate that code string at run-time with run script

tell application id "OGfl"
	tell front canvas of front document
		set shpHasScript to first shape
		run script (script of shpHasScript) as string
	end tell
end tell
If the script returns an object, you should be able to use an idiom like:

Set oScript to (run script strScript)
and then use the methods and properties of that script object.
An example of creating a script object with run script and then using a method of that object:

-- Toggle between a flat and a folder-indented sidebar
-- (An experiment in conditional compilation, as latest Sneaky Peaks 
-- offer quicker (and more rationally ordered) 
-- flattening of the project list)

property pstrNewScript : "
on GetProjects(oDoc)
	using terms from application \"OmniFocus\"
		return flattened projects of oDoc
	end using terms from
end GetProjects
end script

property pstrOldScript : "
on GetProjects(oParent)
	using terms from application \"OmniFocus\"
		tell oParent
			set lstProjects to projects
			set lstFolders to folders
		end tell
		repeat with oFolder in lstFolders
			set lstProjects to lstProjects & my GetProjects(oFolder)
		end repeat
		return lstProjects
	end using terms from
end GetProjects
end script

tell application id "com.omnigroup.OmniFocus"
	set oDoc to default document
	set oWin to front document window of oDoc
	if my IsNarrowed(focus of oWin) then
		set focus of oWin to {}
		if build number ≥ "" then
			set oScript to run script pstrNewScript
			set oScript to run script pstrOldScript
		end if
		set lstProjects to GetProjects(oDoc) of oScript
		set focus of oWin to lstProjects
	end if
end tell

-- Detect whether the sidebar has a narrowed focus
on IsNarrowed(oFocus)
	repeat with oObj in oFocus
		return true
	end repeat
	return false
end IsNarrowed
Thank's Rob -


This works for running the script of the other object. But what I'm hoping to do is call methods within the script of that other object.

OmniGraffle returns scripts as strings, and it sounds like you're saying that I need a script object to call methods it contains.

Am I stuck, or is the a way to create a script object out of a script in a string?
Something like:

set strScript to "script" & linefeed & (script of oShape) as string & linefeed & "end script"

set objScript to run script strScript
You can than call methods from the script object which you have created from the shape's script using the of idiom.

FnSomeHandler() of objScript
PS OGfl shape action code assumes the application context as given, so within your script .... end script bracketing of the source code (for the purposes of run script) you may also need to explicitly add tell application ... end tell bracketing.

(See the OmniFocus example above for the details of escaping the double quotes in the application name within a script string which is to be evaluated at run-time).
Thanks, Rob - that worked like a champ!

Here's the method I rolled your suggestions up into. Note that this is within a shape, so tell application id "OGfl" is implicit.

on getScriptFromObject(_object)
set _textScript to (script of _object) as text
set _scriptObject to "script" & linefeed & _textScript & linefeed & "end script"
return run script _scriptObject
end getScriptFromObject

Example of calling the method foo() in the script of an object referenced by the variable "o"

set oScript to getScriptFromObject(o)
get foo() of oScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

All times are GMT -8. The time now is 10:29 AM.

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