Applescript - Terminal.app - problem using "cd"

Discussion in 'Mac Programming' started by aplnub, Jun 13, 2014.

  1. aplnub macrumors regular

    Joined:
    Nov 16, 2008
    #1
    I have an Applescript that opens the Terminal, changes to a directory, then dumps the filenames in that directory to a text file.

    Here is what the example script looks like. This works no problem.

    tell application "Terminal"
    activate
    delay 1
    do script "cd /Users/john/Desktop"
    delay 1
    do script "ls > test.txt" in window 1
    delay 1
    quit "Terminal"
    end tell


    ----

    This script does not work and I assume it is because my business dropbox folder has a space in it in addition parentheses. This is the line that is giving me problems.

    do script "cd /Users/john/Dropbox\ \(MacRumors\)/Racecar"

    I cannot rename the Dropbox folder because Dropbox evidently has to use this naming convention. Regardless, how can I make this work as is?


    tell application "Terminal"
    activate
    delay 1
    do script "cd /Users/john/Dropbox\ \(MacRumors\)/Racecar"
    delay 1
    do script "ls > test.txt" in window 1
    delay 1
    quit "Terminal"
    end tell

    ---


    Thanks for any help!
     
  2. MCroft macrumors member

    Joined:
    Feb 24, 2003
    Location:
    Houston
    #2
    have you tried enclosing the path name in escaped quotes?

    do script "cd \"/Users/john/Dropbox\ \(MacRumors\)/Racecar\""

    Or just combine it all into one command?

    do script "ls \"/Users/john/Dropbox\…\\" > /path/to/listfile.txt"

    I also tend to use Applescript to return the results of a shell script as a variable, so I can inspect it, etc. Then you can write it out separately.
     
  3. aplnub thread starter macrumors regular

    Joined:
    Nov 16, 2008
    #3
    Thank you. I just tried your suggestions but the extra "" don't help. I have tried everything. :confused:

    If I drag the folder from the finder to the Terminal it works and that is the line it gives me. Applescript won't accept it. I don't get why it doesn't work.

    I like combining them into one line, btw.
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Let's try things one step at a time.

    Step 1: Copy and paste this exact command-line into a Terminal window:
    Code:
    ls -l ~
    
    Then copy and paste the complete output into a post here. Use CODE tags on the pasted output, so it retains its formatting.

    The above command should list the contents of your home folder. One of the items listed should be your dropbox folder.

    After I see the complete listed output, I can tell you another Terminal command to try.


    Ultimately, it's not necessary to "tell Terminal" anything. AppleScript has a do shell script command that's entirely capable of running commands without any need for Terminal or Terminal windows. However, figuring out exactly what command to run is much better suited to Terminal. That's why I'm asking for Terminal results, even if eventually there's no "tell Terminal".
     
  5. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #5
    I'm not up on Applescript. If each "do shell" command spawns it own shell, does the command, and exits then issuing a 'cd' shell command will change the current working directory for that shell. In other words, will that command actually affect the CWD of the script process? My guess would be that it does not. Therefore, the 'cd' command isn't really doing anything useful.

    Something to keep in mind.
     
  6. Partron22 macrumors 68000

    Partron22

    Joined:
    Apr 13, 2011
    Location:
    Yes
    #6
    Instead of
    Code:
    "cd /Users/john/Dropbox\ \(MacRumors\)/Racecar"
    use
    Code:
    set aaa to quoted form of /Users/john/Dropbox (MacRumors)/Racecar"
    
    tell application "Terminal"
    	activate
    	delay 1
    	do script "cd " & aaa
    	delay 1
    	--do script "ls" in window 1
    	do script "ls > test.txt" in window 1
    end tell
    
     
  7. MCroft macrumors member

    Joined:
    Feb 24, 2003
    Location:
    Houston
    #7
    This script works for me.

    set MyVar to do shell script "ls -l \"/Users/mcroft/my file\""
    display dialog MyVar


    Maybe there's a difference between do shell script and do script?
     
  8. aplnub thread starter macrumors regular

    Joined:
    Nov 16, 2008
    #8
    Thanks everyone for the help.

    Here is the output you requested.

    Code:
    total 0
    drwx------   3 john  staff   102 Jun  5 17:22 Applications
    drwx------+ 18 john  staff   612 Jun 13 21:27 Desktop
    drwx------+ 50 john  staff  1700 Jun 11 22:11 Documents
    drwx------+  7 john  staff   238 Jun 11 22:10 Downloads
    drwx------@ 17 john  staff   578 Jun 13 13:24 Dropbox (MacRumors)
    drwx------@ 59 john  staff  2006 Jun 13 16:02 Dropbox (Personal)
    drwx------@ 56 john  staff  1904 Jun  7 09:49 Library
    drwx------+  3 john  staff   102 Apr 23 17:51 Movies
    drwx------+  5 john  staff   170 Apr 23 22:27 Music
    drwx------+ 10 john  staff   340 Jun  7 20:21 Pictures
    drwxr-xr-x+  4 john  staff   136 Apr 23 17:51 Public
    drwxr-xr-x@ 24 john  staff   816 Jun 12 09:53 Transporter




    ----------

    John:~ eric$ cd /Users/john/Dropbox (MacRumors)/Racecar
    -bash: syntax error near unexpected token `('





    ----------

    I will be away from my computer where I am trying to script this for the weekend. I will return on Monday.

    If I can do this without the Terminal that is great. I just started there to make sure the ls > test.txt worked then I just stuck with it. Now I am curious why this won't work. I assume it comes down to the space in the Dropbox folder name that has to be there.
     
  9. Ainze macrumors regular

    Ainze

    Joined:
    Feb 28, 2010
    #9
    Code:
    set ls_folder to quoted form of "/Users/username/Desktop/Folder with space"
    
    do shell script "ls " & ls_folder & " > ~/Desktop/test.txt"
    seems to work fine.

    Or just
    Code:
    do shell script "ls \"/Users/username/Desktop/Folder with space\" > ~/Desktop/test.txt"
    if you prefer one line.
     
  10. aplnub thread starter macrumors regular

    Joined:
    Nov 16, 2008
    #10
    Can you name a Folder "Test (Folder)" on your computer and then try this?

    It may be the space and parentheses. I can't get it to work.
     
  11. Ainze macrumors regular

    Ainze

    Joined:
    Feb 28, 2010
    #11
    Yep, works perfectly. The trick is the escapes (the backslashes "\") around the test folder path. By putting them in, it tells Applescript that the quote marks are part of the literal text, not the code, and so the arguments go through to the shell correctly.
    (My explanation may not be that good, but the code works fine!)

    Code:
    do shell script "ls [COLOR="Red"][B]\"[/B][/COLOR]/Users/username/Desktop/Test (Folder)[COLOR="red"][B]\"[/B][/COLOR] > ~/Desktop/test.txt"
     
  12. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #12
    Please try exactly what Ainze posted. It should work. If it doesn't, then post exactly what you tried, and the exact text of the error message.

    If you think you tried the same thing by following post #2, you didn't. That one had backslashed inside the escaped quotes, which is not what Ainze posted.

    Accuracy and correctness are very important here. Sadly, some of the suggestions for you to try are simply incorrect. I see unnecessary (and thus error-causing) backslashes, missing quote markes, to name but two. What Ainze posted not only looks correct to me, I actually tried it, exactly as posted, after making a folder with the right name, and it worked fine.
     
  13. aplnub thread starter macrumors regular

    Joined:
    Nov 16, 2008
    #13
    The do shell script worked.

    I was missing the the "\" around the path and found one other small error.

    Thank you! Now on to the next thread with applescript.

    I really appreciate everyone hanging in there with me (special thanks to chown33 for riding my butt and Ainze).
     
  14. Ainze macrumors regular

    Ainze

    Joined:
    Feb 28, 2010
    #14
    You're welcome. We're all here to help each other :)
     
  15. chrfr macrumors 603

    Joined:
    Jul 11, 2009
    #15
    I'm a bit late to this, but note that in a shell script, generally you'd rarely want to use cd (Ainze's script is the correct way to do it) but rather specify paths of your commands, input, and output for simple tasks like this one. The fewer lines and commands in a script, the easier it'll be to fix when you have to debug one.
     

Share This Page