I have something I want to script, maybe you can help me?
A want to automate adding a new to-do task to OmniFocus telling me to pickup a library book with the title of the book included from the email notification.
Here is what I want to do:
I want an Applescript to act on a Mail.app rule that watches for specific messages. I only need help with the Applescript. The script should search the content of the mail message matching the mail rule for the following:
Title: Example Book Title
It should then replace “Title:” with “Pickup from library:” and copy the whole thing “Pickup from library: Example Book Title” into the clipboard, activate OmniFocus, and finally create a new task from the text in the clipboard and save the task.
There exists such a book, it is named Applescript the Missing Manual written by Adam Goldstein It is fun with lots of examples to play with!
I don’t use Mail.app for the moment, so I can’t help you very much, but maybe this is usable for someone to finish!
using terms from application "Mail"
set cntnts to contents of the message
set ttle to my keepLinesFromText(cntnts, "Title")
set hdr to swapText(ttle, "Title", "Pick up from Library")
set the cliboard to hdr
end using terms from
tell application "OmniFocus" to activate
-- we do the key combination that leo uses to make a new task!
-- We paste the stuff in still using system events
-- and uses the key combination for saving the task! shift - enter of whatever!
to swapText(theText, swapOut, swapIn)
(* This bit comes from Apple's own "Replace Text in Item Names" script
with some of the variables changed to make it a call-able handler *)
if the theText contains the swapOut then
-- replace target string using delimiters
set AppleScript's text item delimiters to the swapOut
set the text_item_list to every text item of theText
set AppleScript's text item delimiters to the swapIn
set the theText to the text_item_list as string
set AppleScript's text item delimiters to ""
end if
return theText
end swapText
on keepLinesFromText(theText, keepPhrase)
-- http://macscripter.net/viewtopic.php?id=37830 McUsr
-- Concept by NG
set newText to {}
-- Get the text items of the text using the keepPhrase as a delimiter.
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to keepPhrase
set textItems to theText's text items
set textItemCount to (count textItems)
if (textItemCount > 1) then
-- The phrase was in the text. Collect the text items except for the bits of the paragraphs it was in.
tell beginning of textItems to set end of newText to paragraph -1
repeat with i from 2 to textItemCount - 1
tell item i of textItems to if ((count each paragraph) > 1) then
set end of newText to paragraph 1 & return & paragraph -1
end if
end repeat
tell end of textItems to set end of newText to paragraph 1
set newText to newText as text
else
-- The phrase is not in the text.
set newText to theText
end if
set AppleScript's text item delimiters to astid
return newText
end keepLinesFromText
Here’s what I have so far, it’s not working though:
using terms from application "Mail"
set cntnts to contents of the message
set ttle to my keepLinesFromText(cntnts, "Title")
set hdr to swapText(ttle, "Title", "Pick up from Library")
set the cliboard to hdr
end using terms from
tell application "Reminders" to activate
tell application "System Events"
keystroke "n" using command down
keystroke "v" using command down
keystroke return
end tell
to swapText(theText, swapOut, swapIn)
(* This bit comes from Apple's own "Replace Text in Item Names" script
with some of the variables changed to make it a call-able handler *)
if the theText contains the swapOut then
-- replace target string using delimiters
set AppleScript's text item delimiters to the swapOut
set the text_item_list to every text item of theText
set AppleScript's text item delimiters to the swapIn
set the theText to the text_item_list as string
set AppleScript's text item delimiters to ""
end if
return theText
end swapText
on keepLinesFromText(theText, keepPhrase)
-- http://macscripter.net/viewtopic.php?id=37830 McUsr
-- Concept by NG
set newText to {}
-- Get the text items of the text using the keepPhrase as a delimiter.
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to keepPhrase
set textItems to theText's text items
set textItemCount to (count textItems)
if (textItemCount > 1) then
-- The phrase was in the text. Collect the text items except for the bits of the paragraphs it was in.
tell beginning of textItems to set end of newText to paragraph -1
repeat with i from 2 to textItemCount - 1
tell item i of textItems to if ((count each paragraph) > 1) then
set end of newText to paragraph 1 & return & paragraph -1
end if
end repeat
tell end of textItems to set end of newText to paragraph 1
set newText to newText as text
else
-- The phrase is not in the text.
set newText to theText
end if
set AppleScript's text item delimiters to astid
return newText
end keepLinesFromText
You should search for mailrules and messages here, so you learn how to address a message. But you do good so far!
When you have something that can be triggered, by your mail rule, you can use this handler to
see what is going on, and how your script progresses, it takes a message, and a name of a log file, which is dumped into yourhomedir:Library:Logfiles, and which you can watch using the console.app.