Blog Archives

getting to grips with AppleScript

Wall and paper stencil

Learning AppleScript is probably the second most productive thing you can do (the first is learning the Terminal) to improve your Mac experience. You know all those utilities that you see in the Mac App Store, on MacUpdate and so on, with developers charging anything from 99 cents to $20? Well, many of those are just doing simple jobs that you can actually script yourself for free with a little learning of OS X’s unique scripting language (and, indeed, some of those apps have been built in exactly this way).

To keep this post short and practical, I’m going to leave aside the wider discussion of what AppleScript is (and isn’t), what it can (and can’t) do and all manner of other interesting but theoretical things, and instead give you a taste of what you can do with it. I’ll give you some references at the end where you can find out more and learn everything you need.

Let’s get straight to it. Open up the AppleScript Editor by opening the Spotlight search bar and typing Apples. Hit ‘return’ and you’ll be faced with a new editor window. Let’s type something in it.

tell application "Finder"
display dialog "What's your name?" default answer "" with icon note
set myName to the text returned of the result
delay 0.5
display dialog "Hi there, " & myName & "! Welcome to AppleScript!" with icon note
end tell

You could just copy and paste this into the editor, but I’m going to recommend that you don’t. There’s a good reason to take the tedious route and type it in yourself. Like learning a human language, learning a computer language requires using it, and using it repetitively. As you type in the language, you’ll get a feel for its syntax that you won’t get if you just copy and paste. And, unlike a real human language, learning a computer language’s syntax is pretty much the whole battle of mastering it.

After you’ve finished typing, press ‘Command-K’ on the keyboard. If you typed everything correctly, you see the script change into a multi-colored jamboree, like this:

applescript

If you weren’t so lucky, examine what you typed against what’s on the page here. Part of the frustration of any computer language is rooting out typos! Eventually, you get a feel for it and start to learn where to look first, based on the error messages you see. For now, you’ll have to peck and hunt (if you get really fed up, you can always go the cut-and-paste route!).

Assuming you’ve got your script to compile, now it’s time to run it and see what it does. Hit ‘Command-R’ (you can of course use the icons in the toolbar for compiling and running, too) and you should get this:

what's your name?

So go ahead, type your name!

OK, you’re getting the idea. Let’s try something different. Press Control-N to open up a clean editor window and enter this:

say "What's your name?" using "Vicki"
display dialog "My name is " default answer ""
set myName to the text returned of the result
say "His name is " & myName using "Vicki"
say "Welcome to AppleScript, " & myName using "Alex"

As before, press ‘Command-K’ to compile and ‘Command-R’ to run.

As you can see (or hear!), you can even have your computer continue a dialogue (with or without you!) using OS X’s many voices.

It’s worth noting that this feature is extremely useful if you’re learning a foreign language.

The Voices options in System Preferences > Dictation & Speech | Text to Speech | System Voice include many optional voices that you can download that will speak foreign text. You can paste the target text into a dialog box (like the ones you just created), and then listen and practice your language skills as repetitively as you desire. For those wanting to learn Thai, for example, download the voice “Narisa”. You can paste Thai script from the web or from the Dictionary.app (if you have the Thai extension installed) into a dialog box and have “Narisa” say it in a very passable Thai accent. Great for learning!

One last one. How about your own screencapture program? Tired of remembering those shortcut keybindings, or having to fire up Preview or SnagIt for the occasional screen grab? Why not have your own app in the Dock that captures the screen with a single click? Here’s how:

Open a new editor window and enter this (in this case, you might want to copy and paste it, for reasons I’ll explain shortly):


do shell script "screencapture -x ~/Desktop/" & time string of (current date) & ".png"

Do the Command-K thing, but then this time do Command-S instead of Command-R. From the resulting save box, change the File Format near the bottom of the box from ‘Script’ to ‘Application’. Give it a fancy name (ScreenGrab, say?) and save it in your Applications folder.

applescript save as

Once that’s all done, go to your Apps folder, grab the ScreenGrab.app and drag it to the Dock. Clicking on it puts a timestamped screenshot on your Desktop. If you’ve got multiple spaces open, flip between them clicking the ScreenGrab app. That’s one easy way to get a record of your entire set up! Cool, huh? (Don’t forget you can easily change the icon in the Dock for something more to your taste, as I explain here. Also, if you don’t like the / delimiters in the file name, use this version for your app.)

That last little script demonstrates one of AppleScript’s most powerful features: the ability to run other scripts (and apps). The command in the last script (and the reason why I suggested you paste it) was actually a Bash shell command (aka Terminal command), and we know those are very easy to mistype! AppleScript can actually run the commands of many apps from within its own scripts, putting the power of those apps at your disposal (and that includes some apps you’re very likely familiar with, like Word and Excel).

I hope this short intro has given you a taste for exploring AppleScript and finding out more. It’s an incredibly powerful language that you can use to enormous advantage, and profit. In order to do that, you’ll need to go on a learning adventure, but I promise, the following sources will make that relatively painless!

If you’re absolutely brand new to AppleScript, then the must-have starter’s book is

AppleScript 1-2-3

Once you’ve got through that you’ll be able to pretty much teach yourself the rest, picking it up from sources like

Macscripter.net

Other references you should consult once you’re up and running are

Learn AppleScript (Sanderson)

and don’t forget Apple’s own free guide:

AppleScript Language Guide which you can also download as a PDF for offline use.

Finally, my Mac OS X Technologies User Tip contains some of the above links as well as others that may be of interest.

Happy scripting! 🙂






Featured picture: wall and paper stencil by -endlesshate

need a manual for Lion?

Got a new Mac and feeling like you’re on a whole different planet? Providing user manuals with computers isn’t really very Apple somehow 😉 …after all, the OS is supposed to be so intuitive and easy to use we shouldn’t need one…

However, clearly more than a few have been having problems getting used to their new Lion installation as Apple are now prominently displaying two beginners help guides on the ASC support forum.

Have a look at Mac 101 if you’re new to computers in general or haven’t really used one for a while.

If you’re pretty computer savvy but have just switched over from a lifetime of Windows to your first Mac, then Switch 101 will clue you into both some of the major differences and how to accomplish familiar Windows tasks in your new Mac OS X environment.

To keep up to date, troubleshoot, or find answers to specific questions not covered in the above materials, be sure to visit http://www.apple.com/support/lion/. You can also find this page from the  menu at the top left of your screen. Click

 > About This Mac and then ‘More Info…’:

Over on the right of the next panel, click the ‘Support’ button:




You can access both the online Lion manual (indicated in blue), and also a pdf manual (indicated in purple) for your computer from here.

Enjoy exploring! 🙂

Struggling with the basics? Don’t be shy, let us know in the Comments below! 🙂

featured picture The Close Light by *qaz2008

how to unfreeze your iPad

iPads are so easy to use, why bother with the manual? 😉 The chances are though, that at some point you’re either going to find that an app freezes on your screen or your whole tablet becomes unresponsive. Don’t panic, the answer’s simple:

First, be sure that it’s connected to a power source. The most common reason for iPads not working is people don’t realise they’re out of battery! Otherwise try these:

If it’s just a particular App that’s frozen on your screen:

— Hold down the ‘sleep/wake’ button (top right, back edge) for about 5 seconds until the slider appears. Release the ‘sleep/wake’ button. Now hold down the ‘Home’ button (bottom front, centre) until you see your Home screen.

If your whole machine is unresponsive, then do a restart:

— Hold down the ‘sleep/wake’ button for about 5 seconds until the slider appears. Slide it to ‘Off’. Then hold it down again until the Apple logo appears showing that the iPad is restarting.

If that doesn’t work, do a hard reset:

— Hold down the ‘sleep/wake’ button AND the ‘Home’ button simultaneously for about 10 seconds or until the Apple logo appears.

*For more serious problems with your iPad, such as continual restarting or no home screen, have a look here.



featured picture: ice crystals by Typen

%d bloggers like this: