The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   OmniFocus 1 for Mac (http://forums.omnigroup.com/forumdisplay.php?f=38)
-   -   applescript for common OF tasks (http://forums.omnigroup.com/showthread.php?t=23468)

steve 2012-02-11 03:01 PM

applescript for common OF tasks
 
Hi,

I was wondering if there is a way to create a simple apple script for some common tasks that I do in omnifocus.

I would like an applescript that did the following:

- "Clean Up"
- Expanded everything
- Sorted everything projects by Name

In short, can the menu bars items be scripted? Does anyone have samples?

Steve

Brian 2012-02-14 03:46 PM

Here's a "clean up" script:
[CODE]
tell application "OmniFocus"
compact default document
end tell[/CODE]

I believe we support UI scripting via the method discussed on [URL="http://www.macosxautomation.com/applescript/uiscripting/index.html"]this page[/URL], but my brief experimentation failed to yield a working example. This is hopefully a failure of the scripter, and not the app. :-)

Instead, here's one that uses a different approach: it triggers the "Expand all" command by simulating the keyboard shortcut:

[CODE]
tell application "System Events"
tell process "OmniFocus"
keystroke "9" using {command down, control down}
end tell
end tell[/CODE]

The Alphabet-sort of the project list would be a bit trickier, as it's not susceptible to the fake-keyboard-events approach; there's no keyboard shortcut for the "sort by" options in the Edit menu.

Lemme chew on that one for a bit. Possibly including asking one of the engineers about it next time I bump into them in the lunch line. ;-)

Brian 2012-02-14 05:29 PM

Okay, after a bit of experementation, this appears to work, with the caveat that you need to select something in the sidebar manually before running it. Hope this helps!

[CODE]activate application "OmniFocus"
tell application "System Events"
tell process "OmniFocus"
--select all
keystroke "a" using {command down}

-- expand all
tell menu item 1 of menu of menu item 15 of menu 4 of menu bar 1
click
end tell
end tell
end tell[/CODE]

RobTrew 2012-02-15 04:49 AM

Figuring out the menu numbers can, of course, take a bit of time.
Another approach is to generalise a bit and simply write something like:

[B]GetMenuItem("OFOC", {"Edit", "Sort", "By Name"})[/B]

(full code, with reusable functions, below)

[CODE]tell application id "sevs"
set mnuSort to my GetMenuItem("OFOC", {"Edit", "Sort", "By Name"})
tell application id "OFOC" to activate
click mnuSort
end tell



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

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 id "sprf"
activate
set current pane to pane id "com.apple.preference.universalaccess"
end tell
return false
end if
end tell
end GUIEnabled[/CODE]

RobTrew 2012-02-15 05:11 AM

On expansion, the keyboard command works well if you only want to expand whichever panel (sidebar or content) is selected.

You can get a bit more flexibility, if you need it, and combine expand and collapse in a single toggle, with a [URL="http://forums.omnigroup.com/showpost.php?p=78862&postcount=5"]customized expansion script[/URL].


All times are GMT -8. The time now is 12:08 AM.

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