1

I need an AppleScript to zip files (not folders) contained in several sub_subfolders. At the end of each day we have a main folder with a name like SR_2022_Oct_20 inside the main folder are sub folders for each senior photographed. Those subfolders contain multiple files and a sub_sub_ folder named jpg(whitespace prefixed). I need to zip the files that are inside each jpg folder, so it ends up containing the zip file plus the original .jpg files.
The folder structure looks like this:

    main                # Format SR_yyyy_mmm_dd 
    ├── Senior1         # Format LastName_FirstName_ScoolCode 
    │   ├── \ jpg       # NOTE: whitespace as a prefix
    │   │   ├── one.jpg # All jpgs in that folder have to be zipped
    │   │   └── two.jpg # as "main/Senior1/ jpg/Senior1.zip"
    │   ├── one.CR2
    │   ├── three.CR2
    │   └── two.CR2
    └── Senior2         # Format LastName_FirstName_ScoolCode 
        ├── \ jpg       # NOTE: whitespace as a prefix
        │   ├── one.jpg
        │   └── two.jpg
        ├── one.CR2
        ├── three.CR2
        └── two.CR2

There can be 50 or 60 Senior folders in the main folder for each day.
The jpg folder has a space in front of the name
This is way beyond my limited scripting capabilities - any help is greatly appreciated.

4
  • Just do clarify: You want one zip file per jpg folder, stored within that folder?
    – nohillside
    Oct 28, 2022 at 14:20
  • Also, what is the purpose of creating zip files of jpg? Sizewise you gain 2% maybe.
    – nohillside
    Oct 28, 2022 at 14:23
  • Yes one zip file per jpg folder. Our Lab requires all files per senior to be sent in a zip file.
    – JBS
    Oct 28, 2022 at 14:27
  • One not so small detail I left out. The zip file needs to be named with the same name as that senior folder
    – JBS
    Oct 28, 2022 at 15:01

1 Answer 1

0

I would use a shell script.

  • Open Terminal
  • Change to the main directory by typing cd path/to/main and pressing Enter. If you don't know the path, type cd with a trailing space and and drag&drop the folder from Finder into Terminal
  • Run nano zip_seniors (a text editor in Terminal) and paste the code below into the window.
#!/bin/bash

for senior in *; do
    if [ -d "$senior" -a -d "$senior"/" jpg" ]; then
        echo "Zipping $senior"
        zip -q "$senior"/" jpg"/"$senior" "$senior"/" jpg"/*.jpg
    fi
done
  • Save with Ctrl-O, exit with Ctrl-X
  • Run chmod 755 zip_seniors (to make the script executable)
  • Run the script with ./zip_seniors
9
  • I'm really not familiar with shell scripts. How do I use this.
    – JBS
    Oct 28, 2022 at 15:17
  • @jbs Ah right. Will add some explanation details later.
    – nohillside
    Oct 28, 2022 at 15:20
  • @nolillside I very well could be wrong but the shell script looks like it is looking for the word senior in the folder name. The format for those folders is LastName_FirstName_SchoolCode.
    – JBS
    Oct 28, 2022 at 15:44
  • @jbs Not really. Open Terminal and enter cd main; for senior in *; do echo $senior; done to see how loops work.
    – nohillside
    Oct 29, 2022 at 9:06
  • @nolillside ok I learned a little ;-) I got as far as entering the code in the text editor in Terminal. The save command seemed to work but the exit would not let me out of the text editor. So, I ran the echo command code by changing the directory to my test folder and it returned all the first level folder names and a document file called zip_seniors.save -- now I'm lost.
    – JBS
    Oct 29, 2022 at 11:58

You must log in to answer this question.

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