0

I've built an Automator application that takes a file and (after an admin password is entered) changes the ownership and moves it to another user's folder. I'm using the command:

mv -i source target

to move the file. The -i option will prompt before overwriting an existing file with the same name. But this prompt is only on the command line. If there is a duplicate file name, my Automator application acts as if it worked but nothing really happens.

My question:

How do I detect the -i prompt in the shell script and display it as a dialog in the Finder? Here's the complete "Run Shell Script" action:
## get password then chown file
echo $1 | sudo -Sk chown aklap "$2";

## password, then move file
echo $1 | sudo -Sk mv -i "$2" /destination;

x=`echo $2 | awk -F/ '{print $NF}'`;  ## get file name (awk to remove remove path)

osascript <<EOD
  tell app "System Events" to display dialog "The file: \"$x\" has been moved to /destination"
EOD
Adam
  • 613
  • 7
  • 16
  • 1
    I hope nobody has the password `-e 1234` or `hello *` or ... Please use more quotes and instead of `echo $1` use `printf '%s' "$1"` – gniourf_gniourf Nov 01 '13 at 09:18
  • To only get the basename, use either `${2##*/}` after the test `[[ -n ${2##*/} ]]` passes or `$(basename "$2")` but that might fail if `$2` has trailing newlines... Please, be very serious with your script, you're doing automated stuff with admin's rights! It's very dangerous! – gniourf_gniourf Nov 01 '13 at 09:23
  • Thanks for the advice. I know this is sloppy and has security issues; I'm just using it privately on my machine right now. Any advice re: my question though? – Adam Nov 01 '13 at 09:25
  • 1
    I don't know AppleScript well enough to suggest a concrete solution, but I would skip the `-i` option to `mv`, and instead take a multi-step approach: check if the file exists; if it does not, `mv "$2" /destination`; if not, prompt via AppleScript whether to proceed, and move unconditionally if it does not. This separates all user interaction into the AppleScript layer, and all the actual file handling into the `bash` layer. – chepner Nov 01 '13 at 12:51
  • Thanks, checking if the file exists before running the mv command was the answer for me! – Adam Nov 01 '13 at 16:27

0 Answers0