You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

AppleScript to copy email attachment failing - HELP

I found what appears to be an outdated solution to this question here.


I am having no luck with it on Catalina. At first I thought the issue was new Apple security protocols, but I see nothing in that panel suggesting that's the issue. I am unsure how to log this for clues, since it's being run from Apple Mail and not Script Editor. ( that might reveal the cause of the failure)


using terms from application "Mail"
	on perform mail action with messages theMessages
		set attachmentsFolder to "Volumes:Catalina:Users:john:Documents:Attachments:" as rich text
		--remove the angle brackets, but keep the colons, including the last one
		
		tell application "Mail"
			set selectedMessages to theMessages
			try
				repeat with theMessage in selectedMessages
					
					repeat with theAttachment in theMessage's mail attachments
						set originalName to name of theAttachment
						set savePath to attachmentsFolder & originalName
						save theAttachment in file savePath
					end repeat
				end repeat
			end try
		end tell
	end perform mail action with messages
end using terms from


My Mail rule is setup correctly to isolate the correct email. I tested it by changing the Action from "Run AppleScript" to "Set Color of background Green"


I thought that permissions might be an issue so I set them too:


01/05/22_19:22:31 /Users/john
0-zsh% chmod 755 /Users/john/AppleScripts/Wildcat_Weekly.scpt 

01/05/22_19:22:44 /Users/john
0-zsh% ls -la /Users/john/AppleScripts/Wildcat_Weekly.scpt 
-rwxr-xr-x@ 1 john  staff  3478 Jan  5 19:19 /Users/john/AppleScripts/Wildcat_Weekly.scpt


Regarding the Security & Privacy pane. I believe there is supposed to be a warning dialog box when an application or process is trying to gain access - that is not appearing.


In the Menu bar a progress cog does pop up briefly when the action is run.



iMac 27″, macOS 10.15

Posted on Jan 5, 2022 5:33 PM

Reply
Question marked as Top-ranking reply

Posted on Jan 11, 2022 12:35 PM

I was able to get this resolved with existing scripts at https://macscripter.net/viewtopic.php?pid=209404#p209404


I suspected the issue was related to Security Preferences and while running this script it triggered a Security Allow window... Finally!

I'll post the script here for posterity:




Similar questions

11 replies
Question marked as Top-ranking reply

Jan 11, 2022 12:35 PM in response to johnnyjackhammer

I was able to get this resolved with existing scripts at https://macscripter.net/viewtopic.php?pid=209404#p209404


I suspected the issue was related to Security Preferences and while running this script it triggered a Security Allow window... Finally!

I'll post the script here for posterity:




Jan 8, 2022 8:14 PM in response to rccharles

@rccharles,


Thanks for sharing that script! I can't pretend to understand most of it and therefore can't decipher the error.


The error looks like it's towards the end of the script.




The second time I attempt to run the script I receive a script Syntax error. "Expected “,” but found identifier." With the word "rule" highlighted.


on debug(theMessage)
	-- return
	global debugRunning
	local theSize, startupDiskName, pathToLog, fileReference
	
	set pathToLog to (path to home folder rule type rich text) & "tryAttachmentsLog.txt"
	-- log "pathToLog is " & pathToLog
	-- display dialog "pathToLog is " & pathToLog giving up after 4


I then closed it and attempted to reopen and cannot.



01/08/22_22:12:28 /Users/john/Library/Application Scripts/com.apple.mail
0-zsh% ls -la
total 56
drwxr-xr-x    5 john  staff    160 Jan  8 22:09 .
drwxr-xr-x  554 john  staff  17728 Jan  1 13:52 ..
-rw-r--r--@   1 john  staff   6148 Jan  5 20:00 .DS_Store
-rwxr-xr-x@   1 john  staff    526 Jan  8 22:05 ShortMailDownload.scpt

Jan 7, 2022 11:56 PM in response to johnnyjackhammer

This script shows how to dummy up the mail script to run in the script editor. You will need to uncomment some code.

Start apple mail.

Click on some item in the inbox.


run script in the script editor.

Also, the script includes lines to write out debug information to a file. see my debugLog(startMsg).



Jan 9, 2022 4:29 PM in response to johnnyjackhammer

1) my intent isn't to get my script working on your machine, but for you to get better debug information on your machine.

1a) You can modify your script to get it to run on your machine

1b) add logging statements to your script to write to a file.


I think you should modify your script to run on your machine.

I gave a quick overview of what you need to do in front of my script. I'll see if I can write this up in a better format later.


2) Running apple mail script is like threading a thread through a needle. You need to understand poorly written documentation and work around bugs.


3) I'm worried about this message in particular 'removed':

(*debugLog: Created final destination subfolder Catalina:Users:john:Students:'removed' via TeamUnify <notifications+isnasa@teamunify.com>*)


Is there a way of turning off TeamUnify for a test? Might not get it all. Could you try safeboot and run the script in safemode?

Try a safe boot.

   Shutdown your machine. Hold down the shift key. Poweron. The boot up will take longer than normal because the filesystem on the startup drive is being checked and repaired as needed.  All about safe mode including what features and apps safe boot leaves out.  Safe boot uses a software driver instead of using your machines video hardware.

    http://support.apple.com/kb/ht1455


I'm thinking that TeamUnify includes some system extension. SafeBoot doesn't load extensions.


This could be a wild goose chase, you could wait a day and see if I figure out anything more today.


Robert

Jan 8, 2022 8:30 PM in response to johnnyjackhammer

OK, Deleted that script, recreated it, and ran it again, and this time I finally got Security Warnings, whereas last time I did not. I allowed. Still receiving the same error in regards to "type alias".


(this line in your script explains what may be going on with my previous script: mail requires download to the Downloads or subfolders of Downloads)





Jan 6, 2022 12:11 PM in response to johnnyjackhammer

It's hard to debug mail action scripts since errors aren't obviously presented. They *should* (haven't checked recently) be logged in one of the system logs, so that might be worth a look.


As it stands, the flow seems reasonable, but there are a couple of places where it could fail:


> set attachmentsFolder to "Volumes:Catalina:Users:john:Documents:Attachments:" as rich text


if this folder doesn't exist, your script will fail.


Generally try to avoid absolute paths like this. Use the built-in command path to to find things like this. For example this could be rewritten as :


set attachmentsFolder to (path to documents folder) & "Attachments:" as text


(FWIW, your path would not be correct unless your main drive is called 'Volumes'. The 'volumes' component is really only part of the POSIX path definition.)


						save theAttachment in file savePath


This will also fail if there is a filename conflict (e.g. a file already exists in the destination).

Jan 6, 2022 2:38 PM in response to Camelot

Thanks for the suggestion. I did as you say, and still no luck.


I am beginning to think more and more this is a security issue - that the script is being blocked.


There is an Apple CLI utility called tccutil...


I went through the process of resetting security settings but hit a snag with an odd error and decided to just reset everything, restart. Will update on whether this achieves anything.



01/06/22_16:01:43 /Users/john
64-zsh% csrutil status
System Integrity Protection status: disabled.

01/06/22_16:01:50 /Users/john
0-zsh% login root
Password:
iMac191:~ root# sudo tccutil --list
/Library/vnc/vncagent
/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/AE.framework/Versions/A/Support/AEServer
com.apple.ScriptEditor2
com.getdropbox.dropbox
com.google.drivefs

01/06/22_16:11:22 /Users/john
lsappinfo list | grep Script
393) "Script Editor" ASN:0x0-0x1af1af: 
    bundleID="com.apple.ScriptEditor2"
    bundle path="/System/Applications/Utilities/Script Editor.app"
    executable path="/System/Applications/Utilities/Script Editor.app/Contents/MacOS/Script Editor"
394) "com.apple.appkit.xpc.openAndSavePanelService (Script Editor)" ASN:0x0-0x1b01b0: 
395) "QuickLookUIService (com.apple.appkit.xpc.openAndSavePanelService (Script Editor))" ASN:0x0-0x1b11b1: 

01/06/22_16:35:43 /Users/john
0zsh% sudo /usr/bin/tccutil reset All
Successfully reset All

01/06/22_16:37:10 /Users/john
0zsh% sudo /usr/bin/tccutil list
tccutil: Usage: tccutil reset SERVICE [BUNDLE_ID]


Jan 6, 2022 2:59 PM in response to johnnyjackhammer

After a restart still the same issue, must be something in the script.


I tested this and do get a correct path.

set attachmentsFolder to (path to documents folder) & "Attachments:" as text
attachmentsFolder

Result:
"Catalina:Users:john:Documents:Attachments:"


Directory exists in that location:

01/06/22_16:58:05 /Users/john
0-zsh% cd $HOME/Documents/Attachments 

01/06/22_16:58:22 /Users/john/Documents/Attachments
0-zsh% ls -la
total 0
drwxr-xr-x   2 john  staff    64 Jan  5 16:53 .
drwx------@ 39 john  staff  1248 Jan  5 16:53 ..


Jan 10, 2022 10:33 PM in response to rccharles

I do appreciate you taking the time to help me. Thank you again.


It's odd, when I compile I get an error at "set attachmentsFolder to path to rule type string".


When I change it to "as string" the error goes away and it compiles, but "as string" is replaced with "rule type string" ! Compiling it again I get the previous error in an endless loop.


Go figure 😜


I edited it to read:

set attachmentsFolder to "Catalina:Users:john:Documents:"


But I am getting an error. I did a cursory search for that error and error number and came up with nothing.


(*runnning...*)
(*class of theSelectedMessages is list*)
(*count of theSelectedMessages is 1*)
(*Welcome to perform mail action*)
(*attachmentsFolder is Catalina:Users:john:Documents:*)
(*attachmentsFolder now is Catalina:Users:john:Documents:Attachments:*)
(*on error  errMsg is Can’t make «class mssg» id 22590 of «class mbxp» "[Gmail]/All Mail" of «class mact» id "F84893A7-C6E1-4485-AFE0-7BBAD9A67AB6" of application "Mail" into type Unicode text. number is -1700*)
Result:
3



AppleScript to copy email attachment failing - HELP

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.