How to batch zip multiple folders without _macosx files

applescriptautomatorfinderzip

I'm wanting to zip multiple folders at the same time, where each folder is it's own zip file. I'd like to create the zip without the _macosx or any other hidden files, and if possible have the zip file's extension changed to .cbz at the same time. That way I can just run one process on one entire folder and get them all done quickly.

I'm using Ubooquity to host my comics and it treats the files as corrupt if the _macosx file or any other hidden files are inside the zip file.

Best Answer

Here is an Automator Service that uses as single Run Shell Script action to handle the process.

  • In Automator, select: File > New > Service

  • Set: Service receives selected folders in Finder

  • Add a Run Shell Script Action

    • Settings: Shell: /bin/bash and Pass input: as arguments
    • Replace all of the default code with the code show further below.
  • Save the Automator Service giving it an appropriate name, e.g.: Create CBZ Zip Archive

Now in Finder, select the target folder(s) that you want an individual zip archive, with a .cbz extension for each selected folder, then right-click and select Create CBZ Zip Archive, from the services section of the context menu.

Code for Run Shell Script Action:

for d in "$@"; do
    if ! cd "$(dirname "$d")"; then exit; fi
    if ! d="$(basename "$d")"; then exit; fi
    if [[ ! -e "${d}.cbz" ]]; then
        if ! zip -r "${d}.cbz" "$d" -x \*.DS_Store \*.localized; then exit; fi
    fi
done

Notes:

  • As coded, it only creates the filename.cbz zip archive file if the file does not already exist.
  • It creates the filename.cbz zip archive file in the same folder containing the selected folder(s).
  • It's written in a manner that if an error occurs with any of the steps taken, it exits the script without notification. Error handling can be modified as needed/wanted.
  • By default, zip will not include the __MACOSX folder and if you find there are other hidden files besides .DS_Store and .localized, although you shouldn't run in to the latter in this use case, you can add additional exclusions to the zip command.
  • As is, the only indication the script has finished is when the Automator Service Gear Icon on the Menu bar has disappeared. Some other form(s) of completion notification can be added to the script and or the workflow of the Automator Service extended with additional actions as appropriate, needed/wanted.
  • As is, it's no frills, however it does what it's programed to do.