You'll get your Mac news here from now on...

Help TMO Grow

Subscriber Login

Advertising Info


The Mac Observer Express Daily Newsletter


More Info

Site Navigation

Home
News
Tips
Columns & Editorials
Reviews
Reports
Archives
Search
Forums
Links
Mac Links
Software
Reports
Contact



by Stephen Swift


AppleScript Properties
May 29th, 2002

This marks the 7th installment of AppleScript: The Macintosh Autopilot. If you find any of this tutorial not making sense, check out the some of the earlier installments.

The type of property I'm going to show you today is a special type of variable. Remember variables? Read Variables & Other Stories to refresh your memory. You can set a property to represent data like regular variables, but unlike variables, the value of the property is stored permanently in the memory banks of the script.

How It Works

Properties are defined at the beginning of your script. To define a variable we simply type:

set theVariable to "The value."

To define a property, the code looks a little different.

property theVariable : "The data to remember."

What's the difference between a property and a variable?

If you change the value of a property, the change is permanent. If you change the value of a variable, the change is temporary. Let's looks at a few scripts to illustrate the differences.

Script 1:

set the_text to "I love AppleScript!"

(*Read Variables and Other Stories for an explanation of this code.*)

display dialog the_text

(*Read Displaying Dialog Boxes if you are unsure of how to create dialog boxes.*)

set the_text to "I love my Mac!"

(*We changed the value of the_text*)

display dialog the_text

Run this script. No surprises right? Two dialog boxes pop up with the different messages. Now run the script again. Same thing right? Great!

Script 2:

property the_text : "I love AppleScript!"

(*Now the_text is a property instead of a variable.*)

display dialog the_text

(*Read Dialog Boxes if you are unsure of how to create dialog boxes.*)

set the_text to "I love my Mac!"

(*We changed the value of the_text*)

display dialog the_text

Run this script. It's the same thing as before right? Now run the script again. Now both of the dialog boxes read "I love my Mac!" What happened?

When we changed the value of the property, the_text, the script replaced the old value with the new value. It's almost as if AppleScript went into the script editor and changed our code in the first line and deleted our old value and typed in the new value. Of course, it didn't actually do that. When we open the script in the script editor, we still see our original line. AppleScript doesn't look at that code anymore. That code is only there if our variable doesn't have an assigned value already. Because it does have a value, AppleScript ignores that line and continues on reading the rest of the script.

When would I use a property?

Great question! You would use a property when you want to store information. Say you have a script that needs the name of the person running the script. It would be annoying for the user to have to give his or her name each time the script ran. You can have the script ask for the name once and from then on use the name the user entered. For example:

property the_name : ""

(*Creates a new property.*)

if the_name = "" then

(*We need to ask the user for a name if there is no name supplied.*)

(*We can use an if statement to determine if there is a name or not.*)

(*Read "Your First Script" if you are unsure how to use if statements.*)

set the_name to text returned of (display dialog "What is your name?" default answer "")

(*We've set the value of the_name to be whatever the user types in.*)

else

display dialog "Hello " & the_name

end if

Run this script and it asks you for your name. Now run this script again and it remembers your name.

You would also use properties if you needed to constantly change the value of a variable. If you were using a standard variable, you would have to open the script in the script editor and change the code. However, if you use a property, you can easily change the value using a dialog box. For example:

property fav_website : "www.macobserver.com"

(*Creates a new property.*)

set the_dialog to display dialog "Your current favorite Web site is " & fav_website & ". Type in your new favorite Web site." default answer fav_website

set fav_website to text returned of the_dialog

(*We've set the value of fav_website to be whatever the user types in.*)

Now, you can change the value of fav_website easily through this dialog.

A More Practical Example

Take a look at the Connect To script we created a while back. Say you wanted the last place you typed in to appear in the dialog box instead of a default location. You would need to use a property to store the last place you typed in. I'll show you how.

Here's the original script:

display dialog "Type in a URL." default answer "http://www.macobserver.com" buttons ["Cancel", "Connect"] default button 2

if text returned of result = "" then

open location (text returned of result)

else

display dialog "You didn't enter a URL." buttons ["OK"] default button 1

end if

The first thing you need to do is replace "http://www.macobserver.com" with a variable such as theURL. Then you need to add this line to the top of the script:

property theURL : "http://www.macobserver.com"

This turns theURL into a property that is permanently remembered by the script.

The next thing you have to do is to change theURL to reflect the new value that the user typed in. Underneath the line "if text returned of result = "" then" add this line:

set theURL to (text returned of result)

This will change the value of theURL to whatever the user typed in. Because theURL is a property, AppleScript will replace the old value with the new value. The next time the user runs the script, the new URL will come up into the dialog box instead of the old one.

The complete code is:

property theURL : "http://www.macobserver.com"

set the_dialog to display dialog "Type in a URL." default answer theURL buttons ["Cancel", "Connect"] default button 2

if text returned of the_dialog = "" then

set theURL to (text returned of the_dialog)

open location theURL

else

display dialog "You didn't enter a URL." buttons ["OK"] default button 1

end if

The Last Word

If you alter the script (i.e.: edit it in the Script Editor), the new value of the variable will be lost. This is because after a recompile (when AppleScript looks at new code) the property reverts to the default value (the original code). In this case, it would be "http://www.macobserver.com".

The Wrap-Up

1) We learned what a property is and how to use it.
a. A property saves the value of a variable into the script so that it may be recalled at a later point in time.
b. You use the line: set theVariable to "The data to remember" to change the value of the property.
2) If you change the value of the property, AppleScript replaces the old value with the new value. When you call the script in the future, the new value is used and not the old one.
3) After a recompile, the script reverts to the default value defined at the beginning of the script.

Next Time...

You can look forward to learning how to organize your data into lists and records. Lists make storing and recalling data easier. They are a lot of fun and are easy to learn.

More Info

Join the AppleScript gang at the Coder's Corner in The Mac Observer Forums! We discuss AppleScript related news and updates, extract really cool and useful AppleScripts, ask questions, and give answers. It's a lot of fun and a great resource for all your scripting needs! Also, if you have a befuddling AppleScript problem, please send it to Stephen@macobserver.com and I'll be more than willing to help you out!


Comments or Questions? Is this column going to slow or fast for you? Do you want to script something, but don't know how? Do you need something explained or have a question about a script?  My E-mail address, stephen@macobserver.com, is open 24 hours a day, 7 days a week.


Most Recent AppleScript Columns

Break Your Script Into Pieces with Subroutines
June 26th

Repeat Yourself With AppleScript
June 12th

AppleScript Lists
June 5th

AppleScript Archives

Back to The Mac Observer For More Mac News!



Today's Mac Headlines

[Podcast]Podcast - Apple Weekly Report #135: Apple Lawsuits, Banned iPhone Ad, Green MacBook Ad

We also offer Today's News On One Page!

Yesterday's News

 

[Podcast]Podcast - Mac Geek Gab #178: Batch Permission Changes, Encrypting Follow-up, Re-Enabling AirPort, and GigE speeds

We also offer Yesterday's News On One Page!

Mac Products Guide
New Arrivals
New and updated products added to the Guide.

Hot Deals
Great prices on hot selling Mac products from your favorite Macintosh resellers.

Special Offers
Promotions and offers direct from Macintosh developers and magazines.

Software
Browse the software section for over 17,000 Macintosh applications and software titles.

Hardware
Over 4,000 peripherals and accessories such as cameras, printers, scanners, keyboards, mice and more.

© All information presented on this site is copyrighted by The Mac Observer except where otherwise noted. No portion of this site may be copied without express written consent. Other sites are invited to link to any aspect of this site provided that all content is presented in its original form and is not placed within another .