AppleScript Error -1728 Can't get file...

I have tried to customize the AppleScript template “Mail Rule Action” for my purposes. I want to extract the attachments from the e-mail that match a rule.


To do this, I have set up a rule in the Mail program which then calls the following script.


using terms from application "Mail"
	on perform mail action with messages these_messages for rule this_rule
		tell application "Mail"
			set the message_count to the count of these_messages
			repeat with i from 1 to the message_count
				set this_message to item i of these_messages
				
				set attachment_folder_path to (path to downloads folder) as string
				
				tell this_message
					try
						repeat with att in this_message's mail attachments
							set org_name to the name of att
							save att in file (attachment_folder_path & org_name)
						end repeat
					on error error_message number errNum
						display dialog "Some error: " & errNum & return & error_message
					end try
				end tell
			end repeat
		end tell
	end perform mail action with messages
end using terms from


Unfortunately, I always get the following error message.

Can’t get file "Macintosh HD:Users:testuser:Downloads:test_file.pdf"

The error number is -1728.


How can I solve the problem?

Posted on May 24, 2024 7:15 AM

Reply
Question marked as Top-ranking reply

Posted on May 24, 2024 11:54 AM

Because the example template is wrong by using file and no one responsible for maintaining these templates has caught the error. Remove file from the template's save line and it too works. Different ways to skin a cat…


Probably the person that wrote those all capitals comments was fired too… 🧐

Similar questions

7 replies

May 24, 2024 7:51 AM in response to Sigemær

It might be...


• error -1728 errAENoSuchObject: e.g.: specifier asked for the 3rd but there are only 2. Basically this indicates a run-time resolution error.


Analysis

This can happen when working on a Mac and the application in which the file is opened does not have a working AppleScript interface for editing the file.

Example: When the image is opened in Preview, the error is shown.

https://helpcenter.woodwing.com/hc/en-us/articles/204807329-AppleScript-Error-1728-appears-in-InDesign-when-opening-an-image-or-Excel-file-for-editing#:~:text=This%20can%20happen%20when%20working,Preview%2C%20the%20error%20is%20shown.


https://www.macscripter.net/t/file-reference-error-1728/68190

May 24, 2024 10:47 AM in response to VikingOSX

That's it, I removed the file reference and now it's working. Thanks. :)


But I'm wondering why it was in the example template

				(* EXAMPLE: save images attached to the message, to the pictures folder
				-- PROCESS IMAGE ATTACHMENTS
				tell this_message
					set these_attachments to every mail attachment
					set the pictures_folder_path to (path to pictures folder)
					repeat with z from 1 to the count of these_attachments
						set this_attachment to item z of these_attachments
						-- LOOK FOR IMAGE ATTACHMENTS
						if the MIME type of this_attachment begins with "image" then
							set this_name to the name of this_attachment
							-- DOWNLOAD AND SAVE IMAGE FILE
							with timeout of 1800 seconds
								save this_attachment in file ((pictures_folder_path as string) & this_name)
							end timeout
						end if
					end repeat
				end tell
				*)

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

AppleScript Error -1728 Can't get file...

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