(*

CacheCleaner

By: Joe Szedula

   Notes:
  1. This script cleans up the contents of various cache and temporary folders.
  2. This script should be run _before_ any of the applications that use these folders are opened. This script could be run as a "Startup Item." If the folders are in use when this script cleans them, unpredictable errors can be the result. This script attempts to quit the effected applications, but if the applications have been renamed they could still be open when the cache is cleaned.
  3. Applications effected:
    - Acrobat Reader (pre 7.0)
    - Adobe Reader (7.0)
    - BBedit
    - Camino
    - Flash (see: http://en.wikipedia.org/wiki/Local_Shared_Object and
       http://machacks.tv/2009/01/27/flushapp-flash-cookie-removal-tool-for-os-x/)
    - Google Earth
    - Help Viewer
    - Internet Explorer
    - Java 1.3.1 Plugin
    - Java 1.4.2 Plugin
    - Mozilla
    - Mozilla Firefox 1.5 (and later)
    - Mozilla Firefox pre 1.5
    - Photoshop Elements 3.0
    - Quicktime
    - Safari
    - SeaMonkey
    - Sherlock
    - Software Update
  4. Since the purpose of this script is to delete files/folder use it at your own discression.
*)
on run
   set prefs to (path to preferences) as text
   set lib to GetFolder(prefs)
   set AppSupport to lib & "Application Support:"
   set Caches to lib & "Caches:"
   set JavaCache131 to Caches & "Java Applets:1.3.1:jar"
   set JavaCache142 to Caches & "Java Applets:cache:javapi:v1.0:"
   set AppList to {"BBEdit", Â
      "firefox-bin", "mozilla-bin", "seamonkey-bin", "Camino", "Safari", "Internet Explorer", Â
      "Acrobat Reader", "Adobe Reader", "Photoshop Elements 3", "QuickTime Player", Â
      "Help Viewer", "Sherlock", "Software Update", "Google Earth"} as list
   set FlashFolder to prefs & "Macromedia:Flash Player:"
   
   -- quit apps before cleaning their Cache files/folders
   with timeout of 240 seconds
      repeat with theApp in AppList
         set theApp to theApp as text
         set isOpen to isAppOpen(theApp)
         if isOpen then
            tell application theApp to quit
         end if
      end repeat
      
      -- folders to be cleaned
      set MozillaProfiles to lib & "Mozilla:Profiles"
      set FireFoxProfiles to AppSupport & "Firefox:Profiles"
      set FireFoxCaches to Caches & "Firefox:Profiles"
      set CacheList to {Â
         lib & "Icons", Â
         lib & "Safari:Icons", Â
         Caches & "Acrobat:7.0:Search70", Â
         Caches & "Acrobat:Search", Â
         Caches & "BBedit", Â
         Caches & "Camino:Cache", Â
         Caches & "Camino:IconCache", Â
         Caches & "com.apple.helpui", Â
         Caches & "com.apple.SoftwareUpdate", Â
         Caches & "Google Earth", Â
         Caches & "MS Internet Cache", Â
         Caches & "QuickTime:downloads", Â
         Caches & "Safari", Â
         Caches & "Sherlock:WebFoundation", Â
         JavaCache131, Â
         JavaCache142 & "ext", Â
         JavaCache142 & "file", Â
         JavaCache142 & "jar", Â
         JavaCache142 & "tmp", Â
         AppSupport & "Adobe:FileBrowser:PhotoshopElements3", Â
         AppSupport & "FullCircle", Â
         FlashFolder & "#SharedObjects", Â
         FlashFolder & "macromedia.com:support:flashplayer:sys"} as list
      
      tell application "Finder"
         -- clean.up "Mozilla" based web browser folders
         my CleanMozillaProfile(FireFoxProfiles)
         if folder MozillaProfiles exists then
            repeat with f in (name of every folder in folder MozillaProfiles) as list
               set f to MozillaProfiles & ":" & f as text
               my CleanMozillaProfile(f)
            end repeat
         end if
         -- cleans up the contents of a "Firefox" based web browser cache folder" (added for Firefox 1.5)
         if folder FireFoxCaches exists then
            repeat with f in (name of every folder in folder FireFoxCaches) as list
               set CacheFolder to FireFoxCaches & ":" & f & ":Cache" as text
               try
                  delete every item in folder CacheFolder
               end try
            end repeat
         end if
         
         -- discard contents of other cache folders
         repeat with f in CacheList
            set f to f as text
            if folder f exists then
               delete every item in folder f
            end if
         end repeat
      end tell
   end timeout
end run

-- cleans up the contents of a "Mozilla" based web browser "ProfileFolder"
on CleanMozillaProfile(ProfileFolder)
   with timeout of 240 seconds
      tell application "Finder"
         -- make sure profile folder exists before trying to clean it up
         if not (folder ProfileFolder exists) then
            return
         end if
         
         -- look for all profile folders
         repeat with f in (name of every folder in folder ProfileFolder) as list
            try
               -- make sure the real "bookmarks.html" file exists
               --   don't want to delete backups if orginal is missing
               set f to ProfileFolder & ":" & f as text
               my DupCleaner(f, "cookies.txt", "cookies-")
               my DupCleaner(f, "bookmarks.html", "bookmarks-")
            end try
            
            -- delete "old" (more than a week old) "bookmarkbackups" files (added for Firefox 1.5)
            set bm to f & ":bookmarkbackups"
            if folder bm exists then
               try
                  set bm to quoted form of POSIX path of bm as string
                  set cmd to "/usr/bin/find " & bm & " -maxdepth 1 -type f -mtime +7 -exec mv {} ~/.Trash \\;"
                  set theResult to do shell script cmd
               end try
            end if
            
            -- delete the contents of the Cache folder
            set f to f & ":Cache" as text
            if folder f exists then
               try
                  delete every item in folder f
               end try
            end if
         end repeat
      end tell
   end timeout
end CleanMozillaProfile

on DupCleaner(theFolder, theMaster, theRoot)
   with timeout of 240 seconds
      tell application "Finder"
         set m to theFolder & ":" & theMaster
         if file m exists then
            -- look for old files to delete
            set l to (name of every file in folder theFolder whose name begins with theRoot) as list
            repeat with b in l
               set f to theFolder & ":" & b as text
               if file f exists then
                  delete file f
               end if
            end repeat
         end if
      end tell
   end timeout
end DupCleaner

-- returns path of folder containing "thePath"
on GetFolder(thePath)
   if thePath ends with ":" then
      -- get the folder of a folder
      set p to (characters 1 thru ((count of thePath) - 1) of thePath) as text
   else
      -- get the folder of a file
      set p to thePath
   end if
   set x to the offset of ":" in (the reverse of every character of p) as string
   return (characters 1 thru -(x) of p) as text
end GetFolder

-- see if application is open
on isAppOpen(theApp)
   tell application "Finder"
      set theResult to (process theApp exists)
   end tell
   return theResult
end isAppOpen