Skip to Content
AppleScript: The Definitive Guide
book

AppleScript: The Definitive Guide

by Matt Neuburg
November 2003
Beginner to intermediate
480 pages
15h 3m
English
O'Reilly Media, Inc.
Content preview from AppleScript: The Definitive Guide

Handlers as Values

A handler is a datatype in AppleScript. This means that a variable’s value can be a handler. In fact, a handler definition is in effect the declaration (and definition) of such a variable. (That variable’s status, as we have already seen, is essentially the same as that of a property.) The variable’s name is the name of the handler, and its value is the handler’s bytecode, its functionality.

A handler may thus be referred to like any other variable, and you can get and set its value. For example:

on sayHowdy(  )
        display dialog "Howdy"
end sayHowdy
set sayHello to sayHowdy
sayHello(  ) -- Howdy

In that example, we stored a handler as the value of a variable, and then called the variable as if it were a handler! This works because the variable is a handler.

The value of a handler can also be set. No law says you have to set it to another handler. For example, you could do this:

on sayHowdy(  )
        display dialog "Howdy"
end sayHowdy
set sayHowdy to 9
display dialog sayHowdy -- 9

You can set one handler to the value of another, in effect substituting one entire functionality for another. Of course, the functionality has to be defined somewhere to begin with. For example:

on sayHowdy(  )
        display dialog "Howdy"
end sayHowdy
on sayHello(  )
        display dialog "Hello"
end sayHello
set sayHello to sayHowdy
sayHello(  ) -- Howdy

Handlers as Parameters

At this point, you’re probably thinking: “Wow! If I can store a handler as a variable, I can pass it as a parameter to another handler!” ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

AppleScript in a Nutshell

AppleScript in a Nutshell

Bruce W. Perry

Publisher Resources

ISBN: 0596005571Catalog PageErrata