'Run Shell Script' does not work in Automator

I've upgraded to MacOS Monterey and my old Automator workflow scripts no longer work.


I've converted them to 'Quick Actions', but they don't work.


My workflow scripts use 'run shell script', which take the selected files or folders and makes a tarball.


I echo date, variables, commands, and errors to a file to log what's going on.


It's as if tar cannot find the files. Below is the code:

exec 3>~/output.txt

for f in "$@"
do
	date +%+ >&3
	echo "\$f is '${f}'" >&3

	PARENT_DIR="$(dirname "$f")"
	ITEM_NAME="$(basename "$f")"
	TAR_NAME=$(printf '%s' "${ITEM_NAME/ /-}" | tr [:upper:] [:lower:])

	echo "\$PWD is '${PWD}' before CD" >&3 

	cd "$PARENT_DIR" 2>&3
	
	tar -czf "${TAR_NAME}.tar.gz" --no-xattrs --options='gzip:compression-level=9' "$ITEM_NAME" 2>&3

	echo "\$PARENT_DIR is '${PARENT_DIR}'" >&3
	echo "\$ITEM_NAME is '${ITEM_NAME}'" >&3
	echo "\$TAR_NAME is '${TAR_NAME}'" >&3
	echo "\$PWD is '${PWD}' after CD" >&3
done

exec 3>&-


Here is what's printed in ~/output.txt:

Wed Dec 8 12:07:03 PST 2021
$f is '/Users/neal/Documents/Current Work/Interim Billboard/Previous Versions'
$PWD is '/Users/neal' before CD
tar: Previous Versions: Cannot stat: Bad file descriptor
tar: Error exit delayed from previous errors.
$PARENT_DIR is '/Users/neal/Documents/Current Work/Interim Billboard'
$ITEM_NAME is 'Previous Versions'
$TAR_NAME is 'previous-versions'
$PWD is '/Users/neal/Documents/Current Work/Interim Billboard' after CD

This is what I expected, except for the tar errors. The result is an empty tarball.


I have set 'Full Disk Access' to Teminal.app, Automator.app, and even Shortcuts.app.


This code did work in MacOS High Sierra.


Any ideas as to what is going on, or how I can fix this?




iMac 27″, macOS 12.0

Posted on Dec 8, 2021 12:20 PM

Reply

Similar questions

7 replies

Dec 8, 2021 12:52 PM in response to krousen

Does it work when the above content is placed inside a regular shell script and run with command-line arguments in the Terminal? Easier to debug. Try using the appropriate shebang at the beginning of your script.


Maybe even source ~/.bash_profile or source ~/.zshrc at the beginning of the Automator Run Shell Script as it doesn't know your local PATH.

Dec 8, 2021 2:00 PM in response to krousen

The PATH when running under Automator is going to be

PATH=/usr/bin:/bin:/usr/sbin:/sbin


Your default directory will be

cd $HOME


You will at most have 15 or 16 environment variables, most of which will not be all that interesting, except for maybe $USER, $HOME, $LOGNAME, $SHELL, $TMPDIR


Many people get tripped up by PATH not being what they expect, and sometimes because environment variables they have grown to depend on are not present.


Any customizations you may have made in your shell initialization scripts will not be present

Dec 8, 2021 2:40 PM in response to VikingOSX

Hi VikingOSX,


Thank you for testing my shell script.


I don't mind the file extension being added to the tarball, because the script should work on both directories and files.


Did you run this through Automator or Terminal? I could never get the FD to be anywhere other than my $HOME.


Also, I got the script to run in Shortcut as a Quick Action. I had to set the 'shortcut input' for the 'Run Shell Script' to Type: Folder; and Get: file paths. The scripts runs creating a tarball of directories and/or files.


The Shortcut works, but Automator does not. I even sourced my .bach_profile in the Automator version.

Dec 8, 2021 1:18 PM in response to VikingOSX

Hi VikingOSX,


Yes, when I run the commands from a shell script in Terminal, it works.


Also, I made a Shortcut Quick Action with the same code (that is a 'run shell script'), but I have a different problem, the selected files/directories don't have a parent directory. The files/directories output a '"." when I use the dirname command. And, the $PWD defaults to my $HOME directory. However, if I hardwire a directory path in the shell script, then select files/directories in that parent directory, the script works.


I would use the Shortcut version if I can figure out how to capture the parent directory from the selected files.





Dec 8, 2021 2:00 PM in response to VikingOSX

This appears to work and enters no errors in the output.txt file. I am on my Desktop when I run this, and a tar file is created based on the folder contents of TestX:


exec 3>~/Desktop/output.txt
tar -czf blah.tar.gz --no-xattrs --options='gzip:compression-level=9' TestX >&3



Tested: macOS Big Sur 11.6.1., Monterey 12.0.1, Zsh 5.8, and Bash (3.2.57, 5.1.12).


Your ITEM_NAME is still filename.ext after basename which would result in filename.ext.tar.gz. To get a true basename result, you can concurrently strip the path and the extension using:


shopt-s extglob
# get the true basename without path or extension
ITEM_NAME="${f//+(*\/|.*)}"
shopt -u extglob


This works in Zsh 5.8 without any setopt necessary.



Dec 8, 2021 3:00 PM in response to krousen

If this is a Zsh script, then you can use ${f:a} to expand to the absolute path string. I only tested in the Terminal due to some local time constraints. Automator is a piece of work… and it is good that you got it working as a Shortcut on Monterey.


I suppose I should do some Automator/Script migrations to Shortcut to learn more about it. Been avoiding it…

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.

'Run Shell Script' does not work in Automator

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