I have the following terminal command that I want to automate using applescript. I also would like to have a command to cd directories to the folder where I want to apply the command first. After cd to the folder that holds the .pdf files. This code looks at the first letter of the file name and based on that sorts the files into the alphabet folder corresponding to the first letter of the file.

for x in `ls -1 | sed -e 's/^\(.\).*/\1/' | sort -u`; do
mv -i ${x}?* $x

How would I go about doing this? I'm a newbie to this. Any insight would be greatly appreciated. I'm willing to learn Applescript, however, I'm not sure where to start to implement this.


  • What is driving your requirement for an AppleScript version? I've never really found a situation where I need to use an AppleScript instead of a shell script on OS X (specifically, you can run shell scripts directly using Automator if that's what you're trying to do). If you just want to call this script from an AppleScript (rather than rewrite it in AppleScript), see cyberciti.biz/faq/mac-osx-applescript-run-shell-script
    – calum_b
    Feb 1, 2016 at 10:31
  • @scottishwildcat AppleScript should primarily be used, either when GUI scripting is needed, or when communication between processes is desired (if those processes support AppleScript). Beyond that, I'd agree. AppleScript is generally not the best tool for the job. Feb 1, 2016 at 11:41
  • Thank you for the feedback. So to use Automator to do this, do I just paste this code in a workflow? Not sure how to set this terminal command up in Automator. Thanks
    – J Paul
    Feb 1, 2016 at 23:56

1 Answer 1


You can execute shell script directly on OS X, so there's generally no good reason to do that. However, if you really feel like it, you can use do shell script "script" to execute a shell script in AppleScript.

  • Hi Thanks for the response. I have tried the following but cannot seem to get this to work: do script "cd ~/desktop/hazel_organization/test/test1; for x in ls -1 | sed -e 's/^\(.\).*/\1/' | sort -u; do mv -i ${x}?* $x done"
    – J Paul
    Feb 6, 2016 at 10:43
  • @JPaul I would check and make sure all characters that need to be escaped are escaped: what happens when you try to run it?
    – JMY1000
    Feb 6, 2016 at 19:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .