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 "Finder"
say "Folder exists is " & (exists folder (my holding_folder)) as text
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 originalName to name of theAttachment
set x to 0
set addOnText to ""
#
# This 'repeat to set new Attachment name' is necessary because 'Mail' messages can
# contain more than one Attachment with same name.
#
set theNameStart to characters 1 through ((offset of "." in originalName) - 1) of originalName as rich text
try
set theNameExtension to characters (offset of "." in originalName) through -1 of originalName as rich text
on error
set theNameExtension to "" # Folder
end try
repeat
set savedAttachmentPath to (my holding_folder & ":" & theNameStart & addOnText & theNameExtension) as rich text
tell application "Finder"
set existsFile to exists (item savedAttachmentPath)
end tell
if existsFile then
set x to x + 1
set addOnText to (" " & x) as rich text
else
#
# Use 'with replacing' to make absolutely sure of saving
#
try
save theAttachment in file savedAttachmentPath with replacing
on error errmsg number errnum
if runForOz then display dialog "Attachment saving error " & errmsg & " number = " & errnum
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 exitFlag to (exists item savedAttachmentPath)
exit repeat
end try
tell current application to delay 0.1
end repeat
if exitFlag then
say "Existing"
exit repeat
end if
end tell
end if
end repeat
end repeat
end tell