I have a method that worked in Mountain Lion, but refuses to work in Yosemite, and I cannot for the life of me work out why.
The script below sums it up. I get an error -10000, Apple Event handler failed.
If anyone can guide me on what I’m doing wrong, I’d appreciate it, please.
property currentMailItem : {}
property holding_folder : ""
property printDateTimeName : "2014-12-15 102310Z"
set ptd to path to desktop
set temporaryFolderPath to ptd & "Temporary Printing" as text
set my holding_folder to (ptd & "Temporary Printing:" & my printDateTimeName) as text
set runForOz to true
tell application "Finder"
if not (exists folder temporaryFolderPath) then
try
make new folder at folder ptd with properties {name:"Temporary Printing"}
end try
end if
if not (exists folder (my holding_folder)) then
say "making folder"
make new folder at folder temporaryFolderPath with properties {name:my printDateTimeName}
end if
end tell
tell application "Mail"
set my currentMailItem to message 1 of mailbox "* items to store"
repeat with theAttachment in my currentMailItem's mail attachments
set p to 14
set originalName to name of theAttachment
set x to 15
set addOnText to ""
#
# This 'repeat to set new Attachment name' is necessary because 'Mail' messages can
# contain more than one Attachment with same name.
#
repeat
set p to 16
set savedAttachmentPath to (my holding_folder & ":" & addOnText & originalName) as rich text
tell application "Finder"
set p to 17
set existsFile to exists (item savedAttachmentPath)
end tell
set p to 18
if existsFile then
set x to x + 1
set p to 19
set addOnText to (x & " ") as rich text
else
set p to 20
# display dialog savedAttachmentPath as rich text
#
# Use 'with replacing' to make absolutely sure of saving
#
try
tell application "Finder"
say "Folder exists is " & (exists folder (my holding_folder)) as text
end tell
save theAttachment in file savedAttachmentPath with replacing
on error errmsg number errnum
if errnum = -10000 then
try
set POSIXsavedAttachmentPath to POSIX path of savedAttachmentPath as rich text
save theAttachment in file POSIXsavedAttachmentPath with replacing
on error errmsg number errnum
if runForOz then display dialog "Attachment POSIX saving error " & errmsg & " number = " & errnum
end try
else
if runForOz then display dialog "Attachment saving error " & errmsg & " number = " & errnum
end if
end try
tell application "Finder"
set y to 0
repeat
try
set y to y + 1
if y ≥ 40 then
set exitFlag to true
exit repeat
end if
set p to 20.1
set exitFlag to (exists file savedAttachmentPath)
exit repeat
end try
tell current application to delay 0.1
end repeat
if exitFlag then exit repeat
end tell
end if
end repeat
end repeat
end tell