Upgrade 10.8 to 10.10 Yosemite - Apple Mail Script not working

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)

Still stuck on this…

Is the answer that I need to enable accessibility for “osascript” perhaps?

Thanks

After every pair of instructions :

tell application “System Events”
tell process “Mail”

insert :
set frontmost to true

Yvan KOENIG (VALLAURIS, France) dimanche 10 mai 2015 17:34:06

Thanks, Yvan

I will try that next weekend, when I get back to this!

Merci

Andrew

Yvan

The frontmost step has helped. I can see the right clicks happening.

But the selecting of messages does not work, so nothing prints.

The following statement seems to do nothing:

set selected messages of first message viewer to {first message of mailbox "INBOX" of account "Gmail" whose id is messageID}

I have played around with this but cannot get Mail to select messages like it did under Mountain Lion (with the exact syntax as above).

Has something changed with the syntax in this area between ML and Yosemite?

Cheers

Andrew

I have tried this, which runs, but does not in fact select any message…:

tell application "Mail"
	activate
	set unreadGmailMessages to every message of mailbox "INBOX" of account "Gmail" whose read status is false
	--tell front message viewer to set selected messages to unreadGmailMessages
	--set selected messages of first message viewer to {first message of mailbox "INBOX" of account "Gmail"}
	set myMessage to item 1 of unreadGmailMessages
	tell front message viewer to set selected messages to myMessage
	--save myMessage
	delay 5
end tell

OK. I am now completely baffled.

When I run this script…

tell application "Mail"
	activate
	delay 2
	set listofmailboxes to every mailbox of account "Gmail"
	set theM to mailbox "INBOX" of account "Gmail"
	set selected mailboxes of message viewer 1 to theM
	--set sort column of message viewer 1 to date received column
	delay 3
	set selected messages of message viewer 1 to (first message of (theM))
	delay 5
end tell

… in Mail, the inbox greys out and it says “No mailbox selected” at the top. Whatever mailbox was manually selected before just disappears. Of course, no messages are selected either.

The listofmailboxes returns 13 mailboxes including the last - mailbox “INBOX” of account “Gmail”.

What is going on here? I have only only the Gmail account active in Mail. If I tell Mail to select mailbox “INBOX” of account “Gmail”, why is it not doing this?

If I manually select the Mail inbox, all the Gmail messages reappear. I run the script again and Mail reports that “No mailbox selected” and all goes blank!

Sigh!

Andrew

Work around:


tell application "Mail"
	activate
	delay
	set theM to (inbox of message viewer 1) --(mailbox "INBOX" of account "Gmail")
	set selected mailboxes of message viewer 1 to theM
	set sort column of message viewer 1 to date received column
	delay 1 -- important delay or message selection may not happen
	set selected messages of message viewer 1 to (message 5 of (theM))
	delay 1
end tell

:slight_smile:

Hopefully this will allow me to complete the main script…

All back working again.

Must remember not to upgrade OS next time!!!

Thanks Yvan for your help

Andrew

Something puzzle me.

About Mail, I’m aware of the shortcut ⌘0 which is the shortcut for the menu item "Fenêtre de message3
but I don’t know the shortcut ⌘1.

What is it supposed to achieve ?

Yvan KOENIG (VALLAURIS, France) jeudi 14 mai 2015 21:52:35

Hi Yvan

The shortcut ⌘1 on my Mail is set to go to Inbox (under Go to Favourite Mailbox).

Under Mail on ML, that was all I needed. There is now some duplication in the script to get to the Inbox as I script that as well, to be sure to be sure, after the travails below!

Andrew

Here the described item is reached thru ⌥⌘1, not thru ⌘1

You may compare English menu to French one here :
https://app.box.com/s/awugtv3i9mox5db8nulecovsel7inkld

Yvan KOENIG (VALLAURIS, France) Saturday 16 May 2015 11:13:56