Save Mail attachments broken (again)?

Did Catalina break saving Mail attachments via AppleScript again?

Until recently the code below worked:

tell application "Mail"
	set theMessages to the selection
	repeat with aMessage in theMessages
		set attachCount to count of (mail attachments of aMessage)
		if attachCount is not 0 then
			tell aMessage
				repeat with anAttachment in (mail attachments)
					set theName to name of anAttachment
					save anAttachment in file (((path to desktop) as Unicode text) & theName) -- <-- Error occurs here
				end repeat
			end tell
		end if
	end repeat
end tell

Now I get a “Can’t get file [path] of message [details]” error at the indicated line. Well of course you can’t, you haven’t saved it yet, dummy!

Anybody know a workaround?

Model: Mac mini (2018)
AppleScript: 2.4
Browser: Safari 605.1.15
Operating System: macOS 10.14

You may try:

-- better to use “path to” out of the tell application block
-- and done only once
set p2d to path to desktop as text
tell application "Mail"
	set theMessages to the selection
	repeat with aMessage in theMessages
		set attachCount to count of (mail attachments of aMessage)
		if attachCount is not 0 then
			tell aMessage
				repeat with anAttachment in (mail attachments)
					-- check that your system return the name with its extension
					set theName to name of anAttachment --> "EMgKc7xXYAAnjy3.jpg"
					save anAttachment in file (p2d & theName) -->  file "SSD 1000:Users:**********:Desktop:EMgKc7xXYAAnjy3.jpg"
					-- or
					-- save anAttachment in my setfile(p2d & theName)-->  file "SSD 1000:Users:**********:Desktop:EMgKc7xXYAAnjy3.jpg"
				end repeat
			end tell
		end if
	end repeat
end tell

on setfile(apath)
	return apath as «class furl»
end setfile

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 21 janvier 2020 10:53:20

I ran both the OP’s and Yvan’s scripts under Catalina and both threw the error noted by the OP. I decided to simplify the OP’s script in the hope of determining what was happening:

set outputFile to "Macintosh HD:Users:bob:Desktop:attachment.txt"

tell application "Mail"
	set theMessage to the selection
	set theMessage to item 1 of theMessage
	tell theMessage
		set mailAttachment to (mail attachments)
		set mailAttachment to item 1 of mailAttachment
		save mailAttachment in file outputFile
	end tell
end tell

I stepped through the script and everything worked fine until the save line, which caused the error:

The Mail dictionary shows the following, and I can’t see anything wrong in the scripts in this regard:

My test attachment is a text file, so I assume that’s not an issue. I don’t know why this doesn’t work.

Did you ran the version using «class furl» ?
It seems that it’s the only way to get such feature to work, at least with some applications, under Catalina.
My iMac is unable to host this system but I am busy editing all my scripts to use «class furl» in save or write actions.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 21 janvier 2020 17:22:50

I copied and pasted your script into script editor and then ran the script. I didn’t change anything. The script errored at the save anAttachment line with the error message:

I do recall this as an issue, which was discussed in:

https://macscripter.net/viewtopic.php?pid=199787

I’m not certain, butI don’t believe that’s an issue in this case.

Thanks! The «class furl» version works on my machine — although it was a bit confusing at first that you commented out the relevant line when you posted the script! :slight_smile:

I carefully wrote :

save anAttachment in file (p2d & theName) -->  file "SSD 1000:Users:**********:Desktop:EMgKc7xXYAAnjy3.jpg"
-- or
-- save anAttachment in my setfile(p2d & theName)-->  file "SSD 1000:Users:**********:Desktop:EMgKc7xXYAAnjy3.jpg"

It seems that you missed that. Try with :

-- save anAttachment in file (p2d & theName) -->  file "SSD 1000:Users:**********:Desktop:EMgKc7xXYAAnjy3.jpg"
-- or
save anAttachment in my setfile(p2d & theName)-->  file "SSD 1000:Users:**********:Desktop:EMgKc7xXYAAnjy3.jpg"

You may achieve the goal with this code which is simpler:

-- better to use “path to” out of the tell application block
-- and done only once
set p2d to path to desktop as text
tell application "Mail"
	set theMessages to the selection
	repeat with aMessage in theMessages
		set attachCount to count of (mail attachments of aMessage)
		if attachCount is not 0 then
			tell aMessage
				repeat with anAttachment in (mail attachments)
					-- check that your system return the name with its extension
					set theName to name of anAttachment --> "EMgKc7xXYAAnjy3.jpg"
					tell me to set aFile to (p2d & theName) as «class furl» -->  file "SSD 1000:Users:**********:Desktop:EMgKc7xXYAAnjy3.jpg"
					save anAttachment in aFile
				end repeat
			end tell
		end if
	end repeat
end tell

Would be a good idea to look at : https://macscripter.net/viewtopic.php?id=47328

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 21 janvier 2020 17:40:51

Thanks Yvan–I did miss that. I made that change and the script works great.

Just to make sure I understood what was happening, I modified my test script in post 3 to use class furl as shown below and it worked fine.

set outputFile to "Macintosh HD:Users:bob:Desktop:attachment.txt" as «class furl»

tell application "Mail"
	set theMessage to the selection
	set theMessage to item 1 of theMessage
	tell theMessage
		set mailAttachment to (mail attachments)
		set mailAttachment to item 1 of mailAttachment
		save mailAttachment in outputFile
	end tell
end tell

I read that other thread you linked, which I had not read before. Since I’m running Catalina, this is an important issue to keep in mind.

It’s OK because you save only the first attachment.
If you must save several ones, you will have to build the «class furl» objects in a loop and so you would have to use my original handler or use the script posted in message #7.

The first instruction which had to use «class furl» was write in scripts using ASObjC.
Then, since the delivery of Catalina it appears that several applications fail to save objects which aren’t «class furl» ones.
I remember a recent exchange about the behavior of Adobe products on this point.
Alas, my old brain forgot most of its content.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 21 janvier 2020 18:57:08