Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

todd2000

macrumors 68000
Original poster
Nov 14, 2005
1,626
11
Danville, VA
If I type this command into the terminal, it works perfectly.

Code:
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:

Code:
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

Moderator emeritus
Jun 15, 2000
7,958
7
Are you sure the "./" is right? That is a relative path, you may need to put in this_folder in its place.
 

todd2000

macrumors 68000
Original poster
Nov 14, 2005
1,626
11
Danville, VA
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

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
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 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:

Code:
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?
 

todd2000

macrumors 68000
Original poster
Nov 14, 2005
1,626
11
Danville, VA
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 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

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
Since you are involving shell in this, why not just put your command in a shell script and call that from your Applescript?

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

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

todd2000

macrumors 68000
Original poster
Nov 14, 2005
1,626
11
Danville, VA
Since you are involving shell in this, why not just put your command in a shell script and call that from your Applescript?

Code:
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 for more info.

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

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
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

macrumors 68000
Original poster
Nov 14, 2005
1,626
11
Danville, VA
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

Moderator emeritus
Jun 15, 2000
7,958
7
You could try this. I haven't tested it though:

Code:
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

macrumors G4
Nov 3, 2005
10,434
12,250
UK
Applescript using : as its file delimiter and Unix/Terminal using / is probably what is causing this problem.
 

todd2000

macrumors 68000
Original poster
Nov 14, 2005
1,626
11
Danville, VA
You could try. I haven't tested it though:

Code:
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

Code:
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

macrumors 68000
Original poster
Nov 14, 2005
1,626
11
Danville, VA
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.