Jump to content

OSX86 Project's AppleScript tutorial


21 posts in this topic

Recommended Posts

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:

 

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):

 

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:

 

Tell application "iChat" to activate

Edited by A Nonny Moose
Link to comment
Share on other sites

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:

 

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!

Link to comment
Share on other sites

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:

 

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:

 

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:

 

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"):

 

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:

 

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:

 

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.

Link to comment
Share on other sites

  • 4 months later...

Ok, this simple script is an example for displaying dialogs and making the finder speak.

 

tell application "Finder"
display dialog "Yes or no?" buttons {"Yes", "No"} default button 1
say "You're the master" using "trinoids"
end tell

Link to comment
Share on other sites

  • 3 weeks later...

Did you try recording your actions in finder in the script editor?

Link to comment
Share on other sites

  • 1 month later...
  • 1 year later...

I know I'm bringing back a crazy old thread but I just had to share this AppleScript bit. Got it off a site:

 

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 :D

Link to comment
Share on other sites

  • 1 year later...

Son of GUI Scripting. Yes, it's been made a lot easier with newer versions of the Mac OS. Without further ado, here we go!

 

You have to activate the program first so your first line is:

tell application Finder
activate
end tell

 

Now for the fun part. Back we go to System Events in order to get an item "clicked." Here is the full script of the process, which you can modify as you need fit:

tell application "System Events"
tell process "Finder"
	click menu item "Get Info" of menu "File" of menu bar 1
end tell
end tell

 

In this instance, you've just told the Finder to get info on your item, which is a relatively stupid thing to ask System Events to do, but you get the point. For an Automator process, this can come in very handy, as System Events can do a lot of things that Automator actions just can't do.

 

You'll notice that I have to include the menu AND the menu bar. If the action doesn't work, there is a big chance that you have to modify the menu bar to another number. Just fiddle with that setting until you figure it out. In order for ANY of this to work, you have to turn on assistive devices, which is in the Universal Access Preference in System Preferences.

 

There are many other clickable things that can be done with System Events, but that would be the subject of an advanced AppleScript tutorial. Let's just say System Events can do a hell of a lot of things!

 

Here is the completed GUI Script for you to copy into Script Editor or Automator to modify for whatever you need:

 

tell application "Finder"
activate
end tell
tell application "System Events"
tell process "Finder"
	click menu item "Get Info" of menu "File" of menu bar 1
end tell
end tell

 

Now no GUI AppleScript piece is complete without Damned, the AppleScript from Hell. It won't damage your system in any way, so don't think it's a virus. It is incredibly annoying once executed, though, SO BE WARNED!!!

Damned.zip

Link to comment
Share on other sites

 Share

×
×
  • Create New...