Saturday, November 18, 2017

#1 2007-04-16 06:00:26 am

Kevin Bradley
From:: Independence, MO
Registered: 2006-03-13
Posts: 548

"Quick 'n Dirty" Applescript

One of the Popular Kids
The increasing popularity of the Macintosh platform in recent months has been a wonderful thing for those of us who have been with the Mac for many years.  But for those just coming to the Mac (and also to Applescript) it can sometimes be both a difficult adjustment and a new playground to play in.  It is with an eye to those who have NEVER thought that they would find a reason to write an Applescript that I am aiming this week's article.  If you can't figure out what this thing called Applescript is for, here are some quick and dirty examples that will give you an idea of what you can use it for!

Putting It on the Line
Windows and Linux users have been used to using a command line, shell, or terminal window for some time.  For Mac users, it's relatively new.  While the Server version of OS X came out in 1999, the consumer version was not released until 2001, so it's barely 6 years old.  And while you can still open a Terminal window if you like, there are alternatives to doing your work there, like Automator and Applescript, that are sometimes just what you need for a simple command that isn't worth opening a new application for.

Others here at Macscripter have written some excellent tutorials on Automator and on getting started with Applescript and Script Editor.  What I'm covering here won't go over that ground again, so if you need help, check out those articles.  Instead, I'm going to show you how to get some work done with just a few lines of code that you can throw together FAST.

Application Vs. Theory
One thing that Windows users may have difficulty with on the Mac is the lack of a Start Menu.  Apple's Dock has been the subject of much controversy for Mac users - some like it, some hate it. And while there are many good program launchers available for the Mac, sometimes it's just easier to use a script.

One of the nice things about the Mac is that it keeps track of where all your applications are - you don't need to give a full pathname unless you're hacking about in Terminal.  So for our first script, let's create a program launcher that will launch ANY PROGRAM on your Mac!


display dialog "Launch program..." default answer "TextEdit"
copy the text returned of the result to myApp

tell application myApp
end tell

Just type that into Script Editor and save it in your Scripts folder (~/Library/Scripts).  If you haven't activated the Script menu, now might be a good time to do so, as it will help you to run scripts without Script Editor.  When you run this script, it asks what program to run.  You just type the name and it will be launched!

At Your Command
There are also times that you may want to run just one command that you would ordinarily need Terminal for.  Rather than launch the Terminal application for just one command, you can run a simple unix command through Applescript's do shell script command:


set myCmd to text returned of (display dialog "Enter unix Command" default answer "")

   set myResult to do shell script myCmd
   if length of myResult > 0 then display dialog myResult as string buttons {"OK"} default button 1
on error the error_message number the error_number
   display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
end try

Here we ask for a command, and if the length of the result of the command isn't 0 (meaning it is not empty),  display the result in a dialog box.  And we've surrounded the entire thing with a try block, just to catch any errors that might happen.

This script isn't good for commands that have long output - for that, you probably should use Terminal.  But for things like the "uptime" command or to get a quick process listing, it's just fine.

You're Only Fooling Yourself
Now, here's a REALLY dirty trick - getting Applescript to Applescript command!  Yep, you can input a short script and coerce Applescript into running it without entering it in Script Editor!  No kidding.  Here's how:


set myCmd to text returned of (display dialog "Enter Command" default answer "")

   run script myCmd
on error the error_message number the error_number
   display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
end try

Notice any similarity to the Unix command script?  It's basically the same script but instead of do shell script we're using run script.  And we don't wait around for a result (but we could, if we wanted to).  Try typing tell app "Finder" to set its visible to false in the dialog box.  You can also launch applications here.  Try tell app "TextEdit" to activate.

Make It Count!
If you used to use Notepad in Windows or gedit in Linux, you'll appreciate this one.  Often, a full-fledged word processor is too much for a task and many of us prefer to use a simple text editor.  But most text editors lack features - wouldn't it be nice if we could add our OWN features?  You can, with a script!

Most text editors won't count words, but with this script you can get TextEdit to give you a word count for your current document.  Here's the script:


tell application "TextEdit"
   set theText to text of front document
   set wordCount to count (words of theText)
   display dialog (wordCount & " words.") as text
end tell

As written, the script asks for the text of the front document, then counts the words and displays a dialog with the results.  It may sound unbelievable that it is that easy, but it really is.  And that's really what this article is meant to show you - Applescript isn't some difficult thing.  You can do amazing things just with a few lines of code.

Minimum Effort
How easy can a script get?  How about just one line?  If you're like me, you love Exposè, but sometimes want to just clear a bunch of clutter rather than wade through a bunch of open windows.  What if you could minimize all the Finder's windows with just one line of code?  Try this one:


tell application "Finder" to tell every Finder window to set the collapsed to true

These are just some ideas to get you started.  You have seen how, using just a few lines, you can get some things done easier on you Mac than you could have on your old Windows or Linux machine.  Once you get into writing scripts, you'll find even more uses.  The advantage of Applescript is your ability to control almost all of the programs you use daily. 

Where do you go from here?  Here are some suggestions that may help get you on the right path to writing useful scripts:

Craig Smith's beginner's series:
    *Getting Started & Script Editor
    *Variables & Dictionaries
    *Records & Repeats
    *[url=]The Power of Lists[/url]
    *Testing & Shorthand
    *User Interaction
    *Getting the Drop on Droplets

Ben Waldie's series on handlers:
    *Getting Started with Handlers - Part 1
    *Getting Started with Handlers - Part 2
    *Getting Started with Handlers - Part 3

    *Adam Bell's tutorial Lessons & Fun with "do shell script"
    *The Applescript Sourcebook's Where can I find more information about Applescript?
    *And of course, the Macscripter Forums!

Nitewing '98
I distrust morning people, largely because I suspect them of getting together early one day while the rest of us were asleep and setting up the rules of civilization.



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)