Re: Script objects 'as string' in an identifiable way
Re: Script objects 'as string' in an identifiable way
- Subject: Re: Script objects 'as string' in an identifiable way
- From: email@hidden
- Date: Tue, 6 Nov 2001 13:26:39 -0500
On Mon, 5 Nov 2001 20:39:43 +0000, has <email@hidden> asked,
>
Why not name the script in the constructor? Or is there a reason for making
>
it anonymous?
The name of a script object is not too useful. Its not accessible, it doesn't
differentiate between two instances, and it goes away if you copy the script
object. That is, if I use the script object constructor,
to makefoo(t)
script foo
property tag : t
end script
end makefoo
set f1 to makefoo(1)
set f2 to makefoo(2)
copy f1 to f3
{f1, f2, f3, tag of f1, tag of f2, tag of f1 }
--> result: <<script foo>>, <<script foo>>, <<script>>, 1, 2, 1
So, seeing <<script foo>> in the result doesn't tell you which instance of foo,
and that name "foo" disappears when you copy the object.
>
Adding an index property sounds a pretty good idea... think I'll do that
>
myself (maybe a name property too, if only for security against 'copy').
I wouldn't put in indexes or names unless you really need them. The whole point
of object-oriented design is to abstract away the details. Let the computer
keep track of the separate instances. You can use "=" or "is" to compare two
script objects to see if they are really the same one. (And they have to be the
same object, as if created by "set f3 to f1" and not "copy f1 to f3".)
If in your design, you have indexes or names, and rely on them, that means
you've let details about the object leak out, and the script objects have become
coupled to each other and to the surrounding code. Its better to reduce the
coupling, to make each object a stand-alone entity, that can do itself most of
what needs to be done. Sure, the particular problem may require each object to
have a unique identifier or each class of object to have a string name, but I
wouldn't add it as a matter of policy.
>
Any other useful tricks one can do with script objects and constructors?
1. Keep in mind, as you are aware, that "set" and "copy" are different things.
2. You can pass script objects between applications, but its pretty slow. I
have one script server which I call in a block that looks like this:
tell (Storage for "Proposals) of application "Storage Objects"
doSomething()
doSomethingElse for "blah"
set x to someThirdHandler(1,2,3)
end tell
But it was much faster to get the script object once, and tell it to do many
things, than to tell the server many times to get each thing. That is,
tell (Storage for "Proposals) of application "Storage Objects"
repeat 100 times
doSomething()
end repeat
end tell
takes 51 ticks, while
repeat 100 times
tell application "Storage Objects" to doSomething() of Storage
end repeat
takes 2199 ticks.
--
Scott Norton Phone: +1-703-299-1656
DTI Associates, Inc. Fax: +1-703-706-0476
2920 South Glebe Road Internet: email@hidden
Arlington, VA 22206-2768 or email@hidden