The Wonders of AppleScript, Part 1 The Wonders of AppleScript, Part 1 

Introduction

I recently decided to tackle a new project: reduce the amount of work to start a Django project. I always find myself having to follow the same routine:

  1. Create the virtual environment
  2. Install Django and South
  3. Set up the folder layout
  4. Get JS libraries and HTML boilerplate

I have gotten really tired of doing all of this, so I looked into the simple scripting language that is provided by Apple on Mac OS X. What I have discovered is that AppleScript is a very simple and powerful tool that all developers on Mac OS X should pick up.

AppleScript

"AppleScript is primarily an inter-application processing system, designed to exchange data between and control other applications in order to automate repetitive tasks." —Wikipedia (http://en.wikipedia.org/wiki/AppleScript)

When you are writing AppleScript, you will quickly realize that the syntax is very close to the English language. The following is the typical “Hello, World” example:

display dialog "Hello World"

or:

display alert "Hello World"

As you know, variables are essential to all programming languages. To declare variables in AppleScript you use the keyword set:

set myVariable to "Hello"
set myOtherVariable to "World"

You can then use your variable like you would in any other language:

display dialog myVariable & ", " & myOtherVariable

My Django Project Script

After some trial and error and through the use of the documentation, I went ahead and developed my simple script to create Django projects. The script itself is not done and I will be adding more features. For example, downloading the required JS libraries, adding individual apps, and modifying the required files (e.g. settings.py) when certain changes are made. Here is the AppleScript for Project Boilerplate (also on github)

-- Project Boilerplate
-- Written by Omar Estrella
-- Version 0.1
-- Licensed under the WTFPL

set the base_folder to choose folder with prompt "Choose base directory…"
set base_folder to quoted form of the POSIX path of base_folder

-- Choose the type of project we will create
set project_type to choose from list {"Django", "Ruby on Rails"} with title "Choose a project type…" with prompt "Project Type" default items "Django" OK button name "Select" without multiple selections allowed and empty selection allowed

-- Choose the name for the project
set project_name to text returned of (display dialog "Project name" default answer "Project" buttons {"Set"} default button "Set" with title "Choose a project name…" without hidden answer)

-- Handle the Django project
if project_type contains "Django" then
    -- Create VE
    do shell script "cd " & base_folder & "; /usr/local/bin/virtualenv " & project_name & " --distribute --no-site-packages"

    -- Activate VE and install Django and South
    do shell script "cd " & base_folder & "/" & project_name & "; source bin/activate; pip install django south"

    -- Create local django project
    do shell script "cd " & base_folder & "/" & project_name & "; source bin/activate; django-admin.py startproject run"
else
    display alert "Cannot continue…"
end if

New to this

I’m sort of new to this whole tumblr and blogging machine. I’ll probably be using it to document things I work on or various other random topics.

Thats it for now.