Return Subject Mail works with repeat loop not alone

Cannot work out hwat I am doing incorrectly and would apprecite some advice.

This scrip with a repeat loop works.

tell application "Mail"
	set theMsg to selection 
	repeat with theMsg in theMsg
		tell theMsg
			set Subject_ to subject
		end tell
	end repeat
end tell

This does not.

tell application "Mail"
	set theMsg to selection
	tell theMsg
		set Subject_ to subject
	end tell
end tell

The only addition to the first is the loop.

thanks

Peter

Hi,

your syntax to use the same name for an index variable and the list in a repeat loop works, but is not good programming habit.

the result class of selection is always a list.
This syntax makes is clearer


tell application "Mail"
	set selectedMessages to selection
	repeat with aMessage in selectedMessages
		tell aMessage
			set Subject_ to subject
		end tell
	end repeat
end tell

I recommend to use better readable variable names and always the plural form for lists

Hi Stefan

thank you again as I am sure your have gathered I am very new to this and have been putting together bits of scripts I had found on the web. I now understand why my first effort did not work as the selection is a list. If you will allow me to ask a question directly and I apologise if that is bad protocol on this site. Given my intention is to select one message at a time should I not be abel to refer to the one I have selected?

Thanks again

Peter

As selection is always a list even if only one message is selected, use this syntax to refer to a single message


tell application "Mail"
	set selectedMessages to selection
	if selectedMessages is not {} then
		set Subject_ to subject of item 1 of selectedMessages
	end if
end tell

Thanks Stefan thought it was something like that.

Peter