Jump to content

OSX86 Project's AppleScript tutorial


  • Please log in to reply
20 replies to this topic

#1
A Nonny Moose

A Nonny Moose

    Proud PPC User

  • Members
  • PipPipPipPipPipPipPipPipPipPip
  • 2,421 posts
  • Gender:Male
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, 16 July 2006 - 07:10 PM.


#2
Colonel

Colonel

    11 Herbs & Spices

  • Retired
  • 4,157 posts
  • Gender:Male
  • Location:KFC
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.

Attached Files



#3
Korrupted

Korrupted

    Wandering Samurai

  • Retired
  • 1,623 posts
  • Location:Wandering the world over.
  • Interests:Mac<br />Laptops
To show a hello world dialog box, use:

display dialog "Hello, world"


If you want it to only have an ok button:

display dialog "Hello, world" buttons "OK"


#4
A Nonny Moose

A Nonny Moose

    Proud PPC User

  • Members
  • PipPipPipPipPipPipPipPipPipPip
  • 2,421 posts
  • Gender:Male
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!

#5
Swad

Swad

    Founder.

  • Administrators
  • 3,709 posts
  • Gender:Male
  • Location:Chicago
Great thread! Definitely sticky worthy. :angry:

#6
A Nonny Moose

A Nonny Moose

    Proud PPC User

  • Members
  • PipPipPipPipPipPipPipPipPipPip
  • 2,421 posts
  • Gender:Male
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.

#7
Zealot

Zealot

    Zealot FTWeed!!!

  • Members
  • PipPipPipPipPipPipPip
  • 901 posts
  • Gender:Male
  • Location:Santiago ,Chile .
  • Interests:Gamez pc, manga,craks n over cloks
good to know kind of late for me but the help is help anytime.


saludos

#8
Proteo

Proteo

    InsanelyMac Legend

  • Members
  • PipPipPipPipPipPipPip
  • 683 posts
  • Location:Parangaricutirimicuaro
Great thread. Keep these examples coming please!

#9
McSkywalker

McSkywalker

    InsanelyMac Legend

  • Members
  • PipPipPipPipPipPipPip
  • 609 posts
  • Gender:Male
  • Location:Corusant
  • Interests:2D - 3D design<br />Painting<br />Guitar playing<br />Woodworking - Gran...
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


#10
A Nonny Moose

A Nonny Moose

    Proud PPC User

  • Members
  • PipPipPipPipPipPipPipPipPipPip
  • 2,421 posts
  • Gender:Male
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.

#11
Numberzz

Numberzz

    Resident iPhone Expert

  • Retired
  • 3,452 posts
  • Gender:Male
  • Location:San Francisco
how do you make an application quit?

#12
Numberzz

Numberzz

    Resident iPhone Expert

  • Retired
  • 3,452 posts
  • Gender:Male
  • Location:San Francisco
The sample "Select All" script works in Tiger, but not in Leopard 9A303.

#13
A Nonny Moose

A Nonny Moose

    Proud PPC User

  • Members
  • PipPipPipPipPipPipPipPipPipPip
  • 2,421 posts
  • Gender:Male
Check your system preferences and make sure you've enabled assistive devices.

#14
Numberzz

Numberzz

    Resident iPhone Expert

  • Retired
  • 3,452 posts
  • Gender:Male
  • Location:San Francisco
I get the error "The tell statements are nested too deeply." I copied using command-c and then pasting it in the script editor.

#15
A Nonny Moose

A Nonny Moose

    Proud PPC User

  • Members
  • PipPipPipPipPipPipPipPipPipPip
  • 2,421 posts
  • Gender:Male
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.

#16
Numberzz

Numberzz

    Resident iPhone Expert

  • Retired
  • 3,452 posts
  • Gender:Male
  • Location:San Francisco
I get as far as telling System Events to make the Finder a process instead of an app.

#17
McSkywalker

McSkywalker

    InsanelyMac Legend

  • Members
  • PipPipPipPipPipPipPip
  • 609 posts
  • Gender:Male
  • Location:Corusant
  • Interests:2D - 3D design<br />Painting<br />Guitar playing<br />Woodworking - Gran...
Did you try recording your actions in finder in the script editor?

#18
A Nonny Moose

A Nonny Moose

    Proud PPC User

  • Members
  • PipPipPipPipPipPipPipPipPipPip
  • 2,421 posts
  • Gender:Male

how do you make an application quit?


Tell application iChat
quit
end tell

Substitute your app for iChat.

#19
A Nonny Moose

A Nonny Moose

    Proud PPC User

  • Members
  • PipPipPipPipPipPipPipPipPipPip
  • 2,421 posts
  • Gender:Male
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.

#20
~pcwiz

~pcwiz

    InsanelyMac V.I.P.

  • Retired
  • 5,900 posts
  • Gender:Male
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





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

© 2014 InsanelyMac  |   News  |   Forum  |   Downloads  |   OSx86 Wiki  |   Mac Netbook  |   PHP hosting by CatN  |   Designed by Ed Gain  |   Logo by irfan  |   Privacy Policy