Hi
I have upgraded from 10.8 to 10.10 and have an Apple Mail script, which used to run very reliably. It now completely fails. The script is designed to take emails from a particular sender, as they arrive, and print them off as a PDF to a folder in a particular format.
Under Yosemite, it just does not work. I have enabled assistive access for Mail (as GUI stuff is needed) and updated the GUI clicks etc to what I think they should be (there are some minor changes in the updated Mail app).
No joy. No GUI.
It gets to the end of the script though where it sends me an email to say it has printed/saved the PDF (it hasn’t).
I saved the script in debug mode under Script Debugger to see what was going on. The GUI part does not work. But I wanted to check if it was picking up the right message(s). However, when the script is run by the Mail rule, I get an appleScript execution error under Script Debugger saying “osascript got an error: Can’t get rule…”. The script breaks out - it will not even run. Weird.
Anyway, here is the script. Perhaps there is some fundamental code/point I am missing which has changed between Mountain Lion and Yosemite, which someone can guide me to? Or just suggestions how to debug a Mail script (which you cannot run direct from script editors)…
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
-- The filename will be the email date sent & prefix & email time sent & suffix
set prefix to " Receipt "
set suffix to " (SNCF)"
set theToRecipient to "xxxxxx@gmail.com" -- confirmation will be sent here when email printed
tell application "Mail"
activate
delay 1
tell application "System Events"
tell process "Mail"
keystroke "0" using command down
delay 1
keystroke "1" using command down
end tell
end tell
delay 1
repeat with eachMessage in theMessages
--try
set datesent to date sent of eachMessage
set messageID to id of eachMessage
set datetimetext to my makeStamp(datesent, "-", "|", "_")
set datetext to rich text 1 thru 10 of datetimetext
set timetext to rich text 12 thru 19 of datetimetext
set flname to datetext & prefix & timetext & suffix
set tempclip to the clipboard
set the clipboard to flname
--print eachMessage
delay 1
set selected messages of first message viewer to {first message of mailbox "INBOX" of account "Gmail" whose id is messageID}
--on error e number n
-- display dialog "Error number " & (n as string) & " occured " & return & quote & e & quote
--end try
delay 2
display dialog "Preparing to print email to My Paperwork" giving up after 5
--display dialog (messageID as rich text) & " " & (number of items in theMessages) as rich text --giving up after 5
delay 2
my mailGUI(1)
delay 1
set test to my mailGUI(3)
if test is true then set test to my mailGUI(4)
if test is true then my mailGUI(2)
set the clipboard to tempclip
delay 3
set theSubject to "CONFIRMED: " & the subject of eachMessage
set theContent to "Logged with My Paperwork - CONFIRMED"
set newMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
delay 2
tell newMessage
make new to recipient at end with properties {address:theToRecipient}
delay 2
send newMessage
end tell
delay 1
end repeat
end tell
end perform mail action with messages
end using terms from
on makeStamp(Now, DelimD, DelimT, SepT) -- the first delim for date, the second for time, the third to separate date and time.
tell Now to tell 100000000 + day * 1000000 + (its month) * 10000 + year as string ¬
to set dateStamp to text -4 thru -1 & DelimD & text 4 thru 5 & DelimD & text 2 thru 3
tell Now to tell ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as string) ¬
to set timeStamp to text 2 thru 3 & DelimT & text 4 thru 5 & DelimT & text 6 thru 7
return dateStamp & SepT & timeStamp
end makeStamp
on mailGUI(choice)
tell application "System Events"
tell process "Mail"
if choice is equal to 1 then
click menu item "Print." of menu 1 of menu bar item "File" of menu bar 1
else if choice is equal to 2 then
click menu item "Paste" of menu 1 of menu bar item "Edit" of menu bar 1
delay 2
keystroke return
else if choice is equal to 3 then
repeat with n from 1 to 5
if exists sheet 1 of window 1 then
keystroke "p" using command down
delay 2
return true
end if
delay 1
end repeat
return false
else if choice is equal to 4 then
repeat with n from 1 to 5
if exists sheet 1 of sheet 1 of window 1 then
delay 2
--keystroke "p" using command down
delay 2
return true
end if
delay 2
end repeat
return false
end if
end tell
end tell
end mailGUI
Browser: Safari 600.5.17
Operating System: Mac OS X (10.8)