[LEARNING] Writing an Alfred Extension – Applescript
Aug, 21

By marcel - tags: , , , ,

Alfred is a productivity application for Mac OS X, which aims to save you time in searching your local computer and the web. Whether it’s maps, Amazon, eBay, Wikipedia, you can feed your web addiction quicker than ever before.

This post is a short insight on how to write an Extension for Alfred.

A few days ago i had the idea for a simple extension. Putting your Mac to sleep with one simple command, instead of scheduling it via the System Preferences Energy Settings. Simply typing „s 20“ into Alfred to put my Mac to sleep within 20 minutes and if iTunes is running, slowly fade it out.

What tool?

Therefor first of all I had to choose wheter to do it with Applescript or with a shell script.

This decision fell very fast. I had no clue how to make iTunes fade out with a shell script – and I did not find anything on google. So Applescript was my choice.

What do you need?

Google – of course you need google. If your looking for help, Google is always the first place to ask.
Via Google I got very fast into the Basics of Applescript. It’s a very simple language. You write, what you want to do. It’s more like a dialog, not like a programming language.

Applescript. The standard Applescript-Editor is within the Utilities folder located in you Applications folder. Open it, look at it, play around with it.

Coding

Applescript is very simple, as already mentioned. With Applescript you talk to your computer – more or less. For example: I want to delay the script for »sleepDistance« (an integer) minus »fadeTime« (an integer) if the Boolean »delayIt« is true. The code looks like this.

if delayIt is true then delay sleepDistance - fadeTime

Very simple and good to understand, isn’t it? Another example. I want to set the Boolean »delayIt« to false.

set delayIt to false

And again. Very simple and straight – just „say“ what you want. And controlling iTunes is also very simple. If you want to change the iTunes volume to 50%, simply tell iTunes to do so:

tell application iTunes
set sound volume to 50
end tell

And what’s really nice about it is, that you can also run shell scripts within Applescript.

do shell script "pmset sleepnow"

This snippet puts your mac to sleep instantly.

Help

Some places where you can get help, if you plan on playing around with it.
Macuser community
The Applescript Guide – definitely
Directly at Apple

Conclusion

Applescript is a nice little language. You can get things done very fast with it, but it’s also limited. You simply can’t do anything with it. However for me it worked out, i’ve got my nice little script for Alfred, which puts my mac to sleep after »n« minutes.

Download

You can download the Extension right here. Also you can download the source of the Applescript right here.

About marcel

Studying Interaction design at the university of applied sciences Schwäbisch Gmünd. Loves HTML, CSS, Processing and fancy GFX - www.blaufasan.de