I’ve carried out further testing of my script, as well as the sample rule actions script.
The sample script (below) runs fine, displaying the dialog.
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
set theText to "This AppleScript is intended to be used as an AppleScript rule action, but is also an example of how to write scripts that act on a selection of messages or mailboxes." & return & return & "To view this script, hold down the option key and select it again from the Scripts menu."
repeat with eachMessage in theMessages
set theSubject to subject of eachMessage
try
-- If this is not being executed as a rule action,
-- getting the name of theRule variable will fail.
set theRuleName to name of theRule
set theText to "The rule named '" & theRuleName & "' matched this message:"
set theText to theText & return & return & "Subject: " & theSubject
display dialog theText
set theText to ""
end try
end repeat
if theText is not equal to "" then
display dialog theText buttons {"OK"} default button 1
end if
end tell
end perform mail action with messages
end using terms from
My script runs cleanly in the editor if I comment out the “using terms” and “on perform” statements. If I activate them, create a rule and send an email to test it, it doesn’t run.
I’ve tried to log any errors, but running from a rule doesn’t appear to write to the event log. Is there anything obvious that is causing it to fail?
using terms from application "Mail"
on perform mail action with messages theMsgs
tell application "Mail"
set charcount_limit to 140
set login to "xxxxx"
set api_key to "xxxxxxxx"
set adURL to "xxxxxxxxxxx"
set adCaption to "Caption:"
set theMsgs to selection
repeat with theMsg in theMsgs
set theMsgContent to the content of theMsg
set theURL to (the offset of adURL in theMsgContent)
set theURL to the Unicode text theURL thru (theURL + 49) of theMsgContent
set theCaption to ((the offset of adCaption in theMsgContent) + 21)
set theCaption to the Unicode text theCaption thru (theCaption + 65) of theMsgContent
end repeat
try
set theURL to my encode_text(theURL, true, false)
set theCaption to my trimWhiteSpace(theCaption)
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ASCII character 10 -- (a line feed)
set theCaption to text item 1 of theCaption -- not text of, text items of
set AppleScript's text item delimiters to tid -- whatever they were before - ALWAYS SET THEM BACK!
set bitly to "curl --stderr /dev/null \"http://api.bit.ly/v3/shorten?format=txt&longUrl=" & theURL & "&login=" & login & "&apiKey=" & api_key & "\""
set bitly to (do shell script bitly)
set tweet to theCaption & " - " & bitly
set charcount_tweet to (count characters of tweet)
if charcount_tweet ≤ charcount_limit then
-- post to twitter
set twitter_status to quoted form of tweet
set twitter_parm to "status=" & twitter_status
try
set tweet_results to do shell script "twurl -d " & twitter_parm & " /1/statuses/update.xml"
end try
end if
end try
end tell
end perform mail action with messages
end using terms from
-- this sub-routine is used to encode text
on encode_text(this_text, encode_URL_A, encode_URL_B)
set the standard_characters to "abcdefghijklmnopqrstuvwxyz0123456789"
set the URL_A_chars to "$+!'/?;&@=#%><{}[]\"~`^\\|*"
set the URL_B_chars to ".-_:"
set the acceptable_characters to the standard_characters
if encode_URL_A is false then set the acceptable_characters to the acceptable_characters & the URL_A_chars
if encode_URL_B is false then set the acceptable_characters to the acceptable_characters & the URL_B_chars
set the encoded_text to ""
repeat with this_char in this_text
if this_char is in the acceptable_characters then
set the encoded_text to (the encoded_text & this_char)
else
set the encoded_text to (the encoded_text & encode_char(this_char)) as string
end if
end repeat
return the encoded_text
end encode_text
on encode_char(this_char)
set the ASCII_num to (the ASCII number this_char)
set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
set x to item ((ASCII_num div 16) + 1) of the hex_list
set y to item ((ASCII_num mod 16) + 1) of the hex_list
return ("%" & x & y) as string
end encode_char
on trimWhiteSpace(aString)
if aString is not "" then
-- setup for no delimiter
set savedTextItemDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to ""
-- start with the tail end by revering the list
set these_items to reverse of (every text item of aString)
-- keep peeling off 1st space
repeat while item 1 of these_items is space
set these_items to rest of these_items
end repeat
-- flip the list, now do the leading characters
set these_items to reverse of these_items
repeat while item 1 of these_items is space
set these_items to rest of these_items
end repeat
-- reconstruct the string
set these_items to these_items as string
-- restore and return
set AppleScript's text item delimiters to savedTextItemDelimiters
return these_items
end if
end trimWhiteSpace