G’day
The script below bombs on the line before the ‘beep 2’ and I can’t figure out why.
The files are being copied to a server, and I want to rename them in the new position, along with changing the file extension.
Any advice please?
(* Mail Item mover
Use as a Folder Action attached to a dedicated Mail folder, however
NOTE that the script must be attached to the 'Messages' folder
INSIDE the ACTUAL Mail Folder.
The path to the folder is
Username/Library/Mail/Mailboxes/Name_of_your_mailbox/Messages
When mail is dropped on the Mail folder, they are copied to...
1/ a yearly storage folder on the server, with sub-folders based on the
senders name & eMail addresses
2/ a daily, date-orientated folder on the server, with sub-folders
based on senders name & eMail addresses.
The dropped messages are then moved to the Trash folder, to avoid the
dedicated shifting folder from becoming too full, and slow.
By Santa
Version 1.2
*)
-- Alter this property to reflect the name of the Mail box folder
property MailBoxName : "* items to shift"
on adding folder items to this_folder after receiving added_items
tell application "Finder"
try
set ServerFolder to (folder "!preflight_art" of folder "Art Dept" of disk "Internal Backup") as alias
set YearlyStorage to "Yearly Storage " & (year of (current date)) as string
set tempYear to (year of (current date)) as string
set tempMonth to tempYear & "/" & (month of (current date)) as string
set tempDay to (day of (current date))
if tempDay < 10 then set tempDay to "0" & tempDay
set tempDay to tempMonth & "/" & (tempDay) as string
--Check and create YEAR of Storage folder
if not (exists folder YearlyStorage of folder ServerFolder) then
make new folder at folder ServerFolder with properties {name:YearlyStorage}
end if
--Check and create YEAR folder
if not (exists folder tempYear of ServerFolder) then
make new folder at folder ServerFolder with properties {name:tempYear}
end if
--Check and create MONTH folder
if not (exists folder tempMonth of folder tempYear of ServerFolder) then
make new folder at folder tempYear of folder ServerFolder with properties {name:tempMonth}
end if
--Check and create DAY DATE folder
if not (exists folder tempDay of folder tempMonth of folder tempYear of ServerFolder) then
make new folder at folder tempMonth of folder tempYear of folder ServerFolder with properties {name:tempDay}
end if
-- Now save new path as a variable to speed things up
set the destination_folder to (folder tempDay of folder tempMonth of folder tempYear of ServerFolder) as alias
using terms from application "Mail"
tell application "Mail"
repeat with AMessage in added_items
set theName to my getName(AMessage as string) as string
repeat with CycleThroughMessages in messages of mailbox MailBoxName
if (id of CycleThroughMessages & ".emlx" as string) is equal to theName then
set theSource to (the sender of CycleThroughMessages) as string
-- lines added to keep server happy
if length of theSource > 30 then
set theSource to text 1 thru 30 of theSource
else
set theSource to text 1 thru end of theSource
end if
set theSubject to (the subject of CycleThroughMessages) as string
exit repeat
end if
end repeat
set ShiftToTrash to false
tell application "Finder"
try
if not (exists folder theSource of folder YearlyStorage of folder ServerFolder) then
make new folder at folder YearlyStorage of folder ServerFolder with properties {name:theSource}
end if
if not (exists folder theSource of folder destination_folder) then
make new folder at folder destination_folder with properties {name:theSource}
end if
-- Do any desired stuff to message here, before moving (actually copying)
set temp to the current date
set temp3 to day of temp
if temp3 < 10 then set temp3 to "0" & temp3
set temp4 to time of temp
set temp5 to temp4 div 3600
set temp6 to (temp4 - (temp5 * 3600)) div 60
set temp7 to temp4 - (temp5 * 3600) - (temp6 * 60)
if temp5 < 10 then set temp5 to "0" & temp5
if temp6 < 10 then set temp6 to "0" & temp6
if temp7 < 10 then set temp7 to "0" & temp7
--NOTE : this changes name AND EXTENSION
set DateTimeName to (year of temp & "/" & month of temp & "/" & temp3 as string) & " " & temp5 & ":" & temp6 & ":" & temp7 & ".eml"
move AMessage to folder theSource of folder YearlyStorage of folder ServerFolder
set theName to my getName(AMessage as string)
set theFilePath to (folder theSource of folder YearlyStorage of folder ServerFolder) as string
set theFilePath to theFilePath & theName
set thePOSIXFilePath to POSIX path of theFilePath as string
--display dialog thePOSIXFilePath as string
set name of thePOSIXFilePath to DateTimeName
beep 2
move AMessage to folder theSource of folder destination_folder
set the name of theName of folder theSource of folder destination_folder to DateTimeName
set ShiftToTrash to true
end try
end tell
if ShiftToTrash then
set mailbox of CycleThroughMessages to mailbox "Trash"
else
display dialog "The message '" & theSubject & "' could not be moved."
end if
end repeat
end tell
end using terms from
on error errMsg number errNum
display dialog "An error has occurred. Please email me at my contact email address at MacScripters, with the error number and message." & return & return & "Regards, Santa" & return & return & "Error number : " & errNum & return & return & errMsg buttons {"OK"}
end try
end tell
end adding folder items to
on getName(theName)
set KeepDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {":"}
if theName > "" then
set theName to (text items -1 thru end of theName)
end if
set AppleScript's text item delimiters to KeepDelimiters
return theName
end getName
Model: G5 1.8 GHz
AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)