Making AppleScript execute a CP CL command refuses to work.

Hi folks,

I’ve got a question about getting the command line to accept a request for a simple CP command with option -R from an AppleScript using ASS. For some reason CP doesn’t like the way AppleScript is issuing commands. When I use the same command in the CL it works fine but AppleScript complains about it when I try to automate the process. Here’s the code…

on clicked theObject
	set myCatalogsPath_text to (contents of text field "myCatalogsPath" of drawer "drawer" of window "main")
	set myBackupPath_text to (contents of text field "myBackupPath" of drawer "drawer" of window "main")
	
	do shell script ("cp -r & myCatalogsPath_text & " " & myBackupPath_text)
	display dialog "Beginning backup now..." buttons {"OK"} default button 1 attached to window "main" giving up after 15
end clicked

You can see that the AppleScript takes the values entered into text fields (file and folder paths) from another panel and then brings the value to a shell script to be pasted into the CL. (without opening Terminal)

The the command “do shell script” is given which should then execute the command in quotes. “cp” and the option of -R which is needed to copy any symbolic links existing in a directory and places it at the chosen destination as seen in myBackupPath (now myBackupPath_text).

I may have overlooked something here but I’m getting EOF errors for lines 1 & 2. Can anybody see a problem with my syntax or maybe in the script as a whole??

The only thing that comes to mind is to verify the contents of the relevant text fields.

Do you know they exist? Do you know they contain valid directory names/paths?

Additionally, your ‘display dialog’ won’t do what you expect.

As written, the do shell script will wait for the cp to finish before displaying the dialog, despite what your dialog implies.

To resolve this you would need to force the cp to a background task, and suppress all output. This can be done by appending " > /dev/null 2>&1 &" to the end of the shell script parameters. “> /dev/null’ suppresses stdout, “2>&1” suppresses stderr (technically, it sends it to the same place as stdout, which is /dev/null”, and the “&” tells the shell to execute in the background, returning control to your application immediately.

However, if you do that you won’t be able to get any feedback from the cp (permission errors, lack of disk space, etc.), so you won’t know if it worked or not.

Assuming this is an AppleScript Studio app, you should look at some other way of indicating the backup is in progress, and invoke that before you start the cp, removing it when it’s done, e.g. use a static text label in your window, set its contents to “Backing up…”, start the copy, when the do shell script finishes, change the text label back to blank and display any errors that occurred.

I think part of the problem might be in the construction of the shell command. I would build the entire command and then run the do shell script on it. Something like this:

set shellCommand to "cp -r " & myCatalogsPath_text & " " & myBackupPath_text
do shell script shellCommand

One other issue to be aware of is file paths. If a folder or file name includes a space, it needs to be dealt with for the shell script to execute successfully.

A combination of these suggestions is working out- at least I’m over the first hurdle which was execution of the CP command. Although I liked " > /dev/null 2>&1 &" Camelot was right about wanting some error feedback.

The script:

set shellCommand to "cp -r " & myCatalogsPath_text & " " & myBackupPath_text 
do shell script shellCommand

is nice and clean and I like it… You also mentioned handling spaces… As Mac OS X can handle spaces in filenames and folders its not so easy in the CL. Not impossible but AppleScript should deal with it somehow.

Did you have a suggestion for this? Is it anything like the POSIX conversion from AppleScript/Classics use of ":"s in file paths? Thanks for all your replies!

You can try the quoted form of POSIX. If I’m not mistaken, this is supposed to eliminate the need to escape the spaces.

set foo to (choose file with prompt "Choose a file with a space in the name.") as text
set bar to quoted form of POSIX path of foo
display dialog "This path should work in a shell script:" & return & return & bar

So far every piece of advice is helping to get this closer to this happening correctly! The only consistant problem is that usage of CP is being overly scrutinized by the command line… I used Apple’s Simple Shell to test this. This is a combination of AppleScript and some C++ (?). The important thing is that AppleScript is taking the information your putting in and passing it on. It works well enough. I added its functionality to the application I’m working on and there were no problems.

So… I continued to work on scripting the:

display dialog "Copying selection now..." with icon 1 buttons {"OK"} default button 1 attached to window "main" giving up after 10
		set shellCommand to ("cp -R /" & translatedcatalog_path & " /" & translatedbackup_path)
		do shell script shellCommand

Placing the dialog before will work even if an error (cp usage) appears and it does give up after 10 secs also.

I noticed that AppleScript behaves with the CL a little differently than AppleScript Studio does. The stuff that I write for Studio doesn’t always work out. Could there be some problem here? Oh well- I’m starting to confuse myself.