简体   繁体   中英

Copy files through terminal in Automator

So the following issue seems to be caused within Automator but I cannot be sure (I would post an image but I don't have enough points):

I have an Automator service whose first action is “Get Specified Finder Items” tool that goes to ~/Desktop/D53_C71J_C . The next action is a “Run Shell Script” containing

PATH="$@"
echo "Path: $PATH"
cd "$PATH"
for f in *;
do
    echo "f: $f"
    CAT="$PATH/$f"
    echo "CAT: $CAT"
    cp "$f" ~/Desktop
done 

The results are

Path: /Users/ajharn/Desktop/D53_C71J_C
f: D53_C71J_C1.psd
CAT: /Users/ajharn/Desktop/D53_C71J_C/D53_C71J_C1.psd

f: D53_C71J_C1_MERGE.psd
CAT: /Users/ajharn/Desktop/D53_C71J_C/D53_C71J_C1_MERGE.psd
f: D53_C71J_C2.psd

etc. It all works until it gets to cp . I've tested with echos and such and paths are lined up. The confusing part is that cp D53_C71J_C2.psd ~/Desktop works fine in Terminal.

You have chosen your variable name very unwisely: PATH is used by the shell to store the path to directories where executables are located (see the section on special shell parameters in the BashGuide and the bash man page ). As cp is an external program, not a shell command (it is found at /bin/cp ), it is not found by the shell when you overwrite PATH with your current working path – and thus never runs.

Do something more along the lines of

[[ -d "$@" ]] && cd "$@"                # cd into folder if is one
for f in ./*; do                        # always prefix relative globs
    [[ -f "$f" ]] && cp "$f" ~/Desktop  # only copy files
done

and all will be well. Of course, adding a “Get Folder Contents” action and a “Move Finder Objects” one will work too, without any shell intervention.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM