tim
tim

Reputation: 927

zip folder using applescript

I have this applescript that takes the selected item and zips that file/folder and uses the name as the zip name. The problem I have is when I unzip the zip it makes it has a folder structure all of the path from User on up. Like this:

Users:
   username:
      folder:
           folder:

I would just like it to be :

folder:

Here is the code:

tell application "Finder"
    set theItem to selection 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 -r " & zipFile & " " & itemPath
end tell

Upvotes: 4

Views: 6856

Answers (2)

nakagawa yoshiyuki
nakagawa yoshiyuki

Reputation: 21

tell application "Finder"
    set theItems to selection
    repeat with _a from 1 to (count of theItems)
        set theItem to (item _a of theItems) 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 -r " & zipFile & " " & itemPath
    end repeat
end tell

Upvotes: 2

DrGoldfire
DrGoldfire

Reputation: 1026

Add a -j switch to your zip command. In other words, change the last line of your script before "end tell" to:

do shell script "zip -jr " & zipFile & " " & itemPath

That should tell the zip command to "junk" the path to whatever you're trying to compress when it makes the directory structure for the zip file.

Upvotes: 3

Related Questions