GUI Scripting Mail

I am French. You seems a good AppleScripter and well know MAIL.app.

I wrote a script to save mails as RTF (with attachments like pictures or applications from friends calling for help to test).

In mail, it is possible to Save as… (Text, RTF or Raw Text). I try to emulate with GUI scripting and … that works but I have s small problem and nothing appears able to help me. Here you are the point:

=====
tell application “Mail”
activate
end tell
tell application “System Events”
tell process “Mail”
click menu item 5 of menu 3 of menu bar 1
end tell
end tell

delay 15

tell application “Mail”
activate
tell application “System Events”
tell process “Mail”
click menu item 7 of menu 4 of menu bar 1
end tell
end tell
activate
end tell

My problem is to change this delay 15 stupid and heavy by a test on first process end before to launch second process.

First process is "Save as … " and second process is “Delete mail” … but the problem is to control process end (to test if the process is ended).

I knock at all the doors, hoping a reply. Have you some ideas?

Thanks and all the best.

(I am on OS X 10.3.3)

I don’t know if this will help or not but it might be worth a try. Replace your delay with the following code and adjust the delay to a value that is acceptable to you.

set bg_count to 1
tell application "Mail"
	repeat until bg_count is 0
		set bg_count to background activity count
		delay 5
	end repeat
end tell

– Rob

After an email exchange with Jacques, it was determined that the code suggested above didn’t solve the problem. For the sake of others, here’s what works on my system (OS X 10.2.8). Jacques hasn’t replied yet so I don’t know if it works for him.

tell application "System Events"
	set frontmost of process "Mail" to true
	tell process "Mail"
		-- open a "Save Message" dialog
		click menu item 5 of menu 3 of menu bar 1
		
		-- wait for "Save Message" dialog to be dismissed
		repeat
			if exists window "Save Message" then
				delay 2
			else
				exit repeat
			end if
		end repeat
		
		-- proceed
		click menu item 7 of menu 4 of menu bar 1
	end tell
end tell

– Rob

Rob,
I am under X 10.3.3.
The window “Save message” (in french “Enregistrer le message”) is not know by AppleScript. Here is the portion of my script. The window “Enregistrer le message” is ignored. I put beep to test if the loop is used. No, it is not!

[AS …]
tell application “Mail”
activate
tell application “System Events”
tell process “Mail”
click menu item 5 of menu 3 of menu bar 1
end tell
end tell

end tell

– wait for “Save Message” dialog to be dismissed
repeat
if exists window “Enregistrer le message” then
delay 2
beep
else
exit repeat
end if
end repeat

–delay 10

–stop

tell application “Mail”
activate
tell application “System Events”
–set Bouzy to busy indicator
–display dialog Bouzy
tell process “Mail”
click menu item 7 of menu 4 of menu bar 1
end tell
end tell
–activate
–end tell
end tell

[/AS]

How about this?

tell application "System Events"
	set frontmost of process "Mail" to true
	tell process "Mail"
		set winCount to count windows
		click menu item 5 of menu 3 of menu bar 1
		repeat until (count windows) is equal to winCount
			delay 2
		end repeat
		click menu item 7 of menu 4 of menu bar 1
	end tell
end tell

– Rob

Rob,
You are an angel!
Your solution seems perfectly working.
During the day (now I only awake in France, I catch a glimpe on my script (but now it is our script :wink: and tell you after some tests if success is aimed!
Thanks very very much (I am on the problem since a year!) but not the night and sometimes :-)))

Great! I’m glad that we overcame the problem because I was unsure of how to deal with the language issue. :shock:

My initial plan was to count windows but it didn’t work when the Mail application was instructed to do so (Mail didn’t include the save dialog in the window count). The idea of using UI scripting to count the windows didn’t occur to me until later. This means that the script’s benefit is mutual since you appear to have a script that works and I have learned something that might come in handy in other scripts. :slight_smile:

Regards,
Rob

Hi Rob,
My (our) script works only on a selected mail and now, I am trying to use a repeat loop to save each mail of a box with a display dialog to continue or exit the loop.
At the moment, I am able to delete all the mails ;-))) but you know, I try on draft mails I generate!
You know, my AppleScript is exactly so bad my english (very rusty if I can said) :-(.
As an apologize, I may tell that MAIL.app is not really easy to script (for example the save window where it is impossible to write the modified subject that I must modified before asking for apple events to save as …
Fortunalety, exists a kind person named Rob.
Thanks for help
Friendly
Jacques

Thanks for your kind words. :slight_smile:

That’s an understatement! :wink:

Unfortunately, I’m sorry to say that I won’t be able to offer much help. The main reason is that I am still running the Jaguar version of Mail and I’ve heard that there were changes in Mail’s AppleScript terminology when Panther was released. This puts me at an extreme disadvantage when attempting to help someone who runs Panther. I can generally muddle through minor issues, such as the one that we have already addressed, but I can’t go much further than that.

I’m sure that others here, or on Apple’s AppleScript forum, can offer much better advice. Among the participants on both forums is Andreas Amann, developer of Mail Scripts. Andreas is a very talented scripter who is intimately familiar with Mail’s AppleScript implementation.

Good luck, Jacques!

– Rob (a Eudora user)

Hi Rob,
Are you really a MacUser? You use Eudora and Jaguar :wink:
I use Mac OS X 10.3.3 very better and fast than Jaguar or Puma but they charge too much an update each two years.
Be quiet with your help (very significant). Thanks.
I well know Andreas Amann and his scripts so nice.He sent me this suggestion that I have not time to test. But your help was very efficient and I test later Andreas suggestion.
[Script]
tell application “Mail”
activate
end tell
tell application “System Events”
tell process “Mail”
click menu item 5 of menu 3 of menu bar 1
– give Mail enough time to open the window
do shell script “sleep 1”
– wait until the “Save Message” window is no longer showing
tell window “Save Message”
repeat while exists button “Save”
do shell script “sleep 5”
end repeat
end tell
end tell
end tell
[/Script]