Alvin Alexander | Java, Scala, Unix, Perl, Mac OS X

For better or worse, I sold half of my Apple stock this morning. I bought the stock earlier this year around $85, and it's currently at $165. Last week it was at $187, and I thought "I need to put in a sell order around $175" in case it went down, and sure enough it lost $23 while I wasn't paying attention. So this morning I sold off enough to cover my initial investment, and I'm leaving the rest in AAPL for the long term.

Mac SVN FAQ: How do I install Subversion (SVN) on a Mac OS X system?

SVN commands reference FAQ: Can you share some SVN command examples (an SVN commands cheat sheet)?

I just got SVN (Subversion) installed on my MacBook Pro, so I'm throwing these SVN commands out here as a reminder to myself. This is a list of SVN command examples that can be used from the svn command line client:

I just got a Subversion (svn) server running on my MacBook Pro, and thought I'd put this svnserve man page out here as reference:

If you ever want/need to use AppleScript to drive another program that doesn't have AppleScript support, there are a few things you can do. Here's a snippet of code where I'm opening new tabs in Safari, and typing text into the location field:

AppleScript keystroke FAQ: Can you share an AppleScript keystroke/keyboard example?

When I created my AppleScript program to open a list of URLs in Safari I needed a way to type the URLs into Safari. That turned out to be pretty easy, I did it like this:

keystroke ""

The much harder part was figuring out how to simulate typing the Enter (or Return) key from AppleScript. Without further ado, here's the answer:

AppleScript FAQ: How can I run an AppleScript script from the Mac Unix terminal (Unix command line)?

A cool thing about Mac OS X is that you can run AppleScript programs from the Unix shell. (Well, I guess it's cool if you're a Unix user.)

Running an AppleScript program from the Unix shell turns out to be surprising easy. For instance, if my current working directory has a script named OpenUrls.scpt in it, I can run that script from the command line like this:

Once you know that you can run an AppleScript program from the Unix command line it's easy to run it at scheduled intervals using the Unix cron facility. Here's an example of how to run my Safari script at the 7:30 a.m. every day.

30 7 * * * /Users/al/tmp/OpenUrls.scpt

I'm not going to get into the cron facility in any detail here, I just wanted to show how this is done. I will say that I modify the cron script by using this command:

AppleScript application FAQ: How do I save an AppleScript script as an AppleScript application?

I just finished writing an AppleScript program to open multiple URLs in multiple tabs in Safari, and -- being incredibly lazy -- I want to put this script on my desktop and have it run whenever I double-click it. That's all the work I'm willing to put into it, no other approach will do.

Well, I set out to write a simple tip on how to activate a Mac application using AppleScript, and I ended up writing a program to open a list of URLs in separate tabs in Safari using AppleScript. (Yeesh, I really took a detour. Oh, well.)

Without any further ado, here's the source code for this AppleScript program:

AppleScript “list” FAQ: How do I do something for every item in an AppleScript list? (or, How do I loop over an AppleScript list?)

Loop over an AppleScript list with the every keyword

I've shown examples of the AppleScript every keyword in other posts, so rather than create anything new, here's a repeat of one of those examples:

A quick note about getting the current time in AppleScript in two different formats. This command

set t to (time string of (current date))

yields something like this result (depending on the time of day):

"12:48:35 PM"

but this command

set t to (time of (current date))

yields something like this result:


(That's the number of seconds since midnight, in case you were wondering.)

AppleScript command FAQ: Can you show an example of the AppleScript delay command?

If your AppleScript is running too fast you can slow it down with the delay command, like this example:

display dialog 1 buttons {"OK"}
delay 3
display dialog 2

When you click OK after the first dialog, your AppleScript program goes to sleep for three seconds, then displays the second dialog.

Hopefully you'll have more practical uses for this AppleScript sleep/delay command my sample code. :)

AppleScript FAQ: How can I customize AppleScript dialog boxes?

When you display an AppleScript dialog box with the display dialog code, you don't have to just use the standard Cancel and OK buttons, you can change the text to something more meaningful to your specific prompt. For instance, suppose you want to ask someone their age range:

display dialog "How old are you?" buttons {"Less than 1", "Older than 1"}

(Okay, not the greatest example in the world, but that's all I've got today.)

AppleScript while loop FAQ: Can you share some examples of the AppleScript while loop syntax (the AppleScript repeat while syntax)?

Instead of using a keyword like "while" to create loops, AppleScript uses the "repeat" keyword to create loops. There are several versions of this syntax, as follows.

AppleScript repeat syntax: Repeat X times

Here's how to repeat an AppleScript command X number of times:

I've shown some basic AppleScript boolean-oriented syntax like this in another post:

set a to true
if a then
  display dialog a
end if

That code doesn't do too much, especially because you know the variable a is set to true, but it becomes a little more helpful when you don't know if a is true or false:

if a then
  -- do something really important here
end if

You can perform other if/then checks based on numerical tests, like this:

It's easy to work with boolean (true/false) variables in AppleScript. Here's an example of how you assign a boolean to a variable:

Variable names in AppleScript are pretty flexible, especially when compared to other languages. Variable names can consist of letters, numbers, and underscores, with the only limits being (1) the first character in the name can't be a number, and (2) the name itself must be less than about 250 characters (I don't know the exact limit, but you really don't want variable names anywhere near that length limit).

In the latest release of Dr. Dobbs, Michael Swaine reports that "after two years trying to make Ruby on Rails do what he wanted, Derek Sivers went back to PHP and finished the job in two months." Actually, here's a direct link to Derek Sivers' post, including his seven reasons he switched back from Rails to PHP, and what he learned from Rails.


I was reading Dr. Dobbs last night, and they referenced this story about how quantity ends up producing quality, at least when it comes to humans creating things.

The lesson goes like this: A ceramics teacher divides a class into two teams, then tells one team they'll be graded by sheer quantity, while the other side has to produce one great pot. In the end the Quantity Team delivers all the best quality pots.

I've found this to be true with new programmers. All newbies want to learn about things like design patterns, but what they really need to do is just sit down and write a lot of code.

This reminds me of the quote, "One learns by doing the thing." I don't know for sure who said that — possibly Sophocles — but I remember reading it in a Thermodynamics book in college, and I've never forgotten it.