View Single Post
Thanks everyone for your comments and suggestions. There was no solution that exactly fit my needs so I created my own AppleScript to perform the functionality. In my own case I'm using FastScript (http://www.red-sweater.com/fastscripts/) for a hotkey in Mail. This script works on a single or multiple messages and although this does exactly what I need it to, your own mileage may vary. Please note I have some questions towards the end of this post for Omni and/or the community.

Code:
-- FROM /OmniFocus.app/Contents/PlugIns/BuiltInClippingHandlers.plugin/Contents/Resources/Mail-Leopard.applescript
-- Trims "foo <foo@bar.com>" down to "foo@bar.com"
on trim_address(theAddress)
	try
		set AppleScript's text item delimiters to "<"
		set WithoutPrefix to item 2 of theAddress's text items
		set AppleScript's text item delimiters to ">"
		set MyResult to item 1 of WithoutPrefix's text items
	on error
		set MyResult to theAddress
	end try
	set AppleScript's text item delimiters to {""} --> restore delimiters to default value
	return MyResult
end trim_address

-- BASE SCRIPT FROM http://forums.omnigroup.com/showthread.php?t=6182
set messageCount to 0
tell application "Mail"
	-- get the currently selected message or messages
	set selectedMessages to selection
	
	-- if there are no messages selected, do nothing
	if selectedMessages is not {} then
		set messageCount to count of selectedMessages
		repeat with i from 1 to messageCount
			-- parse the email(s)
			set theMessage to item (i) of selectedMessages
			set theName to subject of theMessage
			set theMessageID to (content of header "Message-Id") of theMessage
			set theMessageID to my trim_address(theMessageID)
			set theMessageURL to ("message:<" & theMessageID & ">")
			
			-- create new task in quick entry
			tell application "OmniFocus"
				activate
				set theTask to theName
				set theNote to "Original Message"
				tell quick entry
					set NewTask to make new inbox task with properties {name:theTask, note:theNote}
					set value of attribute "link" of style of note of NewTask to theMessageURL
				end tell
			end tell
		end repeat
	end if
end tell

-- Method 1: Use native AppleScript
-- THIS IS SUPPOSSED TO WORK, BUT FAILS
-- tell applicatation "OmniFocus"
--	activate quick entry
-- end tell

-- Method 2: Use your native hotkey
-- You will need to adjust the keystroke to
-- what you have defined in your OF prefs
--tell application "System Events"
--	keystroke space using {control down, option down}
--end tell

-- Method 3: Use the menu via AppleScript GUI Scripting
-- You must have "Enable access for assistive devices"
-- turned on in System Preferences  Universal Access
tell application "System Events"
	tell process "OmniFocus"
		tell menu bar 1
			tell menu bar item "Window"
				tell menu "Window"
					click menu item "Show Quick Entry"
				end tell
			end tell
		end tell
	end tell
end tell

-- SELECT TASK & EXPAND NOTES FOR FINAL MANUAL EDITING
-- Note: Just change the expandNote value to false
-- if you don't care to have the task notes expanded

set expandNote to true
tell application "System Events"
	keystroke tab
	keystroke (key code 53) -- Escape
	
	-- Note expansion code
	if expandNote is true then
		keystroke "'" using {command down}
		keystroke (key code 53) -- Escape
		if messageCount > 1 then
			-- Expand all note items
			repeat with i from 2 to messageCount
				keystroke (key code 125) -- Arrow Down
				keystroke "'" using {command down}
				keystroke (key code 53) -- Escape
			end repeat
			-- Cursor up first item
			repeat with i from 1 to messageCount
				keystroke (key code 126) -- Arrow Up
			end repeat
		end if
	end if
	
	-- Select first task text for manual editing if needed
	keystroke tab
end tell
Okay so my questions, some of which may need to be answered by Omni...
  1. Why does tell applicatation 'OmniFocus' / activate quick entry / end tell fail? Essentially this does nothing even though there is some indication within the community that this works although other folks state it doesn't but should. Is this by chance a bug?
  2. Is there a less hacky way to expand the notes of each of the tasks in the Quick Entry window? I spent a bit of time trying to get/set "note expanded" but it never quite worked even though I could definitely get/set the boolean.
  3. Is there any better way to manipulate the task items in the quick entry? For example you see a bunch of AppleScript GUI code to select the task text of the first task in the quick entry list. I'm hoping there's a cleaner way of doing this.