I recently wrote an Applescript to archive attachments from selected IMAP email messages to reduce mailbox requirements on the server side as my boss prefers not to delete emails, but just to remove attachments (we have a 1GB space limit).
As such, the script performs the following steps:
- Move the message from the IMAP account to the local machine where the email can be modified.
- Remove the attachment into a folder hierarchy: Year:Month:Day:Subject:Attachment.ext
- Modify the message’s subject to include the previously attached filename.
- Move the message back from the local machine to the IMAP account again.
When I run the script from the script editor with Entourage open, the script runs properly. However, when I run the script from the Entourage script menu, the script halts after the move command. I can’t seem to determine what would be causing the difference in script operation. Does anybody perhaps have an idea?
Here’s the code:
tell application "Microsoft Entourage"
-- tell application "Finder" to set the destination_folder to path to documents folder as text
-- set destination_folder to destination_folder & "Email Attachments"
set destination_folder to "homedir:lauf$:Attachments"
set destination_folder to text returned of (display dialog "What folder do you want to save the attachments to?" default answer destination_folder)
set theMessages to current messages
repeat with i from the (count of theMessages) to 1 by -1
set theMessage to item i of theMessages
set messageDate to time received of theMessage
set messageYear to year of messageDate
set messageMonth to month of messageDate
set messageDay to day of messageDate
set messageSubject to subject of theMessage
set originalFolder to the storage of theMessage
move theMessage to folder "Attachment Remover"
set theOldMessage to theMessage
-- It is imperative that nothing gets moved into the attachment remover
-- folder in order for this script to work properly...I guess I could create
-- a temporary one, but that's too much work to figure out right now. :)
set theMessage to item 1 of the messages of folder "Attachment Remover"
if (count (theMessage's attachments)) > 0 then
set attachmentDir to destination_folder & ":" & (messageYear as text) & ":" & messageMonth & ":" & (messageDay as text) & ":" & messageSubject
my checkAndMakeDirs(attachmentDir)
set allTheAttachments to theMessage's attachments
repeat with theAttachment in allTheAttachments
set theFilename to attachmentDir & ":" & theAttachment's name
try
save theAttachment in theFilename
set theMessage's subject to (theMessage's subject & " <<" & theAttachment's name & ">>")
on error errnum
end try
end repeat
delete allTheAttachments
end if
delete theOldMessage
move theMessage to the originalFolder
end repeat
end tell
on splitText(delimiter, someText)
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set output to text items of someText
set AppleScript's text item delimiters to prevTIDs
return output
end splitText
on checkAndMakeDirs(dir)
set dirlist to splitText(":", dir)
(* We have to assume that the top directory exists *)
if (count dirlist) < 2 then return
tell application "Finder"
set parentFolder to ""
repeat with i from 2 to count dirlist
set parentFolder to (parentFolder as text) & (dirlist's item (i - 1)) as alias
set currentFolder to dirlist's item i
if not (exists folder currentFolder of parentFolder) then
make new folder at parentFolder with properties {name:currentFolder}
end if
end repeat
end tell
end checkAndMakeDirs