Using "System Events" keystroke with "Mail"

Hello, friends.

Thanks for being such a great help to applescript beginners like me.

Another situation and question:

I am still working on a script for Mail that allows me to select a message, run the script from the Mail scripts menu, and end up with a task in Entourage that has the subject of the message in the subject line of the task and the content of the message in the notes section of the task. This has been a fun challenge, and I’m getting closer every day.

Here’s the most recent issue: When I try to run this with an html message in Mail, the content of the message turns out as garblygook in Entourage. When I try to run this with a plain text message in Mail, the content comes out just like I want it.

So, my proposed solution is to change the content of the message to plain text before storing it in the variable that will be used in Entourage in the future. However, I Mail won’t let me force the content to string or Unicode text and “message format” or some such property is not available in the dictionary of Mail as far as I can tell.

So, I’m trying to

  1. use System Events to “keystroke” the keyboard shortcut in Mail (command-option-p) that will switch the message to plain text,
  2. store the content in the variable
  3. use System Events to “keystroke” the keyboard shortcut in Mail (command-]) to switch it back to html

Here’s the pertinent part of the script…


tell application "Mail"
	set theMessages to selection
	repeat with eachMessage in theMessages
		tell application "Mail" to activate
		tell application "System Events" to keystroke "p" using {command down, option down}
		set theContent to content of eachMessage
		tell application "System Events" to keystroke "]" using command down
	end repeat
end tell

This works fine when I run it from Script Editor. However, if I save it in the scripts menu for “Mail” and run it from there, it doesn’t work.

Any ideas?

David Wilcox

Model: Powerbook G4
AppleScript: Applescript 1.9.3
Browser: Firefox 1.0
Operating System: Mac OS X (10.3.9)

Your approach certainly seems to be the best solution under the circumstances, David.

Since Mail’s scripts menu seems to have evaporated in Tiger, I can’t test for that - although your script works fine here from (the system-wide) Script Menu.

The problem could be simply down to a bug, in which case there’s probably not much we can do about it. However, I did wonder whether the script’s execution time might be slightly different when run from Mail’s script menu. If so, then it could be worth trying a couple of slight delays - to possibly give the keystrokes more time to ‘register’.

The following suggestion also uses UI scripting to check whether a message contains html - so that the keystrokes are executed only in those instances.

tell application "Mail" to repeat with eachMessage in (get selection)
	activate
	tell application "System Events" to set htmlContent to enabled of menu item "Plain Text Alternative" of menu "Message" of menu item "Message" of menu "View" of menu bar item "View" of menu bar 1 of application process "Mail"
	if htmlContent then
		tell application "System Events"
			keystroke "p" using {command down, option down}
			delay 0.5 (* allow keystroke to take effect? *)
			tell application "Mail" to set theContent to content of eachMessage
			keystroke "]" using command down
			delay 0.5 (* allow keystroke to take effect? *)
		end tell
	else
		set theContent to content of eachMessage
	end if
end repeat

Good luck! :slight_smile: