• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: a script object (class) as a member of another script object
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: a script object (class) as a member of another script object


  • Subject: Re: a script object (class) as a member of another script object
  • From: Paul Berkowitz <email@hidden>
  • Date: Mon, 29 Sep 2003 00:15:31 -0700

On 9/28/03 10:41 PM, "Rua Haszard Morris" <email@hidden> wrote:

>
> I'm new to applescript, and am coming to terms with its concept of
> 'class'/'object' - 'script object', right?
>
> The question I have is how do I define a class in one file (so I can have a
> lib which is my class/script object, let's say a counter object), and use it
> in another as a property? To be clear i want to use an instance of a script
> object (class) as a property (member) of another class.
>
> e.g.
> file counter.lib:
> script Counter
> property Max : 5
> property Current : 1;
> property NumWrapArounds : 0;
>
> on Count
> set my Max to my Max + 1
> ...
> end
>
> ...
> end
>
> file counteruser:
> -- import the counter.lib file somehow? maybe set counterlib to load script
> file "counter.lib"
> script CounterUser
> property mecounterobj : Counter --i.e. an instance of the counter above
>
> ...
> end
>
> Is there a way to do this?
>
> If so, assuming that there are multiple ways to do it (as there are a couple
> to define a class I beleive), what is the 'normal'/'correct' method?
>
> Any help would be greatly appreciated...


Here's one way:

file counter.lib - saved to desktop as a compiled script:

------------------------

script Counter
property Max : 5
property Current : 1
property NumWrapArounds : 0

on count
set my Max to (my Max) + 1
display dialog ("" & Max)
end count

end script

------------------------

file counteruser:

----------------------

set counterLib to load script alias ((path to desktop as Unicode text) &
"counter.lib")

set CounterUser to counterLib's Counter

tell CounterUser to count

--> "6"
---------------------


You actually don't need a separate script object inside the script file
"counter,lib". The compiled script is itself a script object. Of course you
may have lots of other script objects, handlwers, what-have-you, inside that
file. But if not, this will also work perfectly well:


file counter.lib - saved to desktop as a compiled script:

------------------------

property Max : 5
property Current : 1
property NumWrapArounds : 0

on count
set my Max to (my Max) + 1
display dialog ("" & Max)
end count


------------------------

file counteruser:

----------------------

set counterLib to load script alias ((path to desktop as Unicode text) &
"counter.lib")

tell counterLib to count

--> "6"
---------------------

But 'count' is an AppleScript reserved word, and seems to allow a handler
('on count') without parentheses. This is exceptional, and to be avoided.
You're using 'count' as a sort of placeholder for a handler term. You should
use your own handler term, such as

on increment()
set my Max to (my Max) + 1
display dialog ("" & Max)
end increment


Now, you can use "factory" handlers to generate as many separate instances
of script objects as you might care to have, real OOP:

file counter.lib - saved to desktop as a compiled script:

------------------------

to GenerateNewCounter()

script Counter
property Max : 5
property Current : 1
property NumWrapArounds : 0

on increment()
set my Max to (my Max) + 1
display dialog ("" & Max)
end increment
end script

end GenerateNewCounter

------------------------


file counteruser:

----------------------

set counterLib to load script alias ((path to desktop as Unicode text) &
"counter.lib")

tell counterLib to set counterUser1 to GenerateNewCounter()

tell counterUser1 to increment()
--> "6"
tell counterUser1 to increment()
--> "7"

tell counterLib to set counterUser2 to GenerateNewCounter()
tell counterUser2 to increment()
--> "6"
tell counterUser1 to increment()
--> "8"

---------------------


I'm sure there's more to come....
--
Paul Berkowitz
_______________________________________________
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.

References: 
 >a script object (class) as a member of another script object (From: "Rua Haszard Morris" <email@hidden>)

  • Prev by Date: Re: a script object (class) as a member of another script object
  • Next by Date: Cumulus: Close Information Window
  • Previous by thread: Re: a script object (class) as a member of another script object
  • Next by thread: Cumulus: Close Information Window
  • Index(es):
    • Date
    • Thread