For Each Finder Selection Compress to Individual Archive?

I'd like a macro that, when executed, compresses each Finder selection (folder or file) to its own Zip file named after the file. Basically, it should work like Selecting a single file and then choosing File > Compress [Name].

This macro isn't doing it. It works with a single selection, but as soon as multiple items are selected, it compresses them all into a single Archive.zip, which makes sense because the File menu command changes to compress all selected files together.

Is there a way to do the For Each individual compression/ZIP?

It's a bit tricky to do because the Finder is both asynchronous, and can only compress one thing at a time. But this works (seems to work reliably for me anyway).

Compress Each Selected Item.kmmacros (4.7 KB)

2 Likes

That works for me, too. Thanks very much, Peter!

Thanks for this macro Peter.

Can anyone suggest a way to modify the macro to do the following?

Select folder with a large number of files in finder (say 400 for example)
Compress the files to zip files in groups of 10 (without splitting any individual files into multiple zip files)

Thanks in advance.

mind if I ask, what you'll be using this for?

Use a counter variable in the For Each Action, and add the path of each file to a Variable. When the counter reaches 10, pass the Variable with paths to an Execute Shell Script that uses the zip (or other compress) command to create the zip file. Reset the counter and keep looping.

1 Like

I'm helping a family member who has a photography website which can import and processes zip files, however, it crashes when the zip file, or image files are too large. I'm basically trying to automate the process of zipping the 400 image files into groups of ten without splitting any of the files between two zip files.

That's a fascinating idea. I'll look into it and post the macro if I'm successful.

1 Like

SUCCESS!

I am delighted with this macro. I wonder should I submit it to the macro page on its own thread? It probably has very very limited use for other people, but it will save me loads of time.

I eliminated the whitespace issue by renaming the directory and files until there are no spaces. I could probably do better there.

I also changed the ENV_PATH variable in preferences to /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin (though I don't fully understand the paths I admit, but it seems to work.

Does anyone have any suggestions how I should handle remaining files at the end. Say I highlight 10 files and process them in groups of 3, I have one left over, that I'd also like to be zipped, even if by itself.

Keyboard Maestro Actions.kmactions (5.8 KB)

Congratulations!

Yes, I would most definitely suggest you post it to the Macro Lib section. You might be surprised how many others have the same or very similar need.

Well, this will probably require a design change in your macro, but maybe it is something you can play with if you are interested.

Rather than put each file path in a separate KM variable, just build a list of files in one KM Variable, something like this.

Change of Plan

Well, when I was working up a simple example for you, I really got into it and before I knew it, I had written the entire Macro.

So, here is my macro that will give you another example, another method of achieving the same thing. You can then use whichever you prefer.


Example Output

image


MACRO:   Zipping Multiplle Files in Batches [Example]


#### DOWNLOAD:
<a class="attachment" href="/uploads/default/original/3X/6/8/68ad3b2ab6290516502c1274e9e1521a37bcf339.kmmacros">Zipping Multiplle Files in Batches [Example].kmmacros</a> (21 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

**As always, feel free to post any questions, comments, issues, and/or suggestion you might have concerning this macro.**

---

### ReleaseNotes

Author.@JMichaelTX 

**PURPOSE:**

* **Zip Files of Selected Extensions in Batches**
   * You can limit the number of files per zip file.
   * Zip file uses date/time to ensure uniqueness
   * Output Subfolder will be created if necessary

**HOW TO USE**

1. First, make sure you have followed instructions in the _Macro Setup_ below.
2. Select one or more files in Finder
3. Trigger this macro.

**MACRO SETUP**

* **Carefully review the Release Notes and the Macro Actions**
  * Make sure you understand what the Macro will do.  
  * You are responsible for running the Macro, not me.  ??
.
1. Assign a Trigger to this maro.
2. Move this macro to a Macro Group that is only Active when you need this Macro.
3. ENABLE this Macro.
.
* **REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:**
(all shown in the magenta color)
   * Set Variable “Local__OutputSubFolder”
   * Set Variable “Local__NumFilesPerZip” 
   * Set Variable “Local__ZipRootName”
   * IF:  File Does NOT have an Image Extension;
     * Set the file extensions to be allowed

**REQUIRES:**

1. **KM 8.2+**
2. **macOS 10.11.6 (El Capitan)**

TAGS:  @Zip @Compress @ShellScript @Finder

USER SETTINGS:

* Any Action in _magenta color_ is designed to be changed by end-user

ACTION COLOR CODES

* To facilitate the reading, customizing, and maintenance of this macro,
      key Actions are colored as follows:
* GREEN   -- Key Comments designed to highlight main sections of macro
* MAGENTA -- Actions designed to be customized by user
* YELLOW  -- Primary Actions (usually the main purpose of the macro)
* ORANGE  -- Actions that permanently destroy Variables or Clipboards,
OR IF/THEN and PAUSE Actions


**==USE AT YOUR OWN RISK==**

* While I have given this a modest amout of testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
* If you have any doubts or questions:
  * **Ask first**
  * Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

---

![image|572x2000](upload://quQqYA3JBha0WPFL435YfecVTcl.png)
1 Like

The dangers of helping! I was proud of my macro, but yours is a work of beauty! I still need to examine it in detail to see how you assign multiple values in one KM Variable, and also new to me is the realisation that you can count the files in a folder (though its unsurprising).

I will leave the submitting to you, as I think your macro is superior here. I'm glad my work helped make it happen. Thanks for your assistance.

1 Like

Please feel free to ask questions. Most likely others will have the same question.

1 Like

Hi @JMichaelTX,

I needed to use the macro on the weekend, and it didn't work for me, so I resorted to my old macro, and vowed to go look before I started asking questions. I finally did that today. Turns out somehow I managed to delete a single '%' after a variable, breaking the macro! So I'm glad I had a look.

My query as to assigning multiple values to a single variable was answered also, it simply adds more text to the end of the string. So simple, yet effective. I'm going to shorten the zipfile names myself, the zip files are already numbered and the long file names stress me out!

Thanks again, and please post this macro to the macro library, it's really excellent.

1 Like