View Single Post
I use the "OmniFocus URI Handler" as a way of getting web pages from Firefox into OmniFocus. Unfortunately, despite the protocol stating it'll accept contexts and projects, it doesn't.

I've been trying to get around that by tacking on a textual tag to the end of the task name, and then I had hoped to create an AppleScript that would assign contexts based on that textual tag.

For example, the OmniFocus URI Handler might write "Task name [@phonecall]", and this AppleScript would take the text tag "@phonecall" in the name and assign the actual context "Phone This" to that.

I was able to splice a few people's scripts together, but it just isn't working, and, moreover, it doesn't seem anywhere near working.

As it stands in its form below, the script's error is that it tells me "can't get context".

Additionally, the three things I'd like the script to do -- that I know that, as currently structured, it can't -- are: (a) be able to go through all tasks currently selected, not just one task at a time; (b) remove the textual tag once the task's been properly assigned; and (c) move the task to one particular folder (named "Miscellaneous") once the proper context is assigned. (Ideally, maybe with certain contexts which always go into a certain different folder, I could figure out how to treat those particular exceptions.)

In any case, if anyone who is knowledgeable about OmniFocus and AppleScript would be of a kind enough mind to offer their help, it would be appreciated. I'm sure such a structure would be useful enough for others to adapt for their own, different purposes.

I know that this is badly structured, messy code, so if you are of a mind to help and want to completely rip this apart, go for it.

Code:
tell application "OmniFocus"
	
	tell front document
		tell document window 1
			set theSelectedItems to selected trees of content
			if ((count of theSelectedItems) < 1) then
				display alert "You must first select a single task." message "Select a single task before running this script." as warning
				return
			end if
			if ((count of theSelectedItems) > 1) then
				display alert "You must select only one task." message "Select a single task before running this script." as warning
				return
			end if
			
		end tell
	end tell
	
	set theDoc to document window 1 of document 1
	
	set theSelectedTask to value of item 1 of theSelectedItems
	if the name of item 1 of theSelectedItems contains "@add2sys" then set context of item 1 of theSelectedItems to context "Add to System" of context "URLs"
	if the name of item 1 of theSelectedItems contains "@blog" then set context of item 1 of theSelectedItems to context "Blog About" of context "URLs"
	if the name of item 1 of theSelectedItems contains "@bm" then set context of item 1 of theSelectedItems to context "Bookmark" of context "URLs"
	if the name of item 1 of theSelectedItems contains "@dl-app" then set context of item 1 of theSelectedItems to context "App" of context "Download" of context "URLs"
	if the name of item 1 of theSelectedItems contains "@job" then set context of item 1 of theSelectedItems to context "Job Listing" of context "URLs"
	if the name of item 1 of theSelectedItems contains "@mail" then set context of item 1 of theSelectedItems to context "Mail to Someone" of context "URLs"
	if the name of item 1 of theSelectedItems contains "@notepad" then set context of item 1 of theSelectedItems to context "Project Notepad" of context "URLs"
	if the name of item 1 of theSelectedItems contains "@optimism" then set context of item 1 of theSelectedItems to context "Optimism" of context "URLs"
	if the name of item 1 of theSelectedItems contains "@parse" then set context of item 1 of theSelectedItems to context "Parse" of context "URLs"
	if the name of item 1 of theSelectedItems contains "@queue" then set context of item 1 of theSelectedItems to context "Queue (Add To)" of context "URLs"
	if the name of item 1 of theSelectedItems contains "@read" then set context of item 1 of theSelectedItems to context "Read This" of context "URLs"
	if the name of item 1 of theSelectedItems contains "@rss" then set context of item 1 of theSelectedItems to context "RSS (Add To)" of context "URLs"
	if the name of item 1 of theSelectedItems contains "@thinkabout" then set context of item 1 of theSelectedItems to context "Think About" of context "URLs"
	if the name of item 1 of theSelectedItems contains "@watch-long" then set context of item 1 of theSelectedItems to context "Long" of context "Watch" of context "URLs"
	if the name of item 1 of theSelectedItems contains "@watch-short" then set context of item 1 of theSelectedItems to context "Short" of context "Watch" of context "URLs"
	
	tell first document window of front document
		set selected view mode identifier to "project"
		tell sidebar
			select inbox
		end tell
	end tell

end tell

Last edited by WCityMike; 2009-10-17 at 05:21 PM..