How to make Automator run service I made (shell script) and display the output in Terminal

applescriptautomatorterminal

Hi I'm new to Macs and OSX, I'm using p7zip cli app to compress folders with big files inside (1; 2 GB or more). So I managed to make a Bash script to do it in batch process when I go to bed and I'm not using the computer.

This the code:

#!/bin/bash
 for folder in */
 do
 7z a -t7z -m0=lzma2 -mx=9 -mfb=64 -md=1024m -ms=on -mmt2 "${folder%/}.7z" "$folder"
 done

I have this in Comp7zip.sh file, I put that file inside the main folder that contains all the other subfolders I want to compress. So from the terminal I do:

$cd myfolder
$./Comp7zip.sh

Then in the terminal I can see how the 7z app is working, and it compresses all the folders just fine.

But since it is a bit annoying, copying the .sh file inside the folder and using the terminal every time, I use Automator to create a service and do it with a couple of clicks, this is the code:

#!/bin/bash
cd "$@"
for folder in */
do /usr/local/bin/7z a -t7z -m0=lzma2 -mx=9 -mfb=64 -md=1024m -ms=on -mmt2 "${folder%/}.7z" "$folder"
done

It's almost the same, just add the cd "$@" to use the selected folder in finder, so I have a new service that does the same thing that the .sh file does.

I just select the folder that has all the subfolders I want to compress, then pick my service from Finder's context menu, works fine and compresses all the folders inside the selected folder.

The problem here is, that in this way I can't see the 7z app wile it's working, all the compressing is made in the background with just and icon of a little gear in the menu bar that indicate the compress is running, and nothing more.

So with little files this is not a problem the compress ends fast, but with large files, I can't see how the compress work is doing it.

So the question is, how can I make Automator to show me the running task in the same way the method with the .sh Bash file does?

Also any ideas of how to add to this process a way to compress files outside subfolders in the selected folder? I mean all this code only works with folders and it's content, but not with files, so how cam I make it work with folders and files in the same process?

I'm sorry for my grammar, English is not my native language.

Best Answer

If you run a shell script by:

  1. Opening the Terminal application
  2. Telling the Terminal application to start your script

Then the script's output will, by default, be displayed inside the Terminal window (this can be changed by, for example, redirecting the output to a text file with the > character). This is what you have been doing "before going to bed".

Shell scripts, however, can also be run without ever opening the Terminal application! For example, you could create a launchd plist to run a shell script whenever your computer finishes booting. In this case, the script would run, but nothing would be displayed on screen, because the Terminal application was never opened.

Similarly, Applescript's do shell script command executes shell code directly without ever opening the Terminal.

Instead of running the shell code directly, tell the Terminal application to run the shell code for you:

tell application "Terminal"
  do script "echo 'Hello World'"
end tell

Now, Applescript will open a Terminal window, and run your shell code inside of it.

Screenshot