10.3: Use a Folder Action script for easier zip compression

Nov 24, '03 10:31:00AM

Contributed by: Anonymous

While Panther's Finder's contextual menu allows Zip compression of files with its "Create Archive of ..." this still requires bringing up the contextual menu, selecting the option .. and then 'cleaning up' the original file and newly created archive.zip. As Panther has the attached Folder Actions, I figured this could be used to shortcut the process and consolidate the file/archive within one place. Luckily "Sal from Cupertino" picked up on my query in this regard, and provided the following script to compile with the AppleScript Script Editor.

[robg adds: Read the rest of the hint for the script ... and I think "Sal from Cupertino" is probably Sal Soghoian, the AppleScript Product Manager at Apple (at least, that's what he was a few years back).]


on adding folder items to this_folder after receiving these_items
  tell application "Finder"
    if not (exists folder "Done" of this_folder) then
      make new folder at this_folder with properties ¬
        {name:"Done"}
    end if
    set the destination_folder to folder "Done" of ¬
      this_folder as alias
    set the destination_directory to POSIX path of ¬
      the destination_folder
  end tell
  repeat with i from 1 to number of items in these_items
    set this_item to item i of these_items
    set the item_info to info for this_item
    if this_item is not the destination_folder and the ¬
      name extension of the item_info is not in ¬
      {"zip", "sit"} then
      set the item_path to the quoted form of the ¬
        POSIX path of this_item
      set the destination_path to the quoted form of ¬
        (destination_directory & (name of the ¬
          item_info) & ".zip")
      do shell script ¬
        ("/usr/bin/ditto -c -k -rsrc --keepParent " & ¬
          item_path & " " & destination_path)
    end if
  end repeat
end adding folder items to
NOTES:Having placed the compiled script into /Library -> Scripts -> Folder Action Scripts and then using the contextual menu to attach the Folder Action to an empty folder, you then have the one place to store the original and zipped copy. Keeps the Desktop neat and saves time

Thanks Sal and Applescript!

Comments (8)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20031115033508637