Why Do I Get This Error From Mail?

I’m getting this strange error no matter what I try. So I simplified my script and still the error. Here’s the simple script:

tell application “Mail”
set theMessage to selection
set theSubject to subject of theMessage
end tell

Instead of ngetting the subject of the selected message, I get this error:

error "Can’t get subject of {message id 74067 of mailbox \"INBOX\" of account id \"6E0B3832-1B5E-4689-9C8A-8D6294A0C4A4\" of application \"Mail\", message id 74072 of mailbox \"INBOX\" of account id \"6E0B3832-1B5E-4689-9C8A-8D6294A0C4A4\" of application \"Mail\"}." number -1728 from «class subj» of {«class mssg» id 74067 of «class mbxp» "INBOX" of «class mact» id "6E0B3832-1B5E-4689-9C8A-8D6294A0C4A4", «class mssg» id 74072 of «class mbxp» "INBOX" of «class mact» id "6E0B3832-1B5E-4689-9C8A-8D6294A0C4A4"}

The selection always produces a list, even when you only have one item selected. Your approach only works for individual items, not for a list.

These should work (but are untested).

You can specify an item from the list like this:

set theSubject to subject of (item 1 of theMessage)

If you want to create a list of subjects from a selection of multiple messages there are a couple of approaches you can take.

You can loop through the list, like so:

	set msgList to selection
	set jectList to {}
	repeat with eMsg in msgList
		set end of jectList to eMsg
	end repeat
	-- the list of subjects
	-- if you won't need anything but the subject, you can create a list of subjects like this:
	set jectList to subject of selection

By the way, the new site formats code differently than the old. To format a block of code, put three backticks (ie ```) alone on the line above and below your code. That’s the key to the left of the 1 and above the tab (at least on a US keyboard).

Thanx… I’ll give that a go.

Concerning the backticks, I’ll try to remember that, but please know that the noobies stuff still says [applescript], etc. Someone (maybe not you) should fix that.

As mentioned before I simplified the script to figure out what I was doing wrong. Both responses were very educational. I didn’t realize that Applescript ‘selection’ always returned a list. Being informed of that fact really turned on the brilliant 25-watt bulb in my feeble brain. Thanx to both of you. I rewrote the script and it works fine providing all the data I need now. However, I do have a couple of additional questions. Here is the script I created:

tell application "Mail"
	set someMessage to selection
	set subjectList to {}   -- Script works fine without this line
	repeat with theMessage in someMessage
		set theSubject to subject of theMessage
		set theBody to content of theMessage
		set end of subjectList to theSubject.  -- Script works fine without this line
	end repeat
end tell

Given that the script functions fine without the two lines indicated, is there some reason, unknown to me, that I should include these two lines? And… How can I add code to ensure only one message is selected?

I am attempting to write something to capture selected messages for adding to Obsidian after the content is reformatted to markdown. That is the reason I was initially attempting to select only one message instead of a list. Of course, I now understand that I have to select a list of one single message.

1 Like

If you will only use the script with a single message then those two lines are unnecessary. If planning on processing multiple messages the same way, then they would be useful.

With any type of ‘ensure only one’ question, you need to decide what happens when more messages are selected. Do you actually want the first selected message to be the ‘one’? Or just throw up an error? Or are there other criteria?

If it’s the first message, then you could just get item 1 of selectedMessages or some variation on that. Then you can forego the repeat loop as well. It might look something like this:

set selectedMessages to selection
set theMessage to item 1 of selectedMessages
set theSubject to subject of theMessage 

Regarding the backticks/[applescript] stuff… it is definitely not me. That said, do you have a link for the page? Maybe we can get somebody to look into it.

Thanx much… The answer makes good sense… At least to me. I feel enlightened.

Concerning the [Applescript] vs ``` thing, here is the link: