Need help running a shell script using Applescript

I have a shell script that will zip file within jpg sub-subfolders. The shell script works when run from the terminal but I need to be able to pass this off to someone else. I'd like to have AppleScript prompt for the input folder (main folder containing all the subfolders) and then run the shell against that main folder.

Here's the Shell Script code. I don't know how to use it in an AppleScript.


#!/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


Posted on Oct 29, 2022 7:33 AM

Reply
9 replies

Oct 29, 2022 8:59 AM in response to JB001

Create an Applications -> Automator application that uses the "Run Shell Script" action. Copy and paste your script into the action.


You can change where the "Run Shell Script" action gets its arguments to accept drag & dropped files, so you could modify your script to use $1 as the default directory and then drag and drop a Folder onto your newly created Automator app and run your script against that directory.

Oct 30, 2022 3:27 PM in response to JB001

Where you have 'echo' in the original script, put this osascript

osascript -e "display dialog \"zipping ${senior}\" giving up after 5" &


The "giving up after 5" will automatically terminate the dialog box created in 5 seconds.


The & at the end of the line will cause the dialog box to be generated in the background while the script continues to work, so you DO NOT need to click on the "OK" button, as in 5 seconds this dialog box will self-destruct.


And because the script continues while the dialog box is on the display, the next zip command and the next dialog box will happen, displaying the newer dialog box overtop of the current dialog box. So effectively you will have a continuous stream of dialog boxes appearing over top of each other, telling you wish file is currently being zipped.


And at the end you can have the VikingOSX dialog box telling you that the script has finished.

Oct 29, 2022 1:19 PM in response to JB001

Automator is the silent type and only outputs information if there is an error. You can add the ability of the shell script to output the foldername to a report file on the Desktop though.


echo "Zipping $senior" >> ~/Desktop/ziplog.txt


Then the user can click once on that text file and press the spacebar and see the log via Quick Look.


The double chevron is shell speak for append, and a single '>' overwrites that file with new data.


At the end of the run shell script you can put the following code:


/usr/bin/osascript -e 'Display dialog "Folder zipping is complete" with title "Zip Folder Processing"'


and the user will see something like this:


Oct 29, 2022 9:17 AM in response to BobHarris

Thank you so much. This sounds like exactly what I need. Automator is a new animal for me so I need a little more help. Also I did't create the shell script so modifying it is a little terrifying ;-)

You can change where the "Run Shell Script" action gets its arguments to accept drag & dropped files.

I created the Automator Application and have the code in the Run Shell Script portion. Can't see how to change it to accept drag & dropped files.


modify your script to use $1 as the default directory


Not a clue how to do this

Oct 29, 2022 11:46 AM in response to BobHarris

I got this to work. I added a cd $@ to the beginning of the shell script and changed the Pass input to "as arguments" and it runs as planned. Still don't know how to have it accept drag & dropped files but choosing the input folder works fine.

Thanks again for getting me started on the correct path with this. Here's the final solution:

Oct 29, 2022 12:17 PM in response to VikingOSX

Actually I think I like the Ask for Finder Items better, as you said it is more user friendly. But now I know how to do it both ways -- thank you.

Another "problem" - when I run the shell from terminal the echo command shows each folder as it is being zipped. When run from the Application it doesn't show anything. With as many as 80 folders it would be nice to show the operator that progress is being made. Any thoughts?


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Need help running a shell script using Applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.