WWDC2003 Session 623
Transcript
Kind: captions
Language: en
ladies and gentlemen please welcome
south ago in the applescript product
manager for Stan wow this is uh whoa
okay let me adjust my voice yes Sal is
very thundering at times how's that
they're better okay this is quite a
response I just confirmed my gut
instinct that Apple scripts on fire
again another year of Apple script just
thundering forward because of the
interest I can see a lot of these faces
I haven't seen before so either you're
in the wrong session this is about
bird-watching right okay or you're
interested in some of the core
technologies that you haven't touched a
MACO assembly for namely applescript
your best friend in life is applescript
I must give you a legal warning up front
I'm required by law it started in
Wisconsin but I'm required by law to
give you the following legal warning
applescript is the cocaine of
programming one line and you'll be ours
forever so you have been warned resist
as much as you can let me ask you just a
couple standard questions to find out a
sense about where you're at and see if
that kind of hopefully please please
matches up with my idea about the
session how many of you are aware of
apple script will start with that one so
everybody okay how many of you are
scripting how many of you are scripting
well
how many just kind of like jumped into
it because you had to do something and
you heard you can do it but you don't
know how it worked and you pieced it
together and ybanez your head against
the wall 25 okay that's how I learned to
I spent years and years counting my head
against the wall going note I guess that
doesn't work and coercion I thought was
just moving the matter to the other side
I didn't realize it was actually
applying to the code in the window well
my original idea when I was talking to
skip levels about this session was to
answer that need for an quick
indoctrination into the mystery secrets
about Apple script because a lot of
people get thrown into it they are
introduced to it in a situation where
they suddenly have to deliver some
project and they find a way that works
they find something that works for them
and it might not be the best way to do
something they might be unaware of all
the different things that Apple script
can do and so my my aim with this
session is to give you a peek to the
inside workings of a scripters brain and
to turn you in and turn you on to all
the hidden secrets that I've developed
over the years about how to get up to
speed quickly with Apple script and I
focus particularly on some issues that
are pertinent to what you do is a system
administrator I don't think I need those
can I go to laptop please essential
applescript tools and tips is what I
call and for those that know me which is
probably one or two people in the room
this is the first time in seven years
that I've done slides so I was so
impressed with some of the slide stuff
going on to session I had to get some
together and I'm not going to do all
slides because I like doing a lot of
hands-on I like showing you what is
there like just you know Emeril Lagasse
and I put stuff together no Bank you
know that's what I want to do but I put
together some slides so we could
organize some of the concepts behind
this topic that's it you like that one
introduction okay purpose of the session
is to familiarize those that are already
sophistic
computer users like yourself with the
intricacies of a high-level language
like Apple script because it's very
strange it has some unique properties
about the way that it works and the way
that it thinks and I just wanted to help
you get across the bridge quickly does
that sound like the right thing to do
hmm grumbles room okay all right flight
golf clap I like that and we're going to
focus on issues like file setup and
manipulation and the kind of things that
you do what is Apple script will start
there Apple script is appear to aqua
aqua is the graphic user interface to
the OS an asshole script is the language
user interface to the OS I like to think
about it in those terms there's also
another language interface that's really
low level which is the unix interface
that you're familiar with the terminal
and shell and that stuff too but we are
also have the ability to control most of
the operating system a lot of the
applications that run on it and we can
also talk when I say we I mean the royal
we of Apple script also have the ability
to caulk across networks and the
internet and control applications to
query web services those kind of things
applescript has a very large scope in
the operating system and it can be your
best friend we're going to look at the
Apple script language itself Apple
script is an English like language that
sits on top of an architecture called
apple event Apple events are messages
that are sent internally within the
operating system that communicate
between processes communicate between
applications communicate between
networks it's like emails being sent
between a bunch of people so any action
that you do in the operating system
usually has an apple event attached to
it and we know this because we broke
apple script once and the whole OS
stopped and everybody turned around and
looked at us and we had to do another
build I remember those days but the
Apple script language itself is an
English like language that is
approachable by the average person it
you're quick way to accomplish attacks
you basically write what you think
duplicate every file of the startup disk
whose name contains smith project to the
folder name backup that's actually a
script it's not just a concept it's
actually a script and it's the principle
behind what Apple script can do you take
the mind you would think the thought and
you write it and you should work most of
the time it targets scriptable
applications such as Microsoft Word
Excel iTunes just about every database
Dobie illustrator Adobe InDesign
Photoshop QuarkXPress dozens and dozens
and dozens and hundreds of applications
are scriptable they are ready to receive
these Apple events that occur when you
write an english like sentence that gets
translated into these messages those
messages get sent to the application and
the application respond we're going to
also learn how do you refer to
scriptable items this is a tough one
this is one of the big issues that
usually ties you up and really confuses
you because there's five ways in the
operating system to identify an item
this five different means to have a file
reference in Mac OS 10 an apple script
takes a certain one of them and it
supports all of them and we're going to
look at that and take the mystery out of
how do you use different reference types
with Apple script like so let's let me
go finally and we're going to learn how
to use applescript to find items
applescript has an incredible ability
more so than just about any language
I've ever come across to quickly locate
something in a very easy manner you
remember that sentence I just said
duplicate every file of the startup disk
whose names containing Smith project to
the folder name back up that sentence
not only contains a command to do an
action but it also contains a query that
locates every file whose name contain
Smith project and then performs an
action with it the process of finding
every file whose name contains Smith
project is done for you automatically
recursively using every whose clause
object specifiers ooh very technical
we're also going to look at some of the
basic techniques for programming Apple
script it is a serious language although
I have a lot of fun with it and I like
to make fun when I'm talking about it
Apple script is very much an
industrial-strength language it is a
peer development language with job and
Objective C in our tools you can make
applications entirely on ave applescript
partially out of Apple script or a mix
or a conglomerate of any of those type
of languages and we sit at the table
with the rest of the guys we're no
longer the little child stable in the
back we said it's a big table so
anything that happens to Coco happens to
us too we're a peer development language
and we're going to learn some of the
basic techniques that the language has
such as how do you do a repeat loop how
do you do a conditional statement how do
you do an error handler these are
mysterious things to people coming from
another language because the applescript
has their own little way about doing it
but once you've seen it once you go oh
duh I got it well we'll practice that
part and then we're going to look at
some of the applescript tools that are
available right on the OS all of this is
free by the way did I say free I meant
to say free did I say free free free
free you don't have to pay for Apple
script you don't have to pay for this
incredible ability it's part of every
computer to Apple computer self and a
Hmong some of the tools of the new
script editor and the script menu folder
actions as well and it contains a new
thing called Apple scripts to do which
is part of the Xcode development tools
that will be looking at as well so let's
get started I'm going to dig right into
the Apple script language and I'm going
to reveal the mysteries of the universe
are we ready
okay how do you refer to items this is
the big thing how do you locate a file
if I found a bunch of files whose name
Canaan Smith project how do i use the
reference of those files in a script how
do I take those items I've found and how
am I able to pass them to routines or to
other applications to open them or to
process them well scriptable items are
referenced by their position in their
object hierarchy or where they are in
their chain of command that's the way
that it works in applescript in other
words an example of reference for an
item would be file new card data of
folder documents a folder home of folder
users of startup disk you can see that
chain identifies where the item is it's
the file here in this folder in this
folder of a disk disk and that type of
relationship with that hierarchical
hierarchical relationship is what Apple
scripts based upon it's a based upon
objects and objects having properties
and objects having a location and their
identity as objects it's determined by
where they are in their hierarchy or
where they are in their chain of command
that kind of makes sense let's take a
look at this there's different kinds of
references I just said that there's five
different kinds of references the first
one is a nested reference and I just
gave you an example of it file cars on
folder Documents folder Sal of folder
users of startup disk that's a nested
reference why is it called a nested
reference because it shows you that each
item is contained inside the other item
and it uses the possessive of to
indicate ownership the file is owned by
folder documents which is owned by
folder Sal which is owned by full
other users which is on the startup disk
and that nested reference acts like a
bunch of Russian dolls that you kind of
stick inside of each other but it's just
one type of reference there's path
references which are a means to identify
an item by identifying each part of that
hierarchy as a name separated by a
series of colin's will look at that
there are alias references which are
similar to pass references but point to
a live existing item they're also very
interesting in that they change an
applescript will find them and track
them so if you include an alias
reference in your script and you move
the file later the reference inside the
script changes as well there are eunuchs
references that we're all probably
familiar with POSIX past that kind of
stuff and there are a thing called file
URLs these are the five types of
references that you can use when
identifying items in Mac OS 10 and these
are the five kinds of apple script
supports nested references a nested
reference like I said indicates an
item's position by using a series of
possessive the of so they'd identifies
the final object of a container of a
container of a container of a disk and
is that what I said a nest reference
describes an item's position in its
object carry from the bottom to the top
of its chain using the possessive of or
where it is in its change of command
nested references are the default format
used by scriptable applications and
that's for the finder that's for system
system events that's for iTunes that's
for the quicktime player every
scriptable app when you query it about
an object we turn to a reference in the
nested format and nested references are
used for scriptable items other than
files let's look at some examples here's
an example of a nested reference folder
documents the folder username which
every year username is a folder users of
the startup disk
you can see that the documents folder is
the target object here and its position
its identity is indicated by where it is
in the chain of command document file
cards dot PDF of folder documents that
folder user name of folder users of
startup disk here's the same kind of
thing but now we're looking at a file
within the document folder here's an
example of a QuickTime movie praying 13
of track video overlay of movie to every
scriptable item is identified by its
position in its object hierarchy or
where it is in its chain of command
whether it's a file or whether it's a
frame or whether it's a text box of a
page of a spread of a document in
quarkxpress or indesign whether it's a
transition of a video track of a program
of a project in media 100 this principle
about nested reference in object are key
for apple script stays constant across
the world of apple script path
references nested references
unfortunately cannot be generated by a
script if you have a script and you want
to pass the reference to this object to
the finder to have it delete it or you
want to pass this to quark xpress to
open it or you want to pass it to some
processor to resize your image you can't
generate a nested reference with
applescript you can't make a thing that
says something of something of something
of something of the way that we pass a
reference to an object to an application
so that it can process it this is
interesting it like that is through this
method called path references I guess
you carry data like this then right it
would be like that path references are
used to pass a reference to an object to
the finder and to the system events
applications today I'm going to have a
lot of focus on what i do on files and
folders and directories and those kind
of things because that's the meat and
potatoes of a lot of sysadmin work is
dealing with these kind of things
going to focus on that but just remember
that it also works for any kind of an
object like an itunes track or something
else as well a path is a text string
read left to right and it contains the
name of each item that's part of the
objects chain and these items are
separated by a colon path references are
preceded with the name of the class of
the target item such as a disk a file a
document file or an item or the generic
term item meaning anything and path
references to folders and disks always
end with the colon so if the target item
is the directory your path ends with the
colon and here's some examples tell
application finder so these particular
paths are going to be within the scope
of the finder application disk quote
macintosh HD colon end quote strings and
proper names are always placed within
straight quotes in apple in the world of
applescript that's the way that they are
identified the scripts anything not
within quotes is analyzed as being part
of a dictionary or part of an
applications ability so this is a path
to a disk this is a path reference to a
disk folder macintosh HD colon users
koehler d colon Sal colon documents so
this is a pet to the documents folder on
my hard drive you'll notice that since
the target item is a folder I've
identified the classes folder upfront
and then pass that string behind it item
is a term use that's generic so if you
have a document file an alias file a
font folder a regular folder a suitcase
any kind of object that's supported by
the finder or the the operating system
you can use the word item in front of it
and it's just a generic way to handle it
so if the scripts that you're writing
and you're not sure what kind of thing
you're going to be dealing with you can
use this item document file is used by
the finder in particular as a way of
describing a certain kind of
and then there's another example of a
file item using item and those all occur
within the find the scope of the finder
that's a finder tell block will be
looking at those it begins with
addressing the finder application with
tell applications finder and then
everything after it pertains to the
finder and it finally ends with an end
tell another application that also is
included with Mac os10 that can be used
for controlling files or finding files
you're doing queries for you is the
system events application it's an FBA
it's a faceless background application
that runs and it's scriptable and it has
a disk disk file folder sweet that is
almost identical to what the Finder has
and it does the same kind of thing it's
the finder that's only a slight
difference is that instead of document
file which the Finder has it uses the
word file don't worry about document
file you can type the word file in and
the finder will change it for you when
it when it needs to so you could use
file generically and it would work those
are path references this is an alias
reference these are universal file
specifications that are honored by every
application in apple script and they are
what you use when you need to pass a
reference between applications they look
like a path reference only they have the
word alias in front of them and they
point to existing items that are tracked
by applescript we'll see how that works
in just a second here's an example so
the previous example the where we were
pointing to a disk using a path
reference this is what it would look
like as an alias reference this is what
the Documents folder looks like as an
alias reference this is what the file in
the document folder looks like and then
we have posix references you're familiar
with these are drive from the UNIX
underpinnings of our operating system
one of the great abilities that we got
that really makes mac OS 10 move forward
is its units unix history but these kind
of paths
/ paths are generally not supported by
scriptable applications we've been
around since system 7 we're based upon
the colon alias kind of idea we're not
yet aware of the / idea but there are
easy ways to get back and forth between
the two of them here are some naturally
some POSIX examples you can see you know
there's the documents folder there's a
filing documents folder there's a
another Drive folders name back up and
you notice that the characters are
escape there because of the spaces and
then at the bottom you notice that the
entire line is quoted because of the
spaces inside of the path right did I do
that correctly yes I did thank you very
much now applescript does provide a way
forget driving POSIX references from
other reference types using a construct
called POSIX path and it also took the
wrong key there also provides a way to
coerce these POSIX references into the
kinda applescript can uses with a
construct called POSIX file here's how
you drive a POSIX path you say POSIX
path of and then you provide an alias
reference to an object in this case our
object is the documents folder if I ask
for the POSIX path of this reference it
we get return to our script users /
salad / documents so that's how you get
a POSIX path from an alias reference
there's the POSIX pass up just place
that in front of your reference now if
you if you're not sure that the user
might have used some strange characters
or some spaces or stuff like that which
often occurs on Macintosh world you
might want to ask for the quoted form of
the POSIX path of this reference and
what that does is neatly in white see
I'm getting into the slide thing neatly
in white you notice how it puts those
little ' so it quotes the line for its
automat
quickly so quoted form of POSIX pass up
if you didn't know that you'll be
sitting there pulling your hair out and
then you might be stupid routines to try
to figure out how to append apostrophes
to the end of what you were driving some
of you might have already tried that how
to coerce a posix path now to get to an
alias reference it's going to take us a
couple steps the first one is that we
need to get to what it's called a POSIX
file and if you take a POSIX path and
you say as POSIX file you will course it
to this format here which looks like our
path reference right file then colon
delineated from the drive down to the
object itself by saying as any time you
want to do a coercion from one format
into another in applescript you use the
term as you could take a number course
it to a string by saying 10 as string
you could take a string and corset to a
number by saying quote five quotes as
number use the as word the adverb and
that's not a verb it's a preposition I'm
guessing is that right okay yes whatever
it is it's a wonderful word it's it's a
magical thing use that when you're doing
a coercion that's how you course an
applescript now to get to the alias
we're going to do a double coercion
we're going to first of all coerce to a
POSIX file then the result of that will
be coerced to an alias that's because
Apple scripts are read left to right and
they're executed left to right and from
the top down in a script so the first
thing that happens is is that that POSIX
path is queried course to a POSIX file
which is a path reference and then its
course to an alias so the end result
then is an alias reference that i can
use with quicktime player or any other
scriptable application to have that
folder become a target folder or open
that's how you course from posix to
alias in one line if you didn't know
that life is horrible if you do know
that you're in the air conditioning file
URLs little known but can be useful in
extreme situations file URLs described
items on your drive in the same manner
that the same way that they're described
on the internet which is you know HTTP
blah blah blah blah with all the
different slashes and stuff and they can
get these file URLs from either the
system events the application or from
the finder or you can make them up
yourself if you want to and they look
like this tell application finder to get
the URL of folder documents of home well
that would return me file colon slash
slash localhost / user / file /
documents file localhost user / file now
you'll notice that and the other ones it
automatically provides that for you when
you're asking for the file URL you don't
have to worry about the percentage
encoding that will get handled for you
in the information that's returned so
those are the five types and how do we
coerce between them so let me just
review they are nested reference path
reference alias reference POSIX and then
file URLs to coerce between them will
review this quickly you can coerce
between the following reference sites
nested reference to an alias reference
so i can get a nested reference back
from the finder and say I'd like this as
an alias you can coerce between a path
reference to an alias reference so i can
take a path reference that i construct
myself of my script and then course that
to an alias that i can then pass to an
application you can were coerced from a
POSIX path to a POSIX file to an alias
reference like we just saw and then you
can coerce between an alias reference to
a POSIX path as we saw easy previously
nested references again can
be generated by Scripps so here's an
example folder applications of the
startup disk as an alias will return me
alias macintosh HD colon applications
colon folder macintosh HD applications
as alias if this is an example of a pass
reference within the prince coerced to
an alias so that returns to the same
thing and then n tell the POSIX pass of
an alias macintosh HD blah blah so we
have an alias reference here we're
asking for the POSIX path that will
return us the POSIX path to that
particular directory and then if I take
that and course it to a POSIX file and
then to an alias it returns us back to
an alias so that's how you course an
applescript between the various types is
anybody ever told you that stuff before
it's like mysterious stuff isn't it if
you don't know it in the world of Apple
script you'll go like this and you can
see that half of my head's worn away
from that so let's take a look at some
of this kind of stuff can I have this
machine which what do I call this
machine demo that's demo one thank you
demo one ok this is demo one so before
we begin taking a look at these paths
kind of ideas we need to find out where
applescript is on your computer and I'll
you happy to show you applescript lives
in a couple locations on the computer
the main location is in the apple script
folder and if you open up your whoo
yellow I like that soft on the eyes
that's nice thank you that's really good
nice touch
if you open up the applications folder
within that folder it's a folder called
applescript this is Apple script home
turf this is where we live and we're in
pants or right now so you know some of
these icons might become a little bit
more sophisticated by the time we ship
we do have a script editor that has an
icon though and this is the script
editor application it's new for it's the
beta version that we've been posting on
the website and a lot of people have
been using it it's an incredibly nicer
version than we had before we had the
distinction of being the oldest piece of
code shipping in the operating system
prior to this this is a native Coco
application it has a lot of power don't
be fooled by how clean and nice it looks
this is the script editor application
this is where you read and write and
record your scripts it contains two
different panes in the top area here is
where we write scripts tell application
quote finder member names get put within
a quote mark to open the startup disk
and this is our compile button for
checking our code to make sure it's been
written correctly this is our Run button
and when I run it well that's
interesting oh I hit others okay sorry I
fooled myself I only have me to blame in
that pain is where we write our scripts
you'll notice that as I clicked the
compile what it was checking the script
to make sure it was written properly
certain words become bold certain words
will become indented this is to show you
which words belong to the Apple script
language which words belong to the
application that you're addressing for
example the word tell belongs to Apple
script so does tell at so does too and
so does the startup disk and the verb
open belong to the finder at the bottom
here is a multi paint area and we have
three panes
one is the description pain and scripts
can actually post the little dialogues
with scrolling information about the
script and you can just save a button
click a checkbox when you save your
script and they will do that for you and
you keep that information that you want
to present to the user in this pain
right here your description field the
result field shows you the last action
of your script the results of the last
action so if you had ten actions it
would show you the results of the tenth
line of your script the event log is a
way of tracking what you do with your
script it will follow along and show you
each of the actions and the result from
that so if I ask the finder to get the
name I'm not spelling today the name of
the startup disk and click that you can
see that my event log shows that a
command with sense of the finder gets
the name of the startup disk and then
indented underneath it shows you the
result of that action so this is a way
to debug your scripts you can use the
event log so as a way of tracking what
you're doing and the result window
always contains the results of the last
action which is the name of that
wonderful name Panther w tools HD of the
name of my startup disk this is the
script editor window this is a script
window within the script editor it does
have some new features especially for
this new release and one is called a
navigation bar if you have a rather
complex script that has a lot of
different routines and handlers in it
they would show up here and if you're
using other scriptable language besides
the applescript they would chew up over
here
which ones did you were you referring to
clapping to Python there was somebody
someone once Python Perl and JavaScript
right there is a JavaScript OS a
component from mark Aldred of late-night
software and you can install that and
that would show up right there and you
could use that if you wanted to the
script editor also provides us with a
way to view the internal dictionaries of
applications applications that are
scriptable have scriptable objects they
contain scriptable objects and each of
those objects contain certain qualities
that make it a scriptable object and you
can examine the entire structure of a
scriptable application by asking for its
dictionary so I just choose to choose
open the open dictionary command from
the desktop and I'm going to open up the
QuickTime Player choose open and this is
the dictionary window for the QuickTime
Player this window contains on the left
hand side will contain the terms and the
phrase is used and on the right hand
side will naturally contain a
description about those selected by
default is the standard suite and these
are the commands that are part of the
standard suite by standard suite those
are usually the commands that are
available in most scriptable
applications support this and there your
core set of verbs such as clothes count
delete exists make open print save those
kind of verbs are part of the standard
suite so when I told the finder to open
something I used this verb it says open
and then a rep is expect to reference a
reference to what whatever it is that
I'm supposed to open it will take a list
of them or a single one in this case I
said the startup disk and that's what it
did the in this case the quicktime
player suite contains all the classes of
objects that are scriptable within the
quicktime player for example the parts
of the QuickTime Player that are
scriptable let's start with the utmost
obvious one
it will look at the application this is
the quicktime player application itself
you can see that the dictionary is
divided into a section up here called
elements that's what makes up the
QuickTime Player it can have a mooted
display it can have a favorite a movie a
recent item in a window the properties
of the application itself it does have
some properties that you can set from
ignoring auto present too what is your
quick time is quicktime pro installed
and it will return a boolean value for
that so i could just take this right
here go to my script tell application
quicktime player and run that and
they'll launch the quicktime player and
return me true quicktime pro is
installed on this machine and you'll
notice that it said that brought up my
little welcome movie is guess what
they're calling it and it's set
currently set in the Preferences player
preferences to show this movie for me
automatically well I see that that's
also a property down here and it takes a
boolean value so I could use this and
say set to false check my syntax and run
it and now when I go back to the
quicktime player i choose preferences
player preferences that's turned off so
what happens is this piece of code this
english life sentence is translated into
an event they get sent to the quicktime
player the quicktime player takes that
event respond to it and does something
accordingly the dictionary is where you
learn about your particular applications
that you want to script and all the
things that they can do for example if
we'll open up here and we'll look at the
finder dictionary I'll show you the
dictionary quickly for the finder this
is how the finer dictionary is divided
it has a standard sweet just like the
other application it has its own basic
suites which contains some classes of
objects and then it also has commands
has some finder items and these are the
property's owner privileges and then it
has some verbs like clean up a jack
empty erase reveal you can use the
revealed verb with the reference to the
object to be made visible well let's try
that out so i'll say until app notice
the shortcut reveal the reveal folder
users of the startup disk ah mistakes
something happened and there's a typo
here start up disk now let's check it
again and I'm going to get one teacher a
verb activate which will bring the
finder to the foreground and let's run
that and there it is it selects it
highlights it for you uses the verb
reveal so the point i'm showing you here
is that the dictionary contains the
commands for the various applications
you're going to have to go into these
dictionaries and parse them play with
them have fun with them to learn all the
different things that they can do but
they are available to you right from
within the script editor so let's take a
look at some of the reference types that
we were looking at here and the first
one was a nested reference so I'm going
to take away the activate verb here and
I'm going to say open up my hard drive
and take a look in my home directory
and I'm going to go into my pictures
folder webcam images and get that copy
that so I'm going to say let's make a
nested reference to that so if I just
say home and run that it gives me the
reference to the home directory folder
apple of folder users of startup disk of
application finder of startup disk so if
I said folder pictures of compile now I
have a reference to folder pictures of
folder apple of folder users of startup
disk of application finder and then with
that in that there was a folder called
webcam images so i would say folder web
cam images folder list as you can see
nested references are rather verbose
that's why we don't create them and then
i'm going to take a movie out of there
and then I could say document file quote
this of and that points to that movie
file that's an example of a nested
reference it can use that with any of
the commands like open and it opens up
the movie just as if you double clicked
it from the finder or I could use the
verb delete and then look in the trash
and you can see it placed it in the
trash that's a nested reference it's
used by the finder application and it's
used by as the default for most
scriptable applications the next kind of
reference we're going to look at is a
path reference now I was writing this
nested reference by hand I pass this
reference to the finder by hand because
I wanted to create that for you but if
you have a script that you're writing or
that's functioning it's
performing in action you won't be able
to create this kind of thing indirectly
what you can do is pass it a reference
as a path reference which would be file
or document file this part i can create
because this is just a string and it
would look something like this and then
users colon Apple is it Apple Apple and
then pictures then the name of that
fought up up in the name of this file
again I'll grab it again so a script
could generate this because it's just a
text string and expected up see and I
misspelled webcam images pictures that's
right well let's sell hold on a second
let's try that out so let's see if I got
this right doesn't exist right because I
missed this one thing here called webcam
images now run it and again it opens it
a script can create this because this is
a text string we can get the path to the
startup disk or we could get the path to
another directory and then add or
concatenate the name of the file that we
want to the end of that take that put
the word document file in front of it
and pass it to the finder and that's
what we have here or you can coerce a
path to so let's say home as alias or
home as string I'm sorry home is string
and if we run this it gives me the path
to the home directory but as a string so
then I can take that I'm going to make a
variable and in applescript
this location to home is that an
applescript when you make a variable are
we familiar with the variables are I
assume variables are containers they
contain information an apple script to
make a variable you just make up a name
that Apple script doesn't know that's
not part of a dictionary you can make
them up in a variety of ways such as
this where i use two words that are
underscored together or you could make
it probably something like this where
you have an inner cap or you could just
make it x whatever you want to use for
your variable you can do that but i'm
going to save this location to home is
string and then i have that part stored
into the variable and then I could say
set the target file to and make this a
variable here at the target file to this
location ampersand concatenate and then
I'm going to pass it the rest of the
path so I'll save pictures webcam images
and then the name of the file which I'll
grab one more time because i'm lazy to
memorize it so what I've done now is
created a string let's run that and I
have a string to make it a path
reference all I need to do now is tell
the finder to open document file and
then what's ever inside of this
container which is a path reference and
now that's how you pass a reference to
it the finder or to an application so
that makes sense you can create these
yourself by putting together pieces of
string you can't create a nested
reference on the fly like this is doing
ok then the last thing we wanted to look
at was the alias reference so i could
ask this location i'm going to delete
that again instead of as string i'm
going to say as alias i misspelled alias
and that's how I can coerce from a
nested reference right to an alias
reference so now within the variable i
have alias Panther this to a directory
to my home folder so if I say open this
location what will happen my home
directory open that's how you chorus to
an alias so those are the different
kinds of references nested reference
path reference alias reference and then
we have posix paths as well so if I
wanted to take this and then course that
to a posix path then I could say posix
path of this location because what's
inside the variable is just the same
thing as if I was addressing it and a
misspelled pass notice how applescript
finds that for me and there it gives me
the POSIX pass so if I needed to do a du
shell script where I needed to pass
something to the terminal then I can
course what I had into this kind of a
thing so that makes sense we see how
that works very simple ok let's go back
to the slides laptop 1
this is kind of dry stuff but it's the
stuff that nobody's going to tell you
otherwise okay finding items how do you
find items specific items can be located
by querying the finder of the system
events applications and the way that you
do this is just by asking items can be
located in two different kinds of ways
you can identify items by two different
kinds of ways does anybody know what
they are index and property for example
we have people here I'm standing here
and I could say if I need to find
somebody by index I could say third
person in the third row on my left hand
side please stand up and that would be
just gentleman right here but I want to
bear she please sit up so that's an
index right third third we're looking at
a number where they are in a position in
a list of people or a row of people I'm
looking for a specific one I could say
the 10th row please stand up and the
tents road from here would stand up I'm
using the index property of an item to
identify a specific one to get to where
I want to get to the second way that you
can identify items and Apple script is
buy a property now we all have
properties every scriptable object has
properties a movie has a property of a
duration a movie has a property of a
kind is an audio track or a video track
as a matter of fact it relates a lot to
the way life is consider Sal to be a
scriptable object let's say ok as a
script of logic I have certain qualities
that make me who I am I have a height I
have a weight unfortunately I have an
age I have a position in the room I have
a name and these qualities define who
sal is or help define all that is Sal
now you can use the properties of an
scriptable object as a way of
identifying it
for example here's one with everybody
who's named Bob please stand up I'll
come on really an entire place is no
Bob's okay Wow Charlene okay okay but
you get the idea I could say we could go
by color I could say well everybody's
wearing a red shirt please stand up and
then people would stand up if they so
felt like it but you can see what I'm
talking about is you can use a property
you can use the quality or value of a
property as a way of identifying a
specific item and the same work in
applescript you can find by index or you
can find by property here's how you find
by index every scriptable item on disk
or belonging to an application occupies
the specific space in the order of items
in its parent container this what does
that mean okay find your windows here's
a good one right can there be two Finder
windows on the same level no for some
reason there always has to be one Finder
window they can move on top of another
they're always if one fund or Windows it
gets on another in other words if we
looked at the finder sideways we would
see a stack of windows that were open
that stack is a list of Windows and
every window has an index number
corresponding to the way it is in that
stack the middle guy might be window
three the top guy will be window right
and the last guy would be the total sum
of the number of windows so the index
property identifies where an object is
in the list of objects if I had five
movies open in quicktime player there
can only be one front movie right so the
front movie would have an index value of
so i could say tell movie one to play
and it would play if I said tell movie
two to play from the front movie
wouldn't play the movie behind it would
play so the index property is a way of
identifying a particular item in
applescript numeric values represent an
item's place in blah blah it's
in index okay here's an example tell us
a fake whoops whoops fell applications
finder to get item one of the startup
disk what's that going to get what would
that return it would be probably return
something like folder applications of
the startup disk it's going to return me
a nested reference to the first object
that it finds on the startup disk item
one so I'm using the term item which is
generic identifier member and I'm saying
give me the first one and a bike saying
than the index value of one tell
application finder to get item 12 of
home well that could return anything on
anybody's machine but in this case I
have a nested reference to the folder
downloads of folder salif older users of
the startup disk so I'm identifying it
by the 12th item in the container that I
have I know lovingly as home so items
are when you have a bunch of items in a
container they're actually like a list
this is a list in applescript this is
what it list looks like an apple script
and it's the same kind of list that
works for a list of references a list of
data a list of numbers a list of names a
list a combination of anything an
applescript you can put all kinds of
stuff into a list it doesn't make any
difference you don't have to declare
this list only has fish no you can put
in anything that you want to and it
begins with a curly brace and each item
in the list is separated by a comma and
it's closed with the curly brace so
let's imagine that that's a list of
items so if I ask tell applications
finder to get item count of items of
home of home now what's that what am i
doing there I'm asking for the last file
or last item could be an item right so
it could be a folder we don't know but
I'm asking for the last thing in the
home directory I'm doing it by with this
paren thing as we know in algebra you
know what was that is like turning and
then twist I like that
turn and twist it's good the count of
items of home will get executed first
because it's within parentheses right
similar to like what we learn in seventh
grade with algebra things inside the
parentheses get done first and then the
result replaces them and then the thing
continues from the outside in so the
first thing that's because this script
will do is it's going to get the count
of the items of home we place that and
then use that number as whatever item is
if I have 24 items in the home directory
that will be item 24 will be returned to
me so in this case it was folder USB
keys you being the last thing I probably
had in the folder now so far we've been
looking at our list from left to right
but applescript has this an interesting
thing called a negative index whew see
this is the kind of stuff that you get
when you come to see uncle Sal Apple
supports the iteration of lists actually
in both directions as if you're not
confused enough you need more items in a
list have a positive index value when
the list is transverse from left to
right or from top to bottom in the case
of windows right okay but when you read
them from right to left or from bottom
to top they have negative numbers ooh
interesting for example left to right
this list is item one item to item 3
item for item 5 right but when you read
it the other way from right to left
that's item minus one item minus 2 item
minus 3 item minus 4 item minus 5 okay
let's do that again left to right 1 2 3
4 5 this way back this way minus 1 minus
2 minus 3 minus 4 minus 5 that's called
a negative index value now why do we
know that why do we want to use it what
do we care about that well remember how
I said tell application finder to get
item count of items of home of home
that's a lot of work to do just to get
the last guy in the home what else could
I use instead
minus one darn right so if that returns
the USB so does this tell application
finder to get item minus one of them
returns folder USB key a folder style of
folder users of startup disk so this is
an example of why you want to use
negative index values on occasion if I
wanted to get the item before the last
item of home what would I use the one
before they'd be forward three the one
before did before did before four and
now I'm lost that's not going any
farther okay because we can also find
items by range I can ask for a range of
items using index values for example I
could say tell application finder to get
items two through five of the startup
disk and that's going to return me
perhaps something like that you know
folder applications and the next thing
in the list so in this case the list
it's a list of references before it was
just a list of names right but now a
listing contain anything we want it's a
list of first references folder
applications that start to describe
kitchen find a comma next list items at
us next list item like that so I get
back a list of items by identifying a
range using numbers to application
finery to get items one through minus
one of the startup disk what will that
do everything why don't I just use this
instead see that's the beauty of apple
script we got all this cool stuff so
instead of doing won 2-1 you can just
say every every it's a great word in
apple script yet every item of the
startup disk finding items by
descriptive index now i'm giving you the
extra stuff that you normally don't get
so here's something cool since the
applescript supposed to be
conversational you can write it as
cursed as you like or you can write it
as verbose as you like you can use words
like this for a second third fourth
fifth sixth seventh eighth ninth tenth
you can use those instead of writing out
the numbers why I don't know
so you can say here's why you can tell
application finder to get the first item
of home so you can use the word first or
you can use first twenty third tenth you
know 4,000 678 so you can support it
supports that as well so you could say
tell application finally gets a 20
seconds item of home instead of saying
item 22 you could say 20 second item
conversational finding items by relative
position now this is interesting so
items can be referenced in terms of the
position relative to other items in
their contained parent container so will
the person next to the person on the end
waved his hand of the second row a
little bit of prompting it worked so
there I took somebody as an identifier
was the gentleman on the end of the
second row and that was my pivot and
then based upon him I referred to
somebody else would the person who's
sitting across from the gentleman in the
second row wave his hand right here so
you're using an item by index and then
referring in relative position to that
you can do the same thing with Apple
scripts and files and things like that
and you can use these terms when doing
relatives position front first back last
middle so i could say give me the name
of the middle window of a stack of
windows and it's going to find that
window that's in the middle and return
that I could say give me the name of the
last window give me the name of the
front window and it's a matter of fact
there's last item of the startup disk
the middle track of the front library
that's iTunes you can expand and alter
that relative positioning using the
words after and before the item before
the last item the item after the middle
item so those are examples of how you
can use relative positioning to find
specific things with index values also
you can generate random things by using
the word sum
so if I asked for some item of the
startup disk every time it's going to
give me something different so that's
finding items by index that's an
important concept to get but once you do
that with applescript it's really good
for doing that kind of thing the only
thing that's tricky about index is if
something moves everything changes if
somebody leaves this row the quality of
the roads change now my index stuff that
I had set up doesn't work anymore so be
careful when using index value it's a
way to find an item but it can be very
useful now finding items by property
well you pretty much guess this one to
write every item on disk like Sal has a
property named blah blah he gets older
and older some of the properties you can
change I can change my shirt I can't
change my age that unfortunately is a
read-only property there's some
properties like name kind which is also
file type you can find items like file
type size creation modification dates
just about any property of an item can
be used to delineate it when you're
looking for it with Apple script on it
on a disk you can find them by comparing
the value of their properties to a
desired set of values for example the
first document file whose name contains
Smith's project I'm asking for the first
document file whose name contains Smith
project now watch this magic happen the
first folder whose size is greater than
500,000 that's bite in Apple script
world you'd say bytes and then you have
to do the math we have math Oh every
document file whose file type is jpg hmm
might be useful every document file
whose name extension is PDF hmm that
might be interesting to as well let's
examine its farther how do you do these
queries these finding queries how do you
create a query well we know that when
you do command F in the finder what do
you get you get that pop-up window thing
that has the you go add another detail
add and remove damla let the other
detail mitted to the pop date created
and so you're basically writing a
sentence is what you do
with those things right you want
something whose whose name begins with
this but creation date is after
so-and-so well I query an applescript is
composed by combining these elements
first Oh a positional indicator a target
object type a target container and
object specifier a target property and a
comparison operator is there any more
and a target property value whoo ugly
ugly ugly ugly but it's true and it
looks like this the first document file
of home whose name contains smith
project is actually all those things we
don't think about that when we're doing
it we just think like that because we're
trained to think like English thinking
people whoa for example a positional
indicator is first what if I want to
last what if I want every I could say
every document file of home whose name
contains Smith project I could say the
last document file so that's just a
positional indicator indicating where in
the list of items in the home folder
this particular one is that I want then
there's the target object type what kind
of object are you looking for if you
want just everything you say item if you
want a document file you say document
fall if you want a folder you say folder
if you want an alias file you say alias
file an alias files that thing that you
double-click and it takes you to the
real location if you want to say a URL
link file you say that the target object
cut tight goes next then the target
container where am i searching okay in
this case the word home indicates in the
finder your home directory that could
also be a variable containing an alias
reference to some other path that I want
to have searched right I could have that
that be a folder nested way inside of my
preferences folder and then just pass
that into the
the search next comes the object
specifier which is whose there's also
where you can use where with applescript
and the word it's where it's blah is
blah instead of using whose blah is well
you can say where it's flies blah and
what the English Graham miracle rule is
for that I have no idea but we support
it then what kind of property are you
searching on we're looking by property
now right so in this particular case I'm
using the name property I could use its
modification date I could use its size I
could use its comment tag I could use
whatever label late label turn okay yeah
i thought i gave something up there okay
yes that's right it's all right it's all
right it's all packed try get property
so then you have to identify what
property is then you comparison operator
now this is the interesting one contains
well you can use every one that you can
think of contain does not contain is in
begins with ends with is greater than is
less than its greater than or equal to
its not greater than or equal to isn't
greater than or equal to just about any
one of the things that you can sync up
in your head for the English language
will fit into one of these comparison
operators and then finally what is the
value of a property that you're
searching on and that is in this
particular case the name value Smith
project so all of those things that have
names just go into that we understand
these implicitly you really don't have
to think about them beyond that now
here's how you can change the positional
indicator to change your query the first
document file the last document file
every document file you can change the
comparison operator to change your query
every document follows name contains
Smith project every document file whose
name does not contain Smith's projects
every document file whose name begins
with Smith's project and we talking
about files it's making hence with Smith
project and you can also change the
property value that you're looking for
every document file whose name contains
Smith project or every document follows
name contains a cc stats
if you get the idea of how this works
it's pretty much like you think right
now what about recursing how do you get
down through a directory of hierarchy so
if I have a folder within my documents
folder for a project I want to search
everything in that folder how do I do
that well you go to JavaScript and you
write for if blood bit and then you
start doing a recursive loop that
recalls itself back up at the top again
that when you get down to here no we
don't do that in Apple script we're
civilized we're bohemian but civilized
on it and the way you do that is with
the entire contents property every
document file of the entire contents of
folder documents of home whose name
contains Smith projects will do all the
recurring for me what will I get back
I'll get back a list of references to
every item within that entire hierarchy
who matches those particular query
objects that I set up now let's take a
look at some of that can they go to demo
please one demo one index values demo
one demo to jello one demo two can I go
to the machine next to dem the other
demo machine besides demo to that it's
not a demo machine that is not demo to
quick write it again write it again demo
one Thank You Sal your references cost
you ok you guys going to make it hard on
me all right I'm going to try something
here I'm just going to copy this copy
webcam images and I'm going to go to the
script editor and i'm going to set up a
open the window here called library and
this is my will shortcut window to my
favorite things i'm just going to go
click a new script for that and then i'm
going to go up to it and menu here and
go paste nope doesn't let me paste
reference anymore ok feature nevermind
you didn't hear that ok so what I want
to do is I want to do some searching
here so let me see if I have anything
that's that's searchable every every
file every document file documents file
of the desktop desktop is a special area
who's kind is jpg image I think that
works ha and it gave me a link to an
image and it gave it to me in a list
format why did it give it to me in a
list because I asked for every and it
just happens to be one right so if I
want to get something out of that list
then I could say set this image my
variable to item one of and they'll put
that all in parenthesis like this so
I'll get the first one that's one way to
get the first one up can't item one of
every document file that's interesting
that's a bug probably done by tuesday
let's just short cut the process by
doing that and that gives me back one
why because i asked for the first
instead of every now that i have this
image then i can go open this image so
there's how to do a query and find
something and do something with it right
let's try something else let's go every
item of home whose name contains f okay
there's a lot of stuff you can see that
it returns me a list of all the matching
items right folder sites whose name
contains si ah folder music which
contains SI and folder sites that
contains SI and then i wonder if i can
say reveal if it will let me do that i'm
going to try that and it probably did
selected both of them there they are
both of them have been selected so whoo
so what we have here is cleaning and
waxing in one motion one motion I find
this stuff the other one I actually do
something with it in one line of
applescript we are able to clean and wax
together and that's how you do it using
the these type of finding techniques
let's go back to slides machine please
which is laptop now I'm going to go
through some of this this is secret
stuff that we're done ah oh we're done
can I just have everybody come back to
my room
you guys want to come back to my room
they won't mind the arch and I'm right
over at the origin we'll just walk in
you know they're with me I'm apologized
about that but I hope perhaps that maybe
this gave you a little inkling of what
some of the applescript can do for you
and a little bit of the ability one
place you go dub dub dub apple.com /
applescript click the resources button
and there's a ton of stuff there for you
to peruse thank you so much I appreciate
[Applause]
you