Applescript help Mail URL

I could use some assistance from the AppleScript experts here.
The script below grabs some info from a selected email in Apple Mail and formats it as a markdown link that I can paste into DT and LogSeq.
It’s great for inserting a link to an email into a note or LogSeq journal to track issues and make it easy to find things.
What I need help with is making it loop over multiple selected emails and append the result of each iteration to the clipboard so I can select multiple emails in a folder and get the individual links and data for each of those emails and paste that result into DT or LogSeq.
I get stuck with how to append something to the clipboard or a variable, or knowing what it the best way to do that.

thanks

EXTRA FUN FACT:
I’ve had good results with, on completed projects, replacing the first part of the url to switch the link from pointing at Apple Mail to pointing at the archived message in DEVONthink. I think that’s all because DT uses the same ID hash for the messages that Apple Mail uses/creates.

on run {input, parameters}
	tell application "Mail"
		set _msg to item 1 of (get selection)
		set _messageURL to "message://%3c" & _msg's message id & "%3e"
		set _messageDateSent to date sent of _msg
		set _messageDateSent to my FormatDateTime(_messageDateSent)
		set _messageDateRec to date received of _msg
		set _messageDateRec to my FormatDateTime(_messageDateRec)
		set _msgSubject to subject of _msg
		set _from to sender of _msg
		set _name to extract name from _from
		set the clipboard to (("[Email:" & _msgSubject & "](" & _messageURL & ")" & return & "**From**: " & _name as string) & return & "**Sent**:" & _messageDateSent & return & "**Received**:" & _messageDateRec)
		-- display dialog (the clipboard)
	end tell
	return input
end run

on FormatDateTime(theDate)
	set theDate to theDate as date
	set D to characters -2 through -1 of ("0" & theDate's day) as text
	copy theDate to tempDate
	set the month of tempDate to January
	set M to characters -2 thru -1 of ("0" & 1 + (theDate - tempDate + 1314864) div 2629728) as text
	set Y to characters -1 thru -4 of ((year of theDate) as text) as text
	set hh to "0" & theDate's hours as text
	set hh to characters ((count of hh) - 1) through (count of hh) of hh
	set mm to "0" & theDate's minutes as text
	set mm to characters ((count of mm) - 1) through (count of mm) of mm
	set ss to "0" & theDate's seconds as text
	set ss to characters ((count of ss) - 1) through (count of ss) of ss
	-- format text as LogSeq format [[Year-Month-day]]
	return ("[[" & Y & "-" & M & "-" & D & "]]" & ", " & hh & ":" & mm & ":" & ss as text)
end FormatDateTime

Replace set _msg to item 1 of (get selection)
with

repeat with _msg in (get selection)
	...
end repeat
1 Like

Thank you!
I had to figure out the way to blank out the clipboard and add each loop’s result to the clipboard but Stack Exchange came through for me there too.
Currently this script’s result is destined for LogSeq type formatting but I’ve been using a simpler variation of the one-email-at-a-time script for DT as well

This is the result. (runs in Shortcuts from Menu Bar)

on run {input, parameters}
	set the clipboard to ""
	tell application "Mail"
		repeat with _msg in (get selection)
			-- set _msg to item 1 of (get selection)
			set _messageURL to "message://%3c" & _msg's message id & "%3e"
			set _messageDateSent to date sent of _msg
			set _messageDateSent to my FormatDateTime(_messageDateSent)
			set _messageDateRec to date received of _msg
			set _messageDateRec to my FormatDateTime(_messageDateRec)
			set _msgSubject to subject of _msg
			set _from to sender of _msg
			set _name to extract name from _from
			set the clipboard to (the clipboard) & return & (("[Email:" & _msgSubject & "](" & _messageURL & ")" & return & "	- " & "From: " & _name as string) & return & "Sent:" & _messageDateSent & return & "Received:" & _messageDateRec) & return & "    -    "
			--display dialog (the clipboard)
		end repeat
		-- display dialog (the clipboard)
	end tell
	return input
end run

on FormatDateTime(theDate)
	set theDate to theDate as date
	set D to characters -2 through -1 of ("0" & theDate's day) as text
	copy theDate to tempDate
	set the month of tempDate to January
	set M to characters -2 thru -1 of ("0" & 1 + (theDate - tempDate + 1314864) div 2629728) as text
	set Y to characters -1 thru -4 of ((year of theDate) as text) as text
	set hh to "0" & theDate's hours as text
	set hh to characters ((count of hh) - 1) through (count of hh) of hh
	set mm to "0" & theDate's minutes as text
	set mm to characters ((count of mm) - 1) through (count of mm) of mm
	set ss to "0" & theDate's seconds as text
	set ss to characters ((count of ss) - 1) through (count of ss) of ss
	-- format text as LogSeq format [[Year-Month-day]]
	return ("[[" & Y & "-" & M & "-" & D & "]]" & ", " & hh & ":" & mm & ":" & ss as text)
end FormatDateTime

Personally, I would use a list and push it to the clipboard at the end…

on run {input, parameters}
	set the clipboard to ""
	tell application "Mail"
		set emailList to {}
		repeat with _msg in (get selection)
			set _messageURL to "message://%3c" & _msg's message id & "%3e"
			set _messageDateSent to date sent of _msg
			set _messageDateSent to my FormatDateTime(_messageDateSent)
			set _messageDateRec to date received of _msg
			set _messageDateRec to my FormatDateTime(_messageDateRec)
			set _msgSubject to subject of _msg
			set _from to sender of _msg
			set _name to extract name from _from
			copy (("[Email:" & _msgSubject & "](" & _messageURL & ")" & return & "	- " & "From: " & _name as string) & return & "Sent:" & _messageDateSent & return & "Received:" & _messageDateRec) & return & "    -    " to end of emailList
		end repeat
		set the clipboard to (emailList as string)
	end tell
	return input
end run

Cool.
Thanks

I think the answer is still no but has anyone figured out how to get the mail message IDs, programmatically, on iOS?
The hack of dragging a message into Notes is OK but it’s a hassle one at a time.

Automation on mobile is still sadly lacking.

1 Like

And it probably will not come. There’s no indication that Apple will port OSA to iOS/iPadOS since it probably goes against their concept of security for the platform.
DTTG could add a JavaScript interface like Drafts has, but that would probably (have to?) be different from the macOS variant. So, no portability of scripts…

1 Like

I rely on applescripts running on my Mac Mini,
and processing flagged emails

On IOS Mail, I flag the email by moving it to a Devonthink Mailbox