Best way to store the "original filename" of a document?

I took some time and refined the script a bit and like to share this one as well. This one also works on already imported items.

The script

  • adds the name to the aliases
  • adds the sender domain to the name
  • adds the subject to the (finder) comments
  • checks “Exclude from … Wiki Linking”
  • stores the message id (link) in the URL field

Please note: If the corresponding mail is not selected when the script is run it searches all mailboxes for the mail. I don’t know how this behaves with huge mailboxes and I only tested it with Mail 16/macOS 13.7.1 …

Edit: changed “do shell script” to work on macOS 14.7.1

image

on getMessageId(theOriginMessageId)
	tell application "Mail"
		set theSelection to selection
		repeat with theMessage in theSelection
			if id of theMessage as integer is theOriginMessageId as integer then exit repeat
		end repeat
		try
			if id of theMessage as integer is not theOriginMessageId as integer then error
		on error
			repeat with theMessage in (every message of every mailbox of every account whose id is theOriginMessageId)
			end repeat
			try
				if id of theMessage as integer is not theOriginMessageId as integer then error
			on error
				repeat with theMessage in (every message of every mailbox whose id is theOriginMessageId)
				end repeat
			end try
		end try
		return message id of theMessage
	end tell
end getMessageId

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			try
				set {theOriginSenderHandle, theOriginSubject, theOriginMessageId} to paragraphs of (do shell script ¬
					"/bin/bash -c \"mdimport -d3 -t -n '" & path of theRecord & "' | awk '/com.apple.synapse/{next} /\\{/{a=1}/\\}/{print;a=0}a' | tee >(plutil -extract \":PR:kMDItemOriginMessageID\" raw -) >(plutil -extract \":PR:kMDItemOriginSubject\" raw -) >(plutil -extract \":PR:kMDItemOriginSenderHandle\" raw -) >/dev/null\"")
				set theOriginMessageId to theOriginMessageId as integer
				set isAttachment to true
			on error
				set isAttachment to false
			end try
			if isAttachment then
				set theOriginSenderDomain to " @" & (do shell script "echo '" & theOriginSenderHandle & "'| awk -F '@|>' '{print $2}'")
				set theAliases to aliases of theRecord
				set AppleScript's text item delimiters to ";"
				set theAliases to text items of theAliases
				if theAliases does not contain name of theRecord then
					copy name of theRecord to end of theAliases
				end if
				set aliases of theRecord to theAliases as text
				set AppleScript's text item delimiters to ""
				set exclude from Wiki linking of theRecord to true
				if name of theRecord does not end with theOriginSenderDomain then ¬
					set name of theRecord to name of theRecord & theOriginSenderDomain
				if comment of theRecord does not contain theOriginSubject then
					if comment of theRecord = "" then
						set comment of theRecord to "Subject: " & theOriginSubject
					else
						set comment of theRecord to comment of theRecord & return & "Subject: " & theOriginSubject
					end if
				end if
				try
					set URL of theRecord to "message://" & "%3c" & my getMessageId(theOriginMessageId) & "%3e"
				end try
			end if
		end repeat
	end tell
end performSmartRule
1 Like

I see, thanks. But I could instead use as a criterion a check whether the custom field is empty or not, right?

Yes and you can easily test this yourself.