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 > Developer > AppleScripting Omni Apps
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
OmniFocus Quick Entry Applescript Crash Thread Tools Search this Thread Display Modes
Greetings fellow scripters!

I recently authored an Apple Mail Applescript that adds the last sent email message as task to OmniFocus (OF) and sets the task's context to the recipient of the email.

Unfortunately, when using the Quick Entry window, the script crashes OF 1 out of 3 times. This seems to be due to setting the task's "context" property. Which is to say, when using the Quick Entry window, if I forego setting the "context" property, the script does _not_ crash OF. Note that the script runs without a hitch every time when adding a task directly to OF's Inbox.

Here's my script:
Code:
(* Adds last sent email to OF as a task *)
property _account : "Work"
property _useQuickEntry : false
property _prefix : "Follow-up w/"
property _prependSubject : "re:"
property _defaultContext : "Waiting on"

tell application "Mail"
	set _sentMBox to mailbox "Sent" of account _account
	set _msg to first message in _sentMBox
	set _content to content of _msg
	set _subject to subject of _msg
	set _recipients to name of to recipient of _msg
	set _msgID to do shell script "/usr/bin/python -c 'import sys, urllib; print urllib.quote(sys.argv[1])' " & (message id of _msg)
	
	-- See if there is more than one recipient in the 'To' field
	if (count of _recipients) > 1 then
		set _recipientName to (item 1 of _recipients & " " & "and" & " " & ((count of _recipients) - 1) as string) & " " & "more"
	else
		set _recipientName to item 1 of _recipients
	end if
end tell

-- Sanitize the subject line
set _subject to replace_chars(_subject, "Re: ", "")
set _subject to replace_chars(_subject, "re: ", "")
set _subject to replace_chars(_subject, "Fwd: ", "")
set _subject to replace_chars(_subject, "fwd: ", "")

set _taskTitle to _prefix & " " & _recipientName & " " & _prependSubject & " " & _subject
set _taskNote to "Created from message://%3C" & (_msgID) & "%3E"

tell application "OmniFocus"
	tell default document
		-- Find the recipients context
		try
			set _agendaContext to the first flattened context where its name contains (item 1 of _recipients)
		on error _error
			set _agendaContext to the first flattened context where its name contains _defaultContext
		end try
		
		-- Set Properties
		set _taskProps to {name:_taskTitle, note:_taskNote}
		
		if _useQuickEntry then
			tell quick entry
				open
				
				-- I would like to also set the context here, but 1 out of 3 times this call crashes OF
                                if _agendaContext is not missing value then set _taskProps to _taskProps & {context:_agendaContext}
				set _task to make new inbox task with properties _taskProps
				
				select {_task}
				activate
				tell application "System Events" to keystroke tab
			end tell
		else
			if _agendaContext is not missing value then set _taskProps to _taskProps & {context:_agendaContext}
			set _task to make new inbox task with properties _taskProps
		end if
	end tell
end tell

-- Text Replacement routing
on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars
Any help you could provide would be greatly appreciated!
 
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes



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


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