A Nonny Moose Posted June 16, 2006 Share Posted June 16, 2006 (edited) 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 July 16, 2006 by A Nonny Moose Link to comment https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/ Share on other sites More sharing options...
Colonel Posted June 16, 2006 Share Posted June 16, 2006 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. ichatscript.zip Link to comment https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-130512 Share on other sites More sharing options...
Korrupted Posted June 16, 2006 Share Posted June 16, 2006 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" Link to comment https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-130521 Share on other sites More sharing options...
A Nonny Moose Posted June 17, 2006 Author Share Posted June 17, 2006 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 https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-131253 Share on other sites More sharing options...
Swad Posted June 20, 2006 Share Posted June 20, 2006 Great thread! Definitely sticky worthy. Link to comment https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-132320 Share on other sites More sharing options...
A Nonny Moose Posted June 21, 2006 Author Share Posted June 21, 2006 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 https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-132720 Share on other sites More sharing options...
Zealot Posted November 2, 2006 Share Posted November 2, 2006 good to know kind of late for me but the help is help anytime. saludos Link to comment https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-223547 Share on other sites More sharing options...
Proteo Posted November 2, 2006 Share Posted November 2, 2006 Great thread. Keep these examples coming please! Link to comment https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-223562 Share on other sites More sharing options...
McSkywalker Posted November 2, 2006 Share Posted November 2, 2006 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 https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-223646 Share on other sites More sharing options...
A Nonny Moose Posted November 2, 2006 Author Share Posted November 2, 2006 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. Link to comment https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-223758 Share on other sites More sharing options...
Numberzz Posted November 21, 2006 Share Posted November 21, 2006 how do you make an application quit? Link to comment https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-238986 Share on other sites More sharing options...
Numberzz Posted November 21, 2006 Share Posted November 21, 2006 The sample "Select All" script works in Tiger, but not in Leopard 9A303. Link to comment https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-239253 Share on other sites More sharing options...
A Nonny Moose Posted November 21, 2006 Author Share Posted November 21, 2006 Check your system preferences and make sure you've enabled assistive devices. Link to comment https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-239448 Share on other sites More sharing options...
Numberzz Posted November 22, 2006 Share Posted November 22, 2006 I get the error "The tell statements are nested too deeply." I copied using command-c and then pasting it in the script editor. Link to comment https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-239705 Share on other sites More sharing options...
A Nonny Moose Posted November 23, 2006 Author Share Posted November 23, 2006 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. Link to comment https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-240307 Share on other sites More sharing options...
Numberzz Posted November 23, 2006 Share Posted November 23, 2006 I get as far as telling System Events to make the Finder a process instead of an app. Link to comment https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-240898 Share on other sites More sharing options...
McSkywalker Posted November 23, 2006 Share Posted November 23, 2006 Did you try recording your actions in finder in the script editor? Link to comment https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-240903 Share on other sites More sharing options...
A Nonny Moose Posted November 24, 2006 Author Share Posted November 24, 2006 how do you make an application quit? Tell application iChat quit end tell Substitute your app for iChat. Link to comment https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-241498 Share on other sites More sharing options...
A Nonny Moose Posted December 31, 2006 Author Share Posted December 31, 2006 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. Link to comment https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-265231 Share on other sites More sharing options...
~pcwiz Posted March 4, 2008 Share Posted March 4, 2008 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 Link to comment https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-653436 Share on other sites More sharing options...
Moose Tracks Posted December 3, 2009 Share Posted December 3, 2009 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 https://www.insanelymac.com/forum/topic/19843-osx86-projects-applescript-tutorial/#findComment-1349303 Share on other sites More sharing options...
Recommended Posts