Re: OOP AppleScript and self-initialization
Re: OOP AppleScript and self-initialization
- Subject: Re: OOP AppleScript and self-initialization
- From: has <email@hidden>
- Date: Sat, 9 Aug 2008 13:22:23 +0100
Kevin Muldoon wrote:
I have been experimenting with OOP and self-initialization in
applescript.
Below is a sample of what I believe to be an elegant way to create
objects and have them initialize with one line as well as
having the object able to initialize itself.
It's not. OOP's meant to reduce complexity, not generate it. To be
honest, your code doesn't seem to do anything that couldn't be done
procedurally in a fraction of the size, and without having to wrap and
unwrap your underlying alias values every time you want to use them in
an API that doesn't recognise your custom wrapper objects (basically,
any scriptable application or osax).
Also, mutating your object's state in the 'cp' method to point to the
duplicated file is poor design and liable to cause confusion. Better
to create a new wrapper object for the new file and return that.
However, when my full
code is implemented and the script is saved as an application, I can
only run the app a few times before
a dialog stating "script doesn't understand fobj..." pops up. I
suspect a memory issue but I set my obj's to null (thinking
I'm managing the memory) so I'm confused as to why this is an issue.
AppleScript's memory management is automatic, so you don't 'manage' it
yourself as such. Some general advice though:
1. Beware circular references between objects; IIRC, this will result
in your script leaking memory once these objects go out of scope as
AS's memory management isn't the brightest.
2. Never use the 'copy' command on script objects; it performs a deep
copy that duplicates not only your script object, but all of its
parents, including your top level script and everything in it,
resulting in massive amounts of duplication. This can blow up
AppleScript very easily.
...
Anyway, if you really must implement your filesystem-related commands
as a wrapper object rather than just a simple procedural library, here
is how I'd structure it (not production-quality or tested):
on makeAliasWrapper(fileRef)
script AliasWrapper
property _fileRef : fileRef as alias
on getName()
return name of (info for _fileRef)
end getName
on toAlias()
return _fileRef
end toAlias
on toPath()
return POSIX path of _fileRef
end toPath
on copyTo(newFileRef)
do shell script "cp -R " & quoted form of POSIX path of _fileRef ¬
& space & quoted form of POSIX path of newFileRef
return makeAliasWrapper(newFileRef)
end copyTo
end script
end makeAliasWrapper
-- TEST
set f1 to makeAliasWrapper(alias "path:to:file1")
set f2 to f1's copyTo(alias "path:to:file2")
f2's toAlias()
As with anything else, KISS. (Doubly so for AppleScript.)
BTW, if you want to see some existing examples of OOP:
HTH
has
--
Control AppleScriptable applications from Python, Ruby and ObjC:
http://appscript.sourceforge.net
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden