Command works from Terminal, but not in an Apple Script? Why?

Discussion in 'Mac Programming' started by todd2000, Feb 28, 2008.

  1. macrumors 68000

    Joined:
    Nov 14, 2005
    Location:
    Danville, VA
    #1
    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?
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Are you sure the "./" is right? That is a relative path, you may need to put in this_folder in its place.
     
  3. thread starter macrumors 68000

    Joined:
    Nov 14, 2005
    Location:
    Danville, VA
    #3
    ./ 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.
     
  4. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #4
    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?
     
  5. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    I didn't mean literally. See HiRez's post for what I meant ;)
     
  6. thread starter macrumors 68000

    Joined:
    Nov 14, 2005
    Location:
    Danville, VA
    #6
    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.
     
  7. macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #7
    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.
     
  8. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #8
    Also AppleScript uses the sh shell rather than bash. See this technote for more info.
     
  9. thread starter macrumors 68000

    Joined:
    Nov 14, 2005
    Location:
    Danville, VA
    #9
    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.

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

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #10
    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.
     
  11. thread starter macrumors 68000

    Joined:
    Nov 14, 2005
    Location:
    Danville, VA
    #11
    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.
     
  12. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #12
    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
     
  13. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #13
    Applescript using : as its file delimiter and Unix/Terminal using / is probably what is causing this problem.
     
  14. thread starter macrumors 68000

    Joined:
    Nov 14, 2005
    Location:
    Danville, VA
    #14
    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.
     
  15. thread starter macrumors 68000

    Joined:
    Nov 14, 2005
    Location:
    Danville, VA
    #15
    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.
     

Share This Page