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 > OmniFocus > OmniFocus Extras
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
AppleScript question Thread Tools Search this Thread Display Modes
AppleScript has many nuances that even the most experienced scripter can be caught out by using one term incorrectly. I've been scripting for over 10 years and it can be still be trial and error (or reading a lot of other people's scripts with an application I haven't scripted before). However, if you can get past many of its nuances, AppleScript is a very powerful language. OmniGroup's AppleScript support of their apps is excellent too.

You need to capture the project that the items in the inbox are going to be assigned to first. Following your example, you have a folder in your library called "Work", with a sub-folder called "Operations & Finance" and a project in that folder called "AP, AR, Finance"

In order to capture the project, you must target the OmniFocus document, not its contents, which is how the context is captured at the beginning of the script too. So long as the hierarchy of this folder structure is going to remain the same this can be hard-coded into the script, otherwise you will have to follow whpalmer4's advice and step through the contents of the library looking for a matching name (unfortunately, I don't have time to look at scripting that). To hard-code the hierarchy structure into the script add the following line of script below the "set theContext to..." line:

set theProject to the project "AP, AR, Finance" of folder "Operations & Finance" of folder "Work"

Next, you need to assign the project to the selected Inbox item later in the script. Replace the following line of code:

set name of parent task of anItem to "AP, AR, Finance"

with this line of code:

set assigned container of anItem of theSelectedItems to theProject

That will get what you want to achieve. I've included the entire script below:

Code:
tell application "OmniFocus"
	tell front document
		set theContext to the context "Online" of context "Mac"
		set theProject to the project "AP, AR, Finance" of folder "Operations & Finance" of folder "Work"
		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 assigned container of anItem of theSelectedItems to theProject
				set context of anItem to theContext
				set due date of anItem to current date
			end repeat
		end tell
	end tell
end tell
 
You might find it useful to look at the sample "Send to OmniFocus" script which is bundled inside the app itself (in OmniFocus.app/Contents/Resources/SampleScripts/Send to OmniFocus.applescript).

In particular, it shows how you can use the complete command to find a project or context using the same mechanism as Quick Entry's completion matching, like so:

Code:
set ProjectString to "AP, AR, Finance"
set ContextString to "Online"

set MyProjectArray to complete ProjectString as project maximum matches 1
try
	set MyProjectID to id of first item of MyProjectArray
	set ThisProject to project id MyProjectID
on error
	set ThisProject to (make project with properties {name:ProjectString})
end try

set MyContextArray to complete ContextString as context maximum matches 1
if ((count of MyContextArray) > 0) then
	set MyContextID to id of first item of MyContextArray
	set ThisContext to context id MyContextID
else
	set ThisContext to (make context with properties {name:ContextString})
end if
Note: I just copied the above code directly from the "Send to OmniFocus" script (other than the initial assignment), without editing. Looking at it now, I'm not sure why the project code uses try / on error to notice when a project is missing, while the context code simply checks the number of returned items. It seems to me that either way should work fine, but the second method seems more straightforward and perhaps a little more efficient.
 
whpalmer4,

I may be using "parent task" incorrectly.

All I'm trying to do is set the project of selected tasks in the inbox to the project named "AP, AR, Finance".

I'm also attempting to set the context to be "Online" and the due date to be "current date". Those are now working - I'm 2/3rds of the way there! (Thanks to great help on this forum.)

I think I misunderstood the concept of parent task - I just want to change the project.
 
Latest iteration of the script is below. I attempted to set the project of the selected tasks analogously to the way I set the context, but I get this error message:

Quote:
OmniFocus got an error: Can’t get project "Operations & Finance" of project "Work" of document 1.
Code:
tell application "OmniFocus"
	tell front document
		set theContext to the context "Online" of context "Mac"
		set theProject to the project "AP, AR, Finance" of project "Operations & Finance" of project "Work"
		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 context of anItem to theContext
				set due date of anItem to current date
				set project of anItem to theProject
			end repeat
		end tell
	end tell
end tell
 
Why have you changed line 4 of the script to "of project" instead of "of folder"? You said that the project lives within 2 folders so using project won't find it.
 
Quote:
Originally Posted by Ken Case View Post
You might find it useful to look at the sample "Send to OmniFocus" script which is bundled inside the app itself (in OmniFocus.app/Contents/Resources/SampleScripts/Send to OmniFocus.applescript).

In particular, it shows how you can use the complete command to find a project or context using the same mechanism as Quick Entry's completion matching, like so:

Code:
set ProjectString to "AP, AR, Finance"
set ContextString to "Online"

set MyProjectArray to complete ProjectString as project maximum matches 1
try
	set MyProjectID to id of first item of MyProjectArray
	set ThisProject to project id MyProjectID
on error
	set ThisProject to (make project with properties {name:ProjectString})
end try

set MyContextArray to complete ContextString as context maximum matches 1
if ((count of MyContextArray) > 0) then
	set MyContextID to id of first item of MyContextArray
	set ThisContext to context id MyContextID
else
	set ThisContext to (make context with properties {name:ContextString})
end if
Note: I just copied the above code directly from the "Send to OmniFocus" script (other than the initial assignment), without editing. Looking at it now, I'm not sure why the project code uses try / on error to notice when a project is missing, while the context code simply checks the number of returned items. It seems to me that either way should work fine, but the second method seems more straightforward and perhaps a little more efficient.
Thanks for the example Ken.
 
OK, I tried both methods, still without success.

First, I renamed the projects to folders, and ran this script:

Code:
tell application "OmniFocus"
	tell front document
		set theContext to the context "Online" of context "Mac"
		set theProject to the project "AP, AR, Finance" of folder "Operations & Finance" of folder "Work"
		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 context of anItem to theContext
				set due date of anItem to current date
				set project of anItem to theProject
			end repeat
		end tell
	end tell
end tell
I got this error:

Quote:
OmniFocus got an error: Can’t set project of inbox task id "lAwmI4fc0Eg" of document id "pDdrIS3xC2J" to project id "p_Kz1sJF5_h" of document id "pDdrIS3xC2J".
The project name and folder names are spelled correctly.

I also tried this, attempting to adopt Ken's code:

Code:
tell application "OmniFocus"
	tell front document
		set theContext to the context "Online" of context "Mac"
		set ProjectString to "AP, AR, Finance"
		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 context of anItem to theContext
				set due date of anItem to current date
				set MyProjectArray to complete project string as project maximum matches 1
				if ((count of MyProjectArray) > 0) then
					set MyProjectID to id of first item of MyProjectArray
					set ThisProject to project id MyProjectID
				else
					set ThisProject to (make project with properties {name:ProjectString})
				end if
			end repeat
		end tell
	end tell
end tell
I get this error:

Quote:
OmniFocus got an error: Can’t get project string of content of document window 1 of document 1.
Feeling like an idiot... But at least OmniFocus *does* correctly apply the changes to context & date.
 
Quote:
OmniFocus got an error: Can’t get project string of content of document window 1 of document 1.
It looks like there's a typo in your adaptation of my suggestion: you want "complete ProjectString as project" not "complete project string as project".
 
Ken,

Thanks. I think I'm getting closer. Here's the code:

Code:
tell application "OmniFocus"
	tell front document
		set theContext to the context "Online" of context "Mac"
		set ProjectString to "AP, AR, Finance"
		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 context of anItem to theContext
				set due date of anItem to current date
				set MyProjectArray to complete ProjectString as project maximum matches 1
				if ((count of MyProjectArray) > 0) then
					set MyProjectID to id of first item of MyProjectArray
					set ThisProject to project id MyProjectID
				else
					set ThisProject to (make project with properties {name:ProjectString})
				end if
			end repeat
		end tell
	end tell
end tell
And here is OmniFocus, back to tell me that things are really not shiny.

Quote:
OmniFocus got an error: "AP, AR, Finance" doesn’t understand the complete message.
 
Quote:
Originally Posted by sputnik View Post
Feeling like an idiot... But at least OmniFocus *does* correctly apply the changes to context & date.
AppleScript can do that to me at times too when you sit staring at the code for too long.

I've replicated your project and folder structure (a project called AP, AR, Finance in a folder called Operations & Finance in a folder called Work) in my OmniFocus document and created a sub-context in my Mac context called Online. The following script is working fine for me without any errors. I can select a single item or multiple items in my inbox and they all receive the project, context and due date correctly:

Code:
tell application "OmniFocus"
	tell front document
		set theContext to the context "Online" of context "Mac"
		set theProject to the project "AP, AR, Finance" of folder "Operations & Finance" of folder "Work"
		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 assigned container of anItem to theProject
				set context of anItem to theContext
				set due date of anItem to current date
			end repeat
		end tell
	end tell
end tell
One thing to make sure of is to create a new script to paste the code into otherwise the code from the previous script may be cached until you save the script.

Hopefully this fixes it for you.
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Email/iOS/Mailtags Question... more GTD question more than OmniFocus filmgeek Applying OmniFocus 3 2010-11-06 06:00 AM
Applescript question for Omni Group pvonk OmniFocus 1 for Mac 3 2009-10-06 09:16 AM
Dead stoopid AppleScript question: finding the tell target jporten OmniFocus Extras 1 2009-03-19 10:10 AM
Omniweb Applescript Question russophile OmniWeb General 4 2008-03-14 12:56 PM


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


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