AppleScript to Compress Files and Folders

Bokeh

Photo: Azim Islam

I had the need to select several folders at once in the Mac OS Finder and zip them up as individual archives. This AppleScript to compress files and folders was the solution I came up with.

The script compresses each item selected into its own archive, and works with both folders and files.


tell application "Finder"
    set theList to selection
    repeat with i from 1 to (count of theList)
        set theItem to (item i of theList) as alias
        set itemPath to quoted form of POSIX path of theItem
        set fileName to name of theItem
        set theFolder to POSIX path of (container of theItem as alias)
        set zipFile to quoted form of (theFolder & fileName & ".zip")
        do shell script "zip -jr " & zipFile & " " & itemPath
    end repeat
end tell

Download the source here.

As always, you’ll get the best results when used with FastScripts by Red Sweater Software.

Posts

2 Comments

  1. Kurt says:

    Interesting….

    This is almost something I was needing. However I need to:
    1 – select a folder from Finder
    2 – store the folder name to say “x”
    2 – in that folder “x”, compress all files and sub-folders with path
    3 – rename the zip file to x.zip

    and I don’t know AppleScript. Can you show me?

  2. Kurt says:

    oh…and

    4 – delete all the files that created the zip file.

    The purpose of the zip file is to conserve space on my hard drive.

    Thanks!