Hello, Everyone!
I’ve been working on this script a long time… it is designed to be used with Microsoft Entourage. In order to slim down the email database size, it saves attachments in a specified path (~/Documents/Archived Attachments/[current year]/[current date]), links the email to the newly saved version of the attachment, and then finally (assuming the save was successful) deletes the attachment from the email. I have a nearly identical version working with the older version of Entourage (version X), and it runs fine (mostly). However, this one, for the 2004 version of Entourage, has a really strange issue, which I can not figure out:
As the script is running, even though no windows are told to open (in fact, the script closes ever Entourage window before it begins the archiving procedure), little portions of message windows start appearing here, there & everywhere on the screen. Not complete windows, just small pieces of windows. You can not close them by any normal means, as they are not normal windows. The only way I’ve found to get rid of all these little window pieces that show up is to use Exposé. Hit F9, then F9 again, and all the artifacts that were there are gone… but new ones show up shortly again.
I’ve attached a copy of the script to this post… and it is a ˜SAFE’ version. That is, the line where the script deletes attachments from your email is commented out, just in case anyone would be willing to try out the script to help me out. If you do, please do make a backup copy of your current Entourage identity, just in case, though I can say I’ve done extensive testing and have never had a lost/damaged email, attachment, database, or what have you.
Can anyone tell me what I’m missing?
Thanks so much!
Jon
(*
Features to add:
Link archived attachment(s) to the message it came from. ***DONE***
Progress box (better yet, a bar) with constantly updating attachment count.
Bugs:
1) Throughout script, fragments of message windows appear for no apparent reason!
Pre-Production Changes:
1) Set duration before attachments are archived (6 months?)
*)
display dialog "Entourage is about to archive your attachments. You may continue working in other applications, but it is best if you can avoid using Entourage." & return & return & "Another message like this one will come up when the archiving process is complete." buttons {"Go!"} default button 1
--declaration of global variables:
global totalAttachmentCount
--We're starting at 0 attachments archived.
set totalAttachmentCount to 0
--Generate the appropriate path to save attachments in:
set svPath to createSavePath()
tell application "Microsoft Entourage"
try
close every window
on error errMsg number errNum
display alert "Error: Failed close every window command." & return & errMsg & return & errNum
end try
try
set outerFolders to every folder
on error errMsg number errNum
display alert "Error: Failure to set outerFolders to every folder." & return & errMsg & return & errNum
end try
try
set outerFolderCount to (count outerFolders)
on error errMsg number errNum
display alert "Error: Failure to set outerFolderCount to (count outerFolders)." & return & errMsg & return & errNum
end try
try
set todaysDate to (current date)
my archive_and_save(svPath, outerFolders, outerFolderCount, todaysDate) --Main routine call
set doneDate to (current date)
set timeToRun to (doneDate - todaysDate)
display dialog "All done!" & return & return & totalAttachmentCount & " attachments were archived in just " & timeToRun & " seconds." buttons {"Killer"} default button 1 with title "Spyder Attachment Archiver"
open main window
on error errMsg number errNum
display alert "Error: Failure in archive_and_save sub call." & return & errMsg & return & errNum
end try
end tell
-- SUBROUTINES SUBROUTINES SUBROUTINES SUBROUTINES
on archive_and_save(savePath, outsideFolders, outsideFolderCount, todaysDate)
tell application "Microsoft Entourage"
repeat with i from outsideFolderCount to 1 by -1
try
set theFolder to item i of outsideFolders
on error errMsg number errNum
display alert "Failure to set theFolder to item i of outsideFolders." & return & errMsg & return & errNum
end try
try
my extract_attachments(theFolder, savePath, todaysDate)
on error errMsg number errNum
display alert "Failure in extract_attachments call." & return & errMsg & return & errNum
end try
end repeat
end tell
end archive_and_save
on extract_attachments(thisFolder, save2Path, todaysDate)
set bogusPath to save2Path & "BOGUS:"
tell application "Microsoft Entourage"
try
set thisFolderCount to (count folders of thisFolder)
on error errMsg number errNum
display alert "Error - unable to set thisFolderCount to (count folders of thisFolder). Setting folder count to 0." & return & errMsg & return & errNum & return & "Folder Name: " & (name of thisFolder)
set thisFolderCount to 0
end try
try
if thisFolderCount is greater than 0 then
try
set innerList to every folder of thisFolder
set innerListCount to (count innerList)
on error errMsg number errNum
display alert "Failed to set innerList to every folder of thisFolder, or to set innerListCount to (count innerList)." & return & errMsg & return & errNum
end try
repeat with m from innerListCount to 1 by -1
try
set innerFolder to item m of innerList
my extract_attachments(innerFolder, save2Path, todaysDate)
on error errMsg number errNum
display alert "Failure in recursive extract_attachments call." & return & errMsg & return & errNum
end try
end repeat
end if
on error errMsg number errNum
display alert "Failure in repeat digging into folders, recursively calling extract_attachments subroutine." & return & errMsg & return & errNum
end try
try
try
set folderMsgs to every message of thisFolder
set messagesCount to (count messages of thisFolder)
on error errMsg number errNum
display alert "Failure to set folderMsgs to every message of thisFolder." & return & errMsg & return & errNum
end try
repeat with k from messagesCount to 1 by -1
try
set thisMsg to item k of folderMsgs
on error errMsg number errNum
display alert "Failure to set thisMsg to item k of folderMsgs." & return & errMsg & return & errNum
end try
try
set howOldIsIt to ((time received) of thisMsg)
on error errMsg number errNum
display alert "Failure to set howOldIsIt to ((time received) of thisMsg)." & return & errMsg & return & errNum
try
set howOldIsIt to ((time sent) of thisMsg)
on error errMsg number errNum
display alert "Failure to set howOldIsIt to ((time sent) of thisMsg)." & return & errMsg & return & errNum
set howOldIsIt to current date
end try
end try
try
try
set attachmentCount to (count attachments of thisMsg)
on error errMsg number errNum
set attachmentCount to 0
display alert "Failure to set attachmnetCount to (count attachments of thisMsg). Setting attachment count to 0." & return & errMsg & return & errNum
end try
-- CHANGE THIS FOR PRODUCTION VERSION FROM 6 MINUTES TO 6 MONTHS OR 9 MONTHS!!!!!!!!!
if (attachmentCount is greater than 0) and ((((current date) - (howOldIsIt))) > (minutes * 6)) then
try
set msgAttachments to every attachment of thisMsg
end try
repeat with l from attachmentCount to 1 by -1
try
set thisAttachment to item l of msgAttachments
on error errMsg number errNum
display alert "Failure to set thisAttachment to item l of msgAttachments." & return & errMsg & return & errNum
end try
try
save thisAttachment in bogusPath
set totalAttachmentCount to totalAttachmentCount + 1
set attachName to name of thisAttachment
set safeAttachName to my replacecolons(attachName)
set newFileLocation to (save2Path & safeAttachName)
link thisMsg to alias (newFileLocation)
--Below is the DELETE ATTACHMENT line... please un-comment this for real use.
--delete thisAttachment
on error errMsg number errNum
display alert "Failed to save thisAttachment in bogusPath." & return & "Message: " & (name of thisMsg) & return & "Attachment: " & (name of thisAttachment) & return & errMsg & return & errNum
end try
set thisAttachment to null
end repeat
end if
on error errMsg number errNum
display alert "Failure:" & return & errMsg & return & errNum
end try
end repeat
on error errMsg number errNum
display alert "Failure:" & return & errMsg & return & errNum
end try
end tell
end extract_attachments
--This subroutine simply creates and returns the path for saving attachments to
on createSavePath()
set todaysDate to (current date)
--Format months for save path
if (month of (todaysDate) as integer) < 10 then
set theMonth to 0 & (month of (todaysDate) as integer) as Unicode text
else
set theMonth to (month of (todaysDate) as integer) as Unicode text
end if
set theDay to day of (todaysDate) as Unicode text
set theYear to year of (todaysDate) as Unicode text
set theDate to ((theYear) & "_" & (theMonth) & "_" & (theDay)) as Unicode text
-- Try to create Archived Attachments folder, simply fails out if it already exists.
set docsPath to (path to documents folder)
try
tell application "Finder"
make new folder at docsPath with properties {name:"Archived Attachments"}
end tell
end try
--Create a folder for the year
try
do shell script "mkdir ~/Documents/Archived\\ Attachments/" & theYear
end try
--Create a folder for today's archive.
try
do shell script "mkdir ~/Documents/Archived\\ Attachments/" & theYear & "/" & theDate
end try
set svPath to ((docsPath as text) & "Archived Attachments:" & theYear & ":" & theDate & ":") as text
return svPath
end createSavePath
--This routine borrowed from Paul Berkowitz's 'Save Attachments 2004'
to replacecolons(someText)
set AppleScript's text item delimiters to {":"}
set textItems to text items of someText
set AppleScript's text item delimiters to {"-"}
set someText to textItems as Unicode text
set AppleScript's text item delimiters to {"/"}
set textItems to text items of someText
set AppleScript's text item delimiters to {"-"}
set someText to textItems as Unicode text
set AppleScript's text item delimiters to {""}
return someText
end replacecolons