PDA

View Full Version : Command works from Terminal, but not in an Apple Script? Why?




todd2000
Feb 28, 2008, 11:16 AM
If I type this command into the terminal, it works perfectly.

find ./ ! -name '*.download' | xargs -I trp cp -pf trp /Users/Todd/Downloads/trp 2>/dev/null

I added it to an AppleScript, and attached it to folder action script, and for some reason it doesn't work, and freezes Finder for a while when it tries to execute.

Here is the apple script:

on adding folder items to this_folder after receiving added_items
tell application "Finder"
do shell script "find ./ ! -name '*.download' | xargs -I trp cp -pf trp /Users/Todd/Downloads/trp 2>/dev/null"
end tell
end adding folder items to

Any reason why this apple script shouldn't work, and why it is freezing Finder?



kainjow
Feb 28, 2008, 11:44 AM
Are you sure the "./" is right? That is a relative path, you may need to put in this_folder in its place.

todd2000
Feb 28, 2008, 12:23 PM
Are you sure the "./" is right? That is a relative path, you may need to put in this_folder in its place.

./ is telling it to search in the folder that the script is attached to. "this_folder" is not a valid unix path, and the find command wouldn't know what to do with it. If I CD to the folder that I want to run the script from, and type it in the terminal it works fine, but for some reason not as an AppleScript.

HiRez
Feb 28, 2008, 12:23 PM
If it's not a relative path issue, it might be one of the characters in there that it doesn't like, maybe the ! or / characters. This Apple tech note (http://developer.apple.com/technotes/tn2002/tn2065.html) has some details:

Q: POSIX path doesn’t work right if the file has certain characters in its name — spaces, parentheses, $, *, etc.
A: This is a special case of quoting: you must quote the path to make the shell interpret all the punctuation literally. To do this, use the quoted form of the path. For example, this will work with any file, no matter what its name is:

choose file
do shell script "ls -l " & quoted form of the POSIX path of the result
-- result: "-rw-r--r-- 1 me unknown 1 Oct 25 17:48 Look! a file!"Maybe try using the quoted form function?

kainjow
Feb 28, 2008, 12:40 PM
"this_folder" is not a valid unix path

I didn't mean literally. See HiRez's post for what I meant ;)

todd2000
Feb 28, 2008, 01:37 PM
If it's not a relative path issue, it might be one of the characters in there that it doesn't like, maybe the ! or / characters. This Apple tech note (http://developer.apple.com/technotes/tn2002/tn2065.html) has some details:

Maybe try using the quoted form function?

Im not a programmer, I don't really understand this stuff, my friend gave me that command. What would my command look like if I used the "quoted_form" function?

Basically what Im trying to do it copy every file from a folder EXCEPT files with a .download extension. So far the files that I've been trying to copy don't have any strange characters in them so Im not sure that thats the problem.

jeremy.king
Feb 28, 2008, 02:19 PM
Since you are involving shell in this, why not just put your command in a shell script and call that from your Applescript?

do shell script "sh /path/to/your/shell/script"

Note: the dot should probably be changed to a valid path reference.

Eraserhead
Feb 28, 2008, 02:21 PM
Also AppleScript uses the sh shell rather than bash. See this technote (http://developer.apple.com/technotes/tn2002/tn2065.html) for more info.

todd2000
Feb 28, 2008, 02:30 PM
Since you are involving shell in this, why not just put your command in a shell script and call that from your Applescript?

do shell script "sh /path/to/your/shell/script"

Note: the dot should probably be changed to a valid path reference.

I tried that, and it didn't work either. I can exacute the shell script manually from Terminal, and it works fine, but when I try to call it from my Apple Script it doesn't work, and causes Finder to freeze again.

And for some reason changing the ./ to the actual path makes the script not work, even manually.

Also AppleScript uses the sh shell rather than bash. See this technote (http://developer.apple.com/technotes/tn2002/tn2065.html) for more info.

I've also tried telling it to use /bin/bash, and that didn't help.

Eraserhead
Feb 28, 2008, 02:32 PM
You can stop it freezing Finder by taking it out of the tell application "Finder" block, Finder isn't required to run a shell script.

There is other stuff in the Technote that may help you solve your problem, so its worth reading the whole thing.

todd2000
Feb 28, 2008, 02:51 PM
You can stop it freezing Finder by taking it out of the tell application "Finder" block, Finder isn't required to run a shell script.

There is other stuff in the Technote that may help you solve your problem, so its worth reading the whole thing.

Thank you I got the script from somewhere else, It's not freezing Finder anymore, but it's still not working. I have read the tech note, and tried what they said but still no luck. I made a shell script that I can run from Terminal, and it works perfectly, but not from an Apple Script.

kainjow
Feb 28, 2008, 03:07 PM
You could try this. I haven't tested it though:

on adding folder items to this_folder after receiving added_items
do shell script "find " & quoted form of the POSIX path of the this_folder & " ! -name '*.download' | xargs -I trp cp -pf trp /Users/Todd/Downloads/trp 2>/dev/null"
end adding folder items to

Eraserhead
Feb 28, 2008, 03:16 PM
Applescript using : as its file delimiter and Unix/Terminal using / is probably what is causing this problem.

todd2000
Feb 28, 2008, 03:20 PM
You could try. I haven't tested it though:

on adding folder items to this_folder after receiving added_items
do shell script "find " & quoted form of the POSIX path of the this_folder & " ! -name '*.download' | xargs -I trp cp -pf trp /Users/Todd/Downloads/trp 2>/dev/null"
end adding folder items to

Ok thats the command I tried first, and couldn't get it to work. My friend told me to use the other command.

I am getting close though I managed to get it to work if I put the shell script in the folder that I attached the Script to, and call it from the Apple Script like this

on adding folder items to this_folder after receiving added_items
do shell script "cd /Users/Todd/SafariTemp ; ./tempcopy"
end adding folder items to

Of course that also copys the shell script, and I rather not have the shell script in that folder, but when I try to put the shell script anywhere else, and change the ./ to the actual path, it doesn't work. It only works if I use ./, and run the script from the folder Im trying to search.

todd2000
Feb 28, 2008, 03:22 PM
Applescript using : as its file delimiter and Unix/Terminal using / is probably what is causing this problem.

Im not sure what you mean exactly, but that shouldn't matter anymore since I've made it into a shell script right? Now all Im trying to do is get AppleScript to execute the shell script the way I want.