An example of creating a script object with
run script and then using a method of that object:
Code:
-- 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 : "
script
on GetProjects(oDoc)
using terms from application \"OmniFocus\"
return flattened projects of oDoc
end using terms from
end GetProjects
end script
"
property pstrOldScript : "
script
on GetProjects(oParent)
using terms from application \"OmniFocus\"
tell oParent
-- ADJUST THE WHERE QUERIES TO MATCH THE PURPOSE
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 {}
else
if build number ≥ "77.57.0.134152" then
set oScript to run script pstrNewScript
else
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