Scripting 101: A Very Gentle Introduction to AppleScript

scriptingWe’ve written before at ProfHacker about the power of scripting tasks: Letting your computer take over some of the repetitive things that each of us do regularly daily hourly. Recently, for example, Jason Heppler explained in a guest post “Using AppleScript to Automate Notetaking,” and Konrad wrote last year about “AppleScript for Complex Repetitive Tasks.”

It’s no accident that Jason and Konrad wrote about AppleScript, as OS X’s scripting language offers a user-friendly, yet powerful way to get your computer to help you. This is especially true when combined with the Automator, which offers a drag-and-drop interface for scripting workflows.

In today’s post, I wanted to show how easy it is to get into scripting by demonstrating a much simpler use case than did Jason or Konrad. This script will grab whatever’s in the clipboard, and append it to the end of a specific text file.*

The first step in using AppleScript is to open the AppleScript editor, which is probably in the Utilities folder within Applications. To get an idea for what kinds of things you might be able to do, select “Open Dictionary” from the File menu. You’ll see a list of applications. When you click on the list, you’ll get a list of different commands that you can address to that application. Also, you should probably open the AppleScript Language guide, which explains the generic set of commands that are always available. (Google is also your friend. For example, I found this this article on “Working with Text” helpful when I was starting out.)

In English, what I wanted to do could be described this way: “When I’ve copied a URL, open a specific text file and add it to the end.”

Here’s the script that I ended up with:

set theText to the clipboard
set theFilePath to (“/Whatever/The Path/Actually Is”) & “Whatever the Filename is.txt”

set theFileReference to open for access theFilePath with write permission
” to theFileReference starting at eof
write theText to theFileReference starting at eof
close access theFileReference

Here’s how the script works:

  1. The first line creates a variable called “theText,” and assigns to that variable the contents of the clipboard.
  2. The second line goes and finds the file in question, first by giving the actual path to the file, and then the specific filename.
  3. The third line gives the script permission to write–that is, to add data–to the file.
  4. The fourth line adds a carriage return to the end of the text file, so that whatever’s on the clipboard will be entered on a new line.
  5. The fifth line appends “theText” to the end of the text file.
  6. The last line closes the file.

What’s nice about having this as a script is that I don’t have to manually invoke TextEdit** whenever I want to save a link. You can test the script directly from the AppleScript Editor, so you can verify that it works. The next step is to turn this script into a service, so that it’s available in any application.

To turn a script into a service, just open the Automator, which lives in the Applications folder. Select “New” from the File menu, and select “Service” from the template chooser that pops up. In Automator, find “Run AppleScript,” like so:

Then, paste in your script!

At this point, you should be able to save the script, and it will appear in the “Services” submenu from the File menu. It’ll work. But if you’ve gone to the trouble of automating this simple process, you might as well make a hotkey combination to invoke it, right? Brian explained how to do this last year, but the tl;dr version is: In System Preferences, under Keyboard, select Services. Highlight your newly-created service, and click the “add shortcut” button that appears. Follow the prompts, and you’re done!

This has been, hopefully, a pretty gentle introduction to the world of scripting. The basics are incredibly simple to learn, and the effort required really is repaid quickly. Even this example, minor as it is, reduces interruptions and screen clutter when I’m trying to do other things. I don’t have to switch out of one app, open the text file, paste in a link, save the text file, then switch back to the application I was using.

In the new year, I plan to demonstrate some other simple use cases for scripting, and to build up to more complex examples.

*I use the file to save links that I might send to my wife, so I can send ‘em as a batch.

** I use nvALT myself. But I like to keep some stuff separate–it’s why I don’t save her links to Pinboard or Instapaper. It’s possible I have issues.

Photo “Script” by Flickr user eelke dekker / Creative Commons licensed BY-2.0

Return to Top