The Omni Group Forums

The Omni Group Forums (http://forums.omnigroup.com/index.php)
-   OmniFocus Extras (http://forums.omnigroup.com/forumdisplay.php?f=44)
-   -   AppleScript question (http://forums.omnigroup.com/showthread.php?t=13252)

sputnik 2009-07-30 10:51 AM

AppleScript question
 
I'm using Mail rules to turn reports generated by Netsuite into to-dos. Works great - they go into my inbox.

I would like to take this to the next level, and use AppleScript to automatically assign the items that come in to a specific project, context, and set due date to today.

I've tried to find a pre-written AppleScript but have come up dry. Any tips on how to do this? Or where to go to find an AppleScript I can modify for this purpose?

I would be fine with it either happening automatically, when the tasks come into OmniFocus - or manually - I select the tasks and run the script so they all "leave" the inbox and get filed.

Thanks, Dave

curt.clifton 2009-07-30 11:06 AM

Dave,

Have you look at [URL="http://forums.omnigroup.com/forumdisplay.php?f=44"]the OF Extras forum[/URL]? That's the home for Applescripting OmniFocus. You'll find links to a bunch of scripts there. My Complete and Await Reply script (available from [URL="http://www.rose-hulman.edu/~clifton/software.html"]my software page[/URL]) does some of what you're looking for. It mucks with contexts and dates, so it might serve as a starting point.

It will be easier if you just process the tasks in place, then use the Clean Up command to get them out of the inbox. Clean Up can be called from a script.

sputnik 2009-10-21 05:01 AM

I tried this:

tell application "OmniFocus"
tell front document
-- Gets target context
tell content of document window 1 -- (first document window whose index is 1)
set theSelectedItems to value of every selected tree
if ((count of theSelectedItems) < 1) then
display alert "You must first select an item to complete." as warning
return
end if
repeat with anItem in theSelectedItems
set the project of anItem to "AP, AR, Finance"
set name of the context to "Online"
set due date of the task to "today"
end repeat
end tell
end tell
end tell

I just want to set the context, project, and due date of selected items to: "Online", "AP, AR, Finance", and "Today" respectively. I tried a couple of different ways in the script above, but they both failed.

I'm sure this can't be that hard, but my AppleScript skills are missing.

Thanks,

Dave

DamonC 2009-10-21 01:10 PM

I've updated your AppleScript below. I had to capture the context by using the line:

set theContext to the context "Online"

This gets the context's ID and passes it to the selected items in the script.

Next I changed due date from "today" to "current date", AppleScript's term for getting today.

I've also added a test to see if the selected item is a project by trapping for a parent task. I don't know if that's the correct way to test an item for being a project but it's all I could come up with for now.

Anyway, it works fine in my OmniFocus, you may want to do some testing with various projects to make sure.


tell application "OmniFocus"
tell front document
set theContext to the context "Online"

-- Gets target context
tell content of document window 1 -- (first document window whose index is 1)
set theSelectedItems to value of every selected tree
if ((count of theSelectedItems) < 1) then
display alert "You must first select an item to complete." as warning
return
end if
repeat with anItem in theSelectedItems
if parent task of anItem = missing value then
set name of anItem to "AP, AR, Finance"
set context of anItem to theContext
-- set properties of anItem to {context:ActionContext}
else
set context of anItem to theContext
set due date of anItem to current date
end if
end repeat
end tell
end tell
end tell

curt.clifton 2009-10-21 05:36 PM

[QUOTE=DamonC;68627]
I've also added a test to see if the selected item is a project by trapping for a parent task. I don't know if that's the correct way to test an item for being a project but it's all I could come up with for now.[/QUOTE]

To check whether anItem is a project use:
[CODE]
if (class of anItem is project) then
-- do something projecty with anItem
end if
[/CODE]

DamonC 2009-10-22 01:30 AM

Thanks Curt, I haven't had the time to play with the dictionary for OmniFocus yet.

In this case, the script should be as follows:

tell application "OmniFocus"
tell front document
set theContext to the context "Mac"

-- Gets target context
tell content of document window 1 -- (first document window whose index is 1)
set theSelectedItems to value of every selected tree
if ((count of theSelectedItems) < 1) then
display alert "You must first select an item to complete." as warning
return
end if
repeat with anItem in theSelectedItems
if (class of anItem is project) then
set name of anItem to "AP, AR, Finance"
else
set due date of anItem to current date
end if
set context of anItem to theContext
end repeat
end tell
end tell
end tell

sputnik 2009-10-22 06:10 AM

THanks for your help!

[QUOTE=DamonC;68627]I've updated your AppleScript below. I had to

tell application "OmniFocus"
tell front document
set theContext to the context "Online"

end tell[/QUOTE]

When I run the script sample you kindly provided, I get the error:

[QUOTE]OmniFocus got an error: Can’t get context "Online" of document 1.[/QUOTE]

I know I have a context "Online" in that document. More specifically, it is "Mac : Online". I tried that too, but get the same error.

Something I'm doing wrong?

Thank you,

Dave

DamonC 2009-10-22 06:18 AM

Hi Dave,

Your original post stated that you wanted the context to be set to "Online" which is what I put in the script. What you're now saying is that Online is actually a sub-context of Mac, is that right? I also confused the issue by changing the context to "Mac" in the last script I sent as well.

If Online is a sub-context of Mac, just change the third line of the script to:

set theContext to context "Online" of context "Mac"

sputnik 2009-10-22 07:15 AM

DamonC,

Thanks. AppleScript has a kind of biblical cant, doen't it? Just lots of "ofs" instead of "begats"...

I'm tripping up now on setting the project. I tried a couple of different things, and didn't get anything to work. Here's the latest script. (I know that the selected item has no parent task, so I don't need to test for it with this script.) The error is:

[QUOTE]OmniFocus got an error: Can’t set name of missing value to "AP, AR, Finance".[/QUOTE]

tell application "OmniFocus"
tell front document
set theContext to the context "Online" of context "Mac"
tell content of document window 1 -- (first document window whose index is 1)
set theSelectedItems to value of every selected tree
if ((count of theSelectedItems) < 1) then
display alert "You must first select an item to complete." as warning
return
end if
repeat with anItem in theSelectedItems
set name of parent task of anItem to "AP, AR, Finance"
set context of anItem to theContext
set due date of anItem to current date
end repeat
end tell
end tell
end tell

In this case, "AP, AR, Finance" is in the folder "Operations & Finance" which is in the folder "Work" in "Library".

I tried "AP, AR, Finance" of parent task "Operations & Finance" of parent task "Work"... but I get this error:

[QUOTE]Expected end of line but found “"”[/QUOTE]

I'm sorry to be so dense. I'm told that AppleScript is great because it's just like English, only I don't speak that way!

Best,

Dave

whpalmer4 2009-10-22 07:50 PM

A problem with what you have there is that you're trying to set the name of the parent task of an item in the Inbox, and there is no parent task (because it is in the Inbox), and you can't set the name of a non-existent task...

If you ran the script as written on a task that was not a top-level task (in other words, was a task inside a project in the library), you would rename the action group or project that contained the task, but not assign the task to the desired destination.

I believe you need to actually find the destination project by iterating through all of the contents of the library looking for a matching name, then set the parent task of the task in the Inbox to be that destination project, then do a clean up (compact). Regretfully, I can't think of a good example to copy...


All times are GMT -8. The time now is 07:12 PM.

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