3

I compiled the following script to zip multiple folders with only their contents.

(So for instance, folder X containing files A, B and C will form a zip file containing A, B and C and not folder X as a parent.)

on run {input, parameters}

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

end run

By testing it with a simple folder and a few random files, the script works fine. But when the folder contains a lot of files, the following error pops up:

'zip warning: first full name: /Users/laurens/Desktop/Test folder/.DS_Store second full name: /Users/laurens/Desktop/Test folder/story_content/.DS_Store name in zip file repeated: .DS_Store this may be a result of using -j

zip error: Invalid command arguments (cannot repeat names in zip file)'

enter image description here

I checked the same folder on Windows (through Parallels), and I can't see any .DS_Store files, also I'm not sure why the script refers to those files instead of the folders...

I'm not sure what to make of this.

1
  • For whatever reason, Finder won’t show certain invisible files, including .DS_Store, so you would need to use the Terminal.
    – red_menace
    May 2, 2022 at 14:40

1 Answer 1

4

The reason for the error is that zip tries to compress files by storing their filenames only, as specified by the -j option (from man zip):

Store just the name of a saved file (junk the path), and do not store directory names.

but there are two files with the same filename (.DS_Store):

  • ~/Test folder/.DS_Store
  • ~/Test folder/story_content/.DS_Store

Since zip can't store the same entry twice, it throws an error and aborts.

To solve this, exclude .DS_Store files from the zip file:

on run {input, parameters}

    tell application "Finder"
        set theItems to selection
        repeat with i from 1 to (count of theItems)
            set theItem to (item i 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")
            --
            -- Exclude files named .DS_Store
            do shell script "zip -jr " & zipFile & " " & itemPath & " -x \\*.DS_Store"
            --
            --
            delete (item i of theItems)
        end repeat
    end tell
    
    return input

end run

In the code above, I use -x to instruct zip to exclude .DS_Store files when creating a zip file. Since this prevents duplicated filenames from being selected for compression, the cannot repeat names in zip file' error doesn't occur.

Note that if you use the -x option when running zip in Terminal, you only need one backslash:

zip -jr file.zip files -x \*.DS_Store
7
  • 1
    Cool, I wil try that in a bit! Thank you.
    – LauLauPeet
    May 3, 2022 at 8:19
  • @LauLauPeet Did you have a chance to test the modified AppleScript?
    – jaume
    May 30, 2022 at 13:47
  • The first time it gave an error, but I couldn't reproduce it later on (so let's ignore that for now). The second time it dit work, and also with multiple folders, but now every folder inside the folder is "emptied" in the main folder, so every file is there, but no more folders... Could it be that the loop goes on within the main folder?
    – LauLauPeet
    Jun 2, 2022 at 8:53
  • @LauLauPeet Thank your for feedback. When you select multiple folders, all folder information is indeed lost. Let me give you an example: let's say you have a folder "A" that contains "a.txt" and "b.txt", and a subfolder "B" within "A" that contains files "c.txt" und "d.txt". If you select "A" in the Finder and run the script, the resulting zip file contains "a", "b", "c" and "d", without any folders, but that was already the case with the original script. Or am I missing something?
    – jaume
    Jun 8, 2022 at 9:24
  • 1
    Yes, probably because I tested it without subfolders, only files. I'll try and fix that... only I can't really see what part of the script that's responsible for the folder issue. Thanks!
    – LauLauPeet
    Jun 21, 2022 at 15:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .