So I’ve managed to build a script which will print email and have moved on to trying to get attachments, if any, saved in a particular directory. My intention is to use this a script attached to a rule in the OS X Mail.app.
I was wondering if anyone could analyze the portion of the script below “–this does not–” and provide some feedback as to why it does not work? I don’t want to overwrite attachments with the same name that already exist in the target directory
Thanks!.
using terms from application "Mail"
on perform mail action with messages theMsges
tell application "Mail"
try
repeat with ThisMessage in theMsges
-- begin Save attachments--
set theAttachments to every attachment of content of ThisMessage
set theOutputFolder to "OS X:Users:Shared:_New_Uploads:"
repeat with a from 1 to length of theAttachments
set theAttachment to item a of theAttachments
try
-- this works --
set theAttachmentName to name of theAttachment
set theSavePath to theOutputFolder & theAttachmentName
-- this does not --
--set thePathToCheck to theOutputFolder & theAttachmentName
--if item thePathToCheck exists then
-- set theSavePath to theOutputFolder & theAttachmentName & ".1"
--else
-- set theSavePath to theOutputFolder & theAttachmentName
--end if
save theAttachment in theSavePath
end try
end repeat
--end Save attachments--
set tempID to message id of ThisMessage
-- name of account of mailbox of ThisMessage
set theRecipient to address of recipients of ThisMessage as list
set TempFileName to ((path to desktop) as string) & tempID & ".txt"
set theTempFile to open for access TempFileName with write permission
write "Printed for: " & theRecipient & return to theTempFile
write "---------------------------------------------------------------------------------------" & return to theTempFile
write (tempID as string) & return to theTempFile
write "---------------------------------------------------------------------------------------" & return to theTempFile
write "Sender: " & (sender of ThisMessage as string) & return to theTempFile
write "Subject: " & (subject of ThisMessage as string) & return to theTempFile
write "Sent: " & (date sent of ThisMessage as string) & return to theTempFile
write "Received: " & (date received of ThisMessage as string) & return to theTempFile
write "---------------------------------------------------------------------------------------" & return to theTempFile
write ">Begin Message: " & return & return to theTempFile
write (content of ThisMessage as string) to theTempFile
write ">End Message: " & return to theTempFile
write "---------------------------------------------------------------------------------------" & return to theTempFile
close access theTempFile
do shell script "lpr -r " & "~/Desktop/" & tempID & ".txt"
end repeat
on error TheError
display dialog "Problem: " & TheError
end try
end tell
end perform mail action with messages
end using terms from
using terms from application "Mail"
on run
tell application "Mail" to set sel to selection
tell me to perform mail action with messages (sel)
end run
end using terms from
Hi,
this saves all attachments and creates new file names if the file name exists.
The file extension will be considered and the index number will be incremented
until the name doesn’t exist.
using terms from application "Mail"
on perform mail action with messages The_Messages
set Save_folder to (((path to shared documents folder) as Unicode text) & "_New_Uploads:")
tell application "Mail"
repeat with This_Message in The_Messages
repeat with ma in mail attachments of This_Message
set fName to my checkFileName(Save_folder, name of ma)
save ma in fName
end repeat
end repeat
end tell
end perform mail action with messages
end using terms from
on checkFileName(fDir, fName)
try
set f to (fDir & fName) as alias
set {name:Nm, name extension:Ex} to info for f
if Ex is missing value then set Ex to ""
if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
set idx to 0
repeat
set idx to idx + 1
set checkName to (fDir & Nm & "_" & (idx as string) & "." & Ex)
try
checkName as alias
on error
return checkName
end try
end repeat
on error
return (fDir & fName)
end try
end checkFileName
Thanks Stefan!
I’ve tweaked my script a bit so that it saves attachments as well as adds the attachment names to my email printout. I also modified the “checkFileName” routine so that it mimics the OS X behavior when duplicate files for found., I changed the underscore to a dash “-”.
Any recommendations on printing from Mail? This was the only Applescript method I could re-tool for my purposes.
using terms from application "Mail"
on perform mail action with messages theMsges
--set the attachment folder
set Save_folder to (((path to shared documents folder) as Unicode text) & "_New_Uploads:")
tell application "Mail"
try
repeat with ThisMessage in theMsges
set tempID to message id of ThisMessage
set theRecipient to address of recipients of ThisMessage as list
set TempFileName to ((path to desktop) as string) & tempID & ".txt"
set theTempFile to open for access TempFileName with write permission
--print the list of recipients
write "Printed for: " & return to theTempFile
repeat with i from 1 to length of theRecipient
set theAddress to item i of theRecipient
write theAddress & return to theTempFile
end repeat
--list relevant message information
write "-------------------------------------------------" & return to theTempFile
write "Sender: " & (sender of ThisMessage as string) & return to theTempFile
write "Subject: " & (subject of ThisMessage as string) & return to theTempFile
write "Sent: " & (date sent of ThisMessage as string) & return to theTempFile
write "Received: " & (date received of ThisMessage as string) & return to theTempFile
write "-------------------------------------------------" & return to theTempFile
write ">Begin Message: " & return & return to theTempFile
write (content of ThisMessage as string) & return & return to theTempFile
--check for attachments
if (count of mail attachments in ThisMessage) > 0 then
write "Attachments: " & return to theTempFile
end if
-- save each attachment
repeat with ma in mail attachments of ThisMessage
set fName to my checkFileName(Save_folder, name of ma)
--save the Attachments
save ma in fName
--include in the email print out
write " " & name of ma & return to theTempFile
end repeat
write " " & return to theTempFile
write ">End Message: " & return to theTempFile
write "-------------------------------------------------" & return to theTempFile
close access theTempFile
--print to a printer
do shell script "lpr -r " & "~/Desktop/" & tempID & ".txt"
--do shell script "lpr " & "~/Desktop/" & tempID & ".txt"
end repeat
on error TheError
display dialog "Problem: " & TheError
end try
end tell
end perform mail action with messages
end using terms from
using terms from application "Mail"
on run
tell application "Mail" to set sel to selection
tell me to perform mail action with messages (sel)
end run
end using terms from
on checkFileName(fDir, fName)
try
set f to (fDir & fName) as alias
set {name:Nm, name extension:Ex} to info for f
if Ex is missing value then set Ex to ""
if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
set idx to 0
repeat
set idx to idx + 1
set checkName to (fDir & Nm & "-" & (idx as string) & "." & Ex)
try
checkName as alias
on error
return checkName
end try
end repeat
on error
return (fDir & fName)
end try
end checkFileName
This is the only Applescript method ![:wink: :wink:](/images/emoji/apple/wink.png?v=12)