(1 of 2) Application Scripting Question - Theoretical? (was Re: Technote 2106 is da Bomb)
(1 of 2) Application Scripting Question - Theoretical? (was Re: Technote 2106 is da Bomb)
- Subject: (1 of 2) Application Scripting Question - Theoretical? (was Re: Technote 2106 is da Bomb)
- From: Michael Terry <email@hidden>
- Date: Fri, 9 Apr 2004 18:15:15 -0700
Hi,
I hope this post makes some sense; it's a little bit dashed off. Sorry
for the length too, but I'd rather include too much than too little.
Thanks to Technote 2106, I think I finally understand application
scripting, and further, what I'm allowed to expect from AS developers.
Now, I've got a question. How can I set a whole bunch of application
object properties at once, where each property is different?
Let's use the Finder for a minute, because it's well scriptable and
everybody has it. You can do this:
tell application "Finder" to get name of files 1 thru 2 of item (choose
folder)
--> {"AS dictionary palette.ooutline", "hello"}
and you get a list of the names of the objects specified. But what
about doing the reverse (or converse of obverse or contrapositive or
whatever you call it)? I want to be able to set the names of the first
two files of a folder in a single Apple event.
I picked a bit of a bad first example since names must be unique in
their folder, but we can work around that by changing the query a
little:
set f to (choose file)
tell application "Finder"
get name of first file of folders 1 thru 2 of f
end
--> {"test1", "test1"}
The 'name of first file of folders 1 thru 2 of f' part (object
specifier) identifies a particular set of properties. Using the 'get'
command, we return their values. I want to set them. By itself, that's
not a problem at all:
tell application "Finder"
set name of first file of folders 1 thru 2 of f to "whatever"
end tell
The problem is, you have to assign all the properties to the same
value. As far as I can tell, there's no way in standard AppleScript to
assign multiple unique values. This seems like a big limitation. You
can create these complex object specifiers which get a large collection
of lvalues (assignable locations), but all you can do with them is set
them all at once to the same value.
The lvalue concept is tricky, so let me be clear about what I'm talking
about. A variable is an lvalue, a script property is an lvalue, and an
application object specifier pointing to a property is an lvalue.
Anything you can put on the left hand side of a set operation is an
lvalue.
I don't know about other languages, but in AppleScript--with exceptions
I'll get to in a moment--you use lvalues by coding them directly in
where you want them. There's not a lot of indirection available. You
write:
set x to 3
and you specify 'x' as the lvalue by typing it where you want. You
write:
set name of file 1 of f of app "Finder" to "whathaveyou"
and you specify the lvalue by typing the object specifier 'name of file
1 of f of app "Finder"' directly into the code.
If all you want to do is set multiple properties in a single statement,
you *can* do it:
tell application "Finder"
set {name of first file of folder 1 of f, name of first file of folder
2 of f} to {"something", "else"}
end tell
This suffers from at least two drawbacks. First, the lvalues have to be
hard coded into the script. That's not very flexible or powerful. What
if they didn't have to be, though? What if an application could return
property object specifiers? I've never seen this happen--is it
possible? At first, I thought maybe something like this would be the
way to go (note: this code doesn't work):
tell application "Finder"
get a reference to name of first file of folders 1 thru 2 of f
end tell
--> {a reference to name of document file "something" of folder
"testFold1" of folder "Temporary Items" of startup disk, a reference to
name of document file "else" of folder "testFold2" of folder "Temporary
Items" of startup disk}
(...continued...)
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.