Q: bash "mv" command fails in Automator but works in shell
I'm trying to build an Automator workflow that prompts for two files and then renames the basename of the first to the basename of the second. For example, I have two files: FOO.INP and BAR.OUT. I want to rename the first to BAR.INP
My workflow contains two modules: an AppleScript that provides the user interface to select the files, which passes the results to a shell script as arguments.
I've worked a number of ways to parse out the path and names (both files will be in the same folder) of the files to use them with the bash shell "mv" (move/rename) command. (I can't believe Apple makes it so hard to rename files).
Here's the shell script as I currently have it. I used bash shell string manipulation to extract the path, basename and file extension (if any) of the passed file names, then concatenate them in the 'mv' command:
BASEPATH="${1%/*}"
FILE1STRIPPATH="${1##*/}"
FILE1BASE="${FILE1STRIPPATH%.*}"
FILEONEEXT="${FILE1STRIPPATH##*.}"
FILE2STRIPPATH="${2##*/}"
FILE2BASE="${FILE2STRIPPATH%.*}"
FILETWOEXT="${FILE2STRIPPATH##*.}"
mv "\"${BASEPATH}/${FILE1BASE}.${FILEONEEXT}"\" "\"${BASEPATH}/${FILE2BASE}.${FILEONEEXT}"\"
When I run this in Automator, I get an error:
mv: rename "/Users/myname/Desktop/Test Folder/FOO.INP" to "/Users/myname/Desktop/Test Folder/BAR.INP": No such file or directory
Yet it I run that same command in bash in Terminal, the rename works. What gives?
MacBook Air, OS X El Capitan (10.11.6), 2.0 GHz, 8GB, 512 Flash HD
Posted on Sep 30, 2016 12:04 PM