Flylib.com

Books Software

 
 
 

Section 11.11. Assignment of Multiple Attributes


11.11. Assignment of Multiple Attributes

Recall from Chapter 7 that it is possible to assign multiple values in a single command by using a list:

set {x, y, z} to {1, 2, 3}

You can use this syntax to fetch multiple attributes , using either tell or of :

tell application "Finder"
    set {x, y} to {name, comment} of folder 1
end tell
{x, y} --

{"Mannie", "howdy"}


That code fetches name of folder 1 and comment of folder 1 from the Finder in a single command.

You can use this construct to set multiple properties as well, but only in a tell block (trying to do it with of will cause a runtime error):

tell application "Finder"
    tell folder "Mannie"
        set {comment, name} to {"zowie", "Jack"}
    end tell
end tell

Be careful of the order in which you list the properties when assigning to them. The values are assigned from left to right. This wouldn't have worked:

tell application "Finder"
    tell folder "Mannie"
        set {name, comment} to {"Jack", "zowie"} --

error

end tell
end tell

That code sets the name first, and afterwards there is no longer a folder "Mannie" to set the comment of, so the attempt to set the comment of folder "Mannie" causes a runtime error.



11.12. Object String Specifier

For certain objects in the "real world" (that is, the real world inside the computer), AppleScript has a bootstrap problem. It needs a way to refer to these objects, yet they are not attributes of anything. In fact, they are the things that have attributes. So in order to talk about anything at all outside of the script, AppleScript must pull itself up by its bootstraps.

The solution is an object described using the name of the class followed by a string. It looks rather like an element specifier by name; but the object isn't an element of anything, and the string isn't exactly a name . There is no official term for this construct in Apple's documentation, so I call it an object string specifier . The main real-world objects for which object string specifiers are used are applications, files and aliases, and dates. Details appear in Chapter 13, but here are some examples.

Throughout this chapter I've constructed application targets using an object string specifier with the application class:

tell application "BBEdit"

In the case of a file or an alias, the string is a pathname:

get POSIX path of file "myDisk:myFile"

The string doesn't have to be a literal; a variable will work just as well:

set f to "myDisk:myFile"
get POSIX path of file f



Chapter 12. References

References are an important feature of AppleScript, and many values that you'll encounter using AppleScript will be references. A reference is a special type of data. It isn't the actual data; it's more like a pointer to the data. But it isn't a pointer, either; if you're used to pointers or indirect addressing from some other computer language, a reference is something else again. Remember, AppleScript is all about communicating with scriptable applications. In replying to a communication, the scriptable application might hand you a reference. In this context, a reference is a very powerful thing; it's like an Apple event primed and ready to communicate with an object in that application's world. A reference can be a great convenience, or it can be a danger: if you're not careful, you can send that Apple event and tell that scriptable application to do something you never intended. Unfortunately, AppleScript goes to some lengths to make it difficult for you to know that you've got a reference in the first place! This chapter explains what references are and how to know when you've got one. It also shows how to make a reference deliberately, and how to do some powerful things with references.

The phrase "by reference," as in "Set by Reference" in Chapter 7 and "Pass by Reference" in Chapter 9, is not what this chapter is about. When you pass a list "by reference" as a parameter to a handler, you do not pass a reference; you pass a list in a certain way. To make things even more complicated, we'll talk about passing a reference as a way to simulate passing by reference! The identity of the terminology is unfortunate but unavoidable.