Sunday, April 15, 2007

For hte Editing


Because of popular demand (2 for, none against) I’m going to try to do a week of AppleScript tips. (Last week was supposed to be a week of Safari tips.) I’m going to be taking reasonably sized AppleScript steps.

Today is all about dialog boxes. So, lets get clicking!

First of all, you need to a dialog box to show up. Type:

display dialog "I love the MacTipper Blog!"


Run it. (Cmd-R)

You’ll notice that it gave you the options of “OK” or “Cancel”.
What happens if you do not want to be typical and have "Ok" or "Cancel," instead you want something with a little more panosh. Type:

display dialog "I love the MacTipper Blog!" buttons {"Leave me alone!", "Let's go there!"} default button 2

Now, when you run it, it will give you special buttons instead of the boring ones!
The default button is listed as either "button 1" (on the left) or "button 2) on the right).


Codeing along, we want the app to actually do something. Therefore type:

set question to display dialog "I love the MacTipper Blog!" buttons {"Leave me alone!", "Let's go there!"} default button 2
set answer to button returned of question


Since I have a house full of computer geeks always reminding me to tighten up the code, notice what I added at the beginning...

This does nothing noticeable. However you can make it useful by adding: property target_URL : "http://www.mactipper.com" to the beginning and then typing:

set question to display dialog "I love the MacTipper Blog!" buttons {"Leave me alone!", "Let's go there!"} default button 2
set answer to button returned of question

if answer is equal to "Let's go there!" then
open location target_URL
end if


This will launch the website in your favorite browser. However, you can edit this to more complicated stuff. Such as:

property blgurl : "http://www.mactipper.com"
property aplwebsite : "http://www.apple.com"

set question to display dialog "I love the MacTipper Blog!" buttons {"Leave me alone!", "Let's go there!"} default button 2
set answer to button returned of question

if answer is equal to "Let's go there!" then
open location blgurl
end if

if answer is equal to "Leave me alone!" then
open location aplwebsite
end if



Note that you can edit this so that it opens up your blog url or your favorite website. Now, all you have to do is save it as an app and start clicking!

Congratulations, you’ve completed day 1 of the Week of Applescript! Tomorrow we’ll talk a bit more in depth about dialog boxes! If you think that what I’ve been writing is a “Waste of ones and zeroes” then say so in the comments!

No comments: