Help - Search - Members - Calendar
Full Version: OSX86 Project's AppleScript tutorial
InsanelyMac Forum > Apple World > Mac Programming and Development > AppleScript and Automator
A Nonny Moose
I thought it would be good for us to work out some kind of tutorial that explains AppleScripting in stupidly simple English. Here's the starter:

The "Tell" block

This is normally the very first thing you'll ever do in an AppleScript. Basically, you have to TELL something to do something. For instance:

CODE
Tell Application "iChat"

End Tell


In this instance, you're telling iChat to do something, but what can you tell it to do? Well, you can always tell it to Activate (open):

CODE
Tell Application "iChat"
Activate
End Tell


So this sample script just told iChat to open. Of course, you can tell applications to do a lot of other things besides open, as other posters will show.

You can also make it a one line Applescript, which will make it more sentence-like:

CODE
Tell application "iChat" to activate
Colonel
I made the script and compiled it for those who want to see what the script should look like when it's done.
Sound helpful?

Oh, and I also think that this would make a great sticky.
Korrupted
To show a hello world dialog box, use:

CODE
display dialog "Hello, world"



If you want it to only have an ok button:

CODE
display dialog "Hello, world" buttons "OK"
A Nonny Moose
In UNIX world, you normally have to go to Terminal to do something. For instance, to change your working folder to /Applications, you'd be typing "cd /Applications." If you're constantly using a certain directory in Terminal (for instance, you need to clean out files from that directory and Finder is being picky), you can cut to the chase by using the "do script" command. For instance:

CODE
Tell Application "Terminal"
Activate
do script "cd /Applications"
end tell


You just told Terminal to open and do the command you always start out with in a Terminal session (whatever that command may be). You can learn about some other commands by opening the AppleScript dictionary from your Script Editor application or just watch this space!
Swad
Great thread! Definitely sticky worthy. smile.gif
A Nonny Moose
GUI scripting: it's not as evil as you think, but it involves a lot of tell blocks (see the very first post here for info on Tell blocks)

First off, head to System Preferences, click Universal Access and allow access for assistive devices. Without this, you're sunk. Come back after you've clicked it.

Got it clicked? Great, now you're ready to make an AppleScript. Launch Script Editor and ger ready to tell it a whole lot of stuff. Let's pretend we're going to work in the Finder, so start off with:

CODE
Tell Application "Finder"
activate
end tell


All right, now let's look at that menu bar (that's the thing at the top of the screen that has the application name on it). We're going to make the Finder use the select all command. This is done by manipulating an application buried deep inside of OS X called System Events (and you don't need it to activate either, which is weird). So our starter code is:

CODE
tell application "System Events"
tell process "Finder"


Note the Finder becomes a PROCESS now instead of an application. This is normal. Now we have to tell what we're aiming for, which is the menu bar:

CODE
tell application "System Events"
tell process "Finder"
tell menu bar 1


But wait, there are more tell blocks, because you have to tell the GUI where to go in that menu bar (in this case, "Edit"):

CODE
tell application "System Events"
tell process "Finder"
tell menu bar 1
tell menu bar item "Edit"
tell menu "Edit"


Note we have to get the Edit menu mentioned twice, once as a menu bar item and once as a menu. For every item that reveals a menu, it has to be mentioned twice or the script will fail (see my Safari Debug script if you'd like an example of mutiple menus). For now we're dealing with one menu and we've hit gold--we've found Select All! So you have to tell the GUI to click it:

CODE
tell application "System Events"
tell process "Finder"
tell menu bar 1
tell menu bar item "Edit"
tell menu "Edit"
click menu item "Select All"


All right! Now we have FIVE tell blocks that need to be told their work is done, so now we insert five "End Tell" statements:

CODE
tell application "System Events"
tell process "Finder"
tell menu bar 1
tell menu bar item "Edit"
tell menu "Edit"
click menu item "Select All"
end tell
end tell
end tell
end tell
end tell


And you've just bent the Mac OS GUI to your will.
Zealot
good to know kind of late for me but the help is help anytime.


saludos
Proteo
Great thread. Keep these examples coming please!
McSkywalker
Ok, this simple script is an example for displaying dialogs and making the finder speak.

CODE
tell application "Finder"
    display dialog "Yes or no?" buttons {"Yes", "No"} default button 1
    say "You're the master" using "trinoids"
end tell
A Nonny Moose
If you have 10.1, make an AppleScript that says (sans spaces)

say "f u c k s sheep"

It's a laugh as your Mac will honk right in the middle of it.
Numberzz
how do you make an application quit?
Numberzz
The sample "Select All" script works in Tiger, but not in Leopard 9A303.
A Nonny Moose
Check your system preferences and make sure you've enabled assistive devices.
Numberzz
I get the error "The tell statements are nested too deeply." I copied using command-c and then pasting it in the script editor.
A Nonny Moose
It could be that Leopard might be doing a new way of GUI scripting then. I'm not sure, since I'm using Tiger and not Leopard.
Numberzz
I get as far as telling System Events to make the Finder a process instead of an app.
McSkywalker
Did you try recording your actions in finder in the script editor?
A Nonny Moose
QUOTE (ajb01 @ Nov 21 2006, 12:27 AM) *
how do you make an application quit?


Tell application iChat
quit
end tell

Substitute your app for iChat.
A Nonny Moose
All users of 10.4.8 and above, please note the GUI scripting just...won't...work. It will work for lower supported versions.

I need to research and find out why GUI scripting has changed and respost.
~pcwiz
I know I'm bringing back a crazy old thread but I just had to share this AppleScript bit. Got it off a site:

CODE
say "Dum dum dum dum dum dum dum he he he ho ho ho fa lah lah lah lah lah lah fa lah full hoo hoo hoo" using "Cellos"


Go to Script Editor paste that code in, compile it and run it. The results are quite humorous biggrin.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.