Zip folder using applescript

I have this applescript that takes the selected item and zips up this file / folder and uses the name as the zip name. The problem is that when I unzip the zip code, it creates a folder structure all the way from the user. Like this:

Users:
   username:
      folder:
           folder:

I just would like to:

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
+3
source share
2 answers

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

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

This should point the zip command to the garbage path to what you are trying to compress when it creates the directory structure for the zip file.

+3
source
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
+2
source

Source: https://habr.com/ru/post/1790226/


All Articles