Scripting Mail.app

I am trying to access Mail.app in an AppleScript:


tell application "Mail"
	repeat with myMsg in selection
		set mySubject to the subject of myMsg -- this is the line with the error
	end repeat
end tell

But even this does not work, I get an “NSCannotCreateScriptCommandError” in line 3. I have no idea what I am doing wrong.

Hi,

Try using a real list of the selected messages by using a variable. For example:

tell application “Mail”
set the_sel to (selection)
repeat with this_sel in the_sel
set this_subject to (subject of this_sel)
display dialog this_subject
end repeat
end tell

gl,

This works, thanks a lot. I wonder why this is so.

And do I have to do this for all apps I want to script or is this just a problem with Mail.app?

Hi DerGraf,

If there is a ‘selection’ property for a certain application, then it’s usually a threaded list (each item is seen individually). After you set a variable to it, it becomes a real list. Sometimes you may want to use the threaded list. For instance, you may want a command to work on many items and the command doesn’t work on lists.

I’m not sure if this is what is happening in Mail. Sometimes it could be that you just need parenthesis. For repeat loops I ususally set a variable first.

gl,