Capturing output from cp or ditto

I’ve been working with an applescript that uses shell scripts to do some file copies. The script itself works fine, but I’m trying to create logs with information about what just happened. I can capture output of all kinds of scripts from do shell script by just setting it to a variable.

set theVar to do shell script "whoami" -- This works just fine
return theVar

The problem is with the cp and ditto commands. I’ve flagged them as verbose, and they just don’t return any output. Here’s an example:

set theVar to do shell script "sudo ditto -V -rsrc '/Volumes/acomData/Misc./Preference\ Profiles/Art Director:Graphic\ Artist/User Preferences/' '/Users/user/Library/Preferences/';"
return theVar --returns an empty variable

The command works fine from a terminal. I see the Verbose output as flagged. The script works too, as I can see the effects of it, but I just can’t capture the output! Do you have any suggestions?

Why don’t you just redirect the output of cp or ditto command to a log file directly from the shell.

I’ve tried:

do shell script "cp -v /Path_to_folder/Folder1/*.xtg /Path_to_folder/Folder2 > /Path_to_folder/Log_folder/some_file.log"

I also recomed to read “Technical Note TN2065” covering topics about using “do shell script” command in AppleScript. You should get some answers.


Thanks for your help. Your advice makes good sense. I tried that with this command

sudo ditto -V -rsrc '/Volumes/acomData/Misc./Preference Profiles/Art Director:Graphic Artist/User Preferences/' '/Users/user/Library/Preferences/' > /preferenceToolBox.040624090051.txt

and all I got was an empty log file. Any suggestions?

FYI, I pretty much have this one figured out. You have to capture standard error - not standard out for commands such as ditto. to do this you use syntax like this:

comand -arguments 'whatever else' > path to your standard out file 2> path to your standard error file

You can also use >> to make a file append, so you could redirect both outputs into the same file and run extra stuff in them over time.

You need to redirect the stderr (standard error) to stdout (standard out) by adding ‘2>&1’ to the end of the command like this:

set myanswer to do shell script " ditto -V ~/Desktop/ ~/Desktop/ 2>&1"
log "my answer is: " & myanswer

[Session started at 2005-07-06 20:10:16 +0200.]
2005-07-06 20:12:01.506 clicky[1189] “my answer is: >>> Copying /Users/moondog/Desktop/
copying file … 709126 bytes”

where without it goes:

set myanswer to do shell script " ditto -V ~/Desktop/ ~/Desktop/"
log "my answer is: " & myanswer

[Session started at 2005-07-06 20:12:38 +0200.]
2005-07-06 20:12:40.906 clicky[1205] "my answer is: "