Search code examples
terminalapplescriptcd

Change directory and applescript terminal command


I have tried the following but cannot seem to get this to work:

do script "cd ~/desktop/test; for x in ls -1 | sed -e 's/^\(.\).*/\1/' | sort -u; do mv -i ${x}?* $x done"

I'm wanting to perform this command in applescript. I run this in applescript and I get an error regarding "" marks but am not sure how to correct it. I'm a complete newbie to applescript. willing to learn just a little lost.

Thanks


Solution

  • Note that in AppleScript, whenever you pass a shell script command with \, use \\ to represent it.

    As:

    do shell script "cd ~/Desktop/; for x in `ls -1 | sed -e 's/^\\(.\\).*/\\1/' | sort -u`; do echo ${x}?* $x; done"
    

    Also, it uses for x in `command`, instead of for x in command. It's because `` treats everything inside it to be a command, and expects the result of this command, just as $():

    do shell script "cd ~/Desktop/; for x in $(ls -1 | sed -e 's/^\\(.\\).*/\\1/' | sort -u); do echo ${x}?* $x; done"