Re: Inheritance and Loaded Libraries
Re: Inheritance and Loaded Libraries
- Subject: Re: Inheritance and Loaded Libraries
- From: David Weiss <email@hidden>
- Date: Thu, 28 Oct 2004 13:09:53 -0700
Let me provide more detail.
I'm trying to do two things at once here:
1. I need to have a text script that when compiled and run has access to all properties and methods of around 26 dynamically loaded libraries. The libraries will be loaded as needed. Additionally each of the libraries needs to have access to each other's properties and methods. I've succeeded in doing this in a single script file with inheritance, but I have several thousand scripts. Duplicating the library code in each script is not an option. Here's the test script that I wrote to show and test the exact solution I want:
script ParentObject
property identifier : "ParentObject"
property XLib : missing value
property PrefLib : missing value
property InstallLib : missing value
-- simiplified, add about 20 more script libraries here
property proveIt : false
property proveFunctionTest : false
on functionTest()
if my proveFunctionTest then display dialog "functionTest() in ParentObject"
return my identifier
end functionTest
on testFunctions()
if my proveIt then display dialog "testFunctions() in ParentObject"
-- access functions
if (my XLib's functionTest()) is not "XLib" then error "access to XLib's functionTest() from " & my name & " failed"
if (my PrefLib's functionTest()) is not "PrefLib" then error "access to PrefLib's functionTest() from " & my name & " failed"
if (my InstallLib's functionTest()) is not "InstallLib" then error "access to InstallLib's functionTest() from " & my name & " failed"
if (my test's functionTest()) is not "test" then error "access to test's functionTest() from " & my name & " failed"
-- infinite loop if (my parent's functionTest()) is not "ParentObject" then error "access to parent's functionTest() from " & my name & " failed"
end testFunctions
on initialize()
set my XLib to getLib("XLib")
set my PrefLib to getLib("PrefLib")
set my InstallLib to getLib("InstallLib")
end initialize
on theLibraryName)
-- simiplified, I'd load the libraries from external files here.
if theLibraryName is "XLib" then
script XLib
property parent : ParentObject
property identifier : "XLib"
property name : "XLib"
on accessProperties()
if my proveIt then display dialog "accessProperties() in XLib"
-- access properties
if (my XLib's name) is not "XLib" then error "access to XLib's name property from " & my name & " failed"
if (my PrefLib's name) is not "PrefLib" then error "access to PrefLib's name property from " & my name & " failed"
if (my InstallLib's name) is not "InstallLib" then error "access to InstallLib's name property from " & my name & " failed"
if (my test's name) is not "test" then error "access to test's name property from " & my name & " failed"
if (my parent's identifier) is not "ParentObject" then error "access to my parent's identifier property from " & my name & " failed"
end accessProperties
on functionTest()
if my proveFunctionTest then display dialog "functionTest() in XLib"
return my identifier
end functionTest
on testFunctions()
if my proveIt then display dialog "testFunctions() in XLib"
-- access functions
-- infinite loop if (my XLib's functionTest()) is not "XLib" then error "access to XLib's functionTest() from " & my name & " failed"
if (my PrefLib's functionTest()) is not "PrefLib" then error "access to PrefLib's functionTest() from " & my name & " failed"
if (my InstallLib's functionTest()) is not "InstallLib" then error "access to InstallLib's functionTest() from " & my name & " failed"
if (my test's functionTest()) is not "test" then error "access to test's functionTest() from " & my name & " failed"
if (my parent's functionTest()) is not "ParentObject" then error "access to parent's functionTest() from " & my name & " failed"
end testFunctions
end script
return XLib
else if theLibraryName is "PrefLib" then
script PrefLib
property parent : ParentObject
property identifier : "PrefLib"
property name : "PrefLib"
on accessProperties()
if my proveIt then display dialog "accessProperties() in PrefLib"
-- access properties
if (my XLib's name) is not "XLib" then error "access to XLib's name property from " & my name & " failed"
if (my PrefLib's name) is not "PrefLib" then error "access to PrefLib's name property from " & my name & " failed"
if (my InstallLib's name) is not "InstallLib" then error "access to InstallLib's name property from " & my name & " failed"
if (my test's name) is not "test" then error "access to test's name property from " & my name & " failed"
if (my parent's identifier) is not "ParentObject" then error "access to my parent's identifier property from " & my name & " failed"
end accessProperties
on functionTest()
if my proveFunctionTest then display dialog "functionTest() in PrefLib"
return my identifier
end functionTest
on testFunctions()
if my proveIt then display dialog "testFunctions() in PrefLib"
-- access functions
if (my XLib's functionTest()) is not "XLib" then error "access to XLib's functionTest() from " & my name & " failed"
-- infinite loop if (my PrefLib's functionTest()) is not "PrefLib" then error "access to PrefLib's functionTest() from " & my name & " failed"
if (my InstallLib's functionTest()) is not "InstallLib" then error "access to InstallLib's functionTest() from " & my name & " failed"
if (my test's functionTest()) is not "test" then error "access to test's functionTest() from " & my name & " failed"
if (my parent's functionTest()) is not "ParentObject" then error "access to parent's functionTest() from " & my name & " failed"
end testFunctions
end script
return PrefLib
else if theLibraryName is "InstallLib" then
script InstallLib
property parent : ParentObject
property identifier : "InstallLib"
property name : "InstallLib"
on accessProperties()
if my proveIt then display dialog "accessProperties() in InstallLib"
-- access properties
if (my XLib's name) is not "XLib" then error "access to XLib's name property from " & my name & " failed"
if (my PrefLib's name) is not "PrefLib" then error "access to PrefLib's name property from " & my name & " failed"
if (my InstallLib's name) is not "InstallLib" then error "access to InstallLib's name property from " & my name & " failed"
if (my test's name) is not "test" then error "access to test's name property from " & my name & " failed"
if (my parent's identifier) is not "ParentObject" then error "access to my parent's identifier property from " & my name & " failed"
end accessProperties
on functionTest()
if my proveFunctionTest then display dialog "functionTest() in InstallLib"
return my identifier
end functionTest
on testFunctions()
if my proveIt then display dialog "testFunctions() in InstallLib"
-- access functions
if (my XLib's functionTest()) is not "XLib" then error "access to XLib's functionTest() from " & my name & " failed"
if (my PrefLib's functionTest()) is not "PrefLib" then error "access to PrefLib's functionTest() from " & my name & " failed"
-- infinite loop if (my InstallLib's functionTest()) is not "InstallLib" then error "access to InstallLib's functionTest() from " & my name & " failed"
if (my test's functionTest()) is not "test" then error "access to test's functionTest() from " & my name & " failed"
if (my parent's functionTest()) is not "ParentObject" then error "access to parent's functionTest() from " & my name & " failed"
end testFunctions
end script
return InstallLib
end if
end getLib
end script
script test
property parent : ParentObject
property identifier : "test"
property name : "test"
on run
tell application "Finder"
-- access properties
if (my XLib's name) is not "XLib" then error "access to XLib's name property from " & my name & " failed"
if (my PrefLib's name) is not "PrefLib" then error "access to PrefLib's name property from " & my name & " failed"
if (my InstallLib's name) is not "InstallLib" then error "access to InstallLib's name property from " & my name & " failed"
if (my test's name) is not "test" then error "access to test's name property from " & my name & " failed"
if (my parent's identifier) is not "ParentObject" then error "access to my parent's identifier property from " & my name & " failed"
my XLib's accessProperties()
my PrefLib's accessProperties()
my InstallLib's accessProperties()
my test's accessProperties()
-- set my parent's identifier to "blah"
-- access functions
if (my XLib's functionTest()) is not "XLib" then error "access to XLib's functionTest() from " & my name & " failed"
if (my PrefLib's functionTest()) is not "PrefLib" then error "access to PrefLib's functionTest() from " & my name & " failed"
if (my InstallLib's functionTest()) is not "InstallLib" then error "access to InstallLib's functionTest() from " & my name & " failed"
if (my test's functionTest()) is not "test" then error "access to test's functionTest() from " & my name & " failed"
if (my parent's functionTest()) is not "ParentObject" then error "access to parent's functionTest() from " & my name & " failed"
my XLib's testFunctions()
my PrefLib's testFunctions()
my InstallLib's testFunctions()
my test's testFunctions()
end tell
end run
on testFunctions()
if my proveIt then display dialog "testFunctions() in test"
-- access functions
if (my XLib's functionTest()) is not "XLib" then error "access to XLib's functionTest() from " & my name & " failed"
if (my PrefLib's functionTest()) is not "PrefLib" then error "access to PrefLib's functionTest() from " & my name & " failed"
if (my InstallLib's functionTest()) is not "InstallLib" then error "access to InstallLib's functionTest() from " & my name & " failed"
-- infinite loop if (my test's functionTest()) is not "test" then error "access to test's functionTest() from " & my name & " failed"
if (my parent's functionTest()) is not "ParentObject" then error "access to parent's functionTest() from " & my name & " failed"
end testFunctions
on accessProperties()
if my proveIt then display dialog "accessProperties() in test"
-- access properties
if (my XLib's name) is not "XLib" then error "access to XLib's name property from " & my name & " failed"
if (my PrefLib's name) is not "PrefLib" then error "access to PrefLib's name property from " & my name & " failed"
if (my InstallLib's name) is not "InstallLib" then error "access to InstallLib's name property from " & my name & " failed"
if (my test's name) is not "test" then error "access to test's name property from " & my name & " failed"
if (my parent's identifier) is not "ParentObject" then error "access to my parent's identifier property from " & my name & " failed"
end accessProperties
on functionTest()
if my proveFunctionTest then display dialog "functionTest() in test"
return my identifier
end functionTest
end script
tell ParentObject to initialize()
tell test to run
This script works just great, until you try to break out the main parent object into another applet.
2. So why do I need an applet? I need this code to be location independent. That is, I need to be able to simply copy the folder of libraries and scripts to any location on any machine and have it all just work. Because of short comings in the "path to me" command in a compiled script or text script I never really know "where" the script lives, and cannot deduce where the libraries are to in order to load them. I discovered that a script applet can solve this in two ways: First path to me of a script applet actually returns the applet's actual location, and second, I could explicitly "tell application "ScriptRunner" to open alias" and avoid any of the nasty "where's application "ScriptRunner" dialogs. If the script was run from a script editor, it may ask once, but that's it, and allowable, since a human can point out the parent applet once, and will not ask again for a good while. (I wish I knew what exactly flushed this memory so I could avoid it!)
That's why I need the applet. I thought that I could simply use the applet as "controller", if you will, for all my access to library functions.
Duplication of Script Objects
I do have one solution for all this, but it results in duplication of libraries, and all my attempts to use references have failed! There can and must be only ONE copy of each library, as they are interconnected. If library A changes a property in library B, library C must be able to see that along with the script that is running.
Any help with this would be greatly appreciated!
David Weiss
On Oct 27, 2004, at 5:23 PM, David Weiss wrote:
I'm totally confused. Why doesn't this work?
I've got a 3 part AppleScript system:
Exhibit 1 - The Script Runner: An AppleScript Appliction
property XLib : missing value
on runScript(theScript)
set XLib to (load script ("Macintosh HD:Users:davidweiss:Desktop:ALib.scpt" as alias))
run theScript
quit
end runScript
Exhibit 2 - The Script Library: A Compiled Script
on runTest()
say "a Lib"
end runTest
Exhibit 3 - The Script: A text script
script myScript
property parent : application "ScriptRunner"
-- my XLib's runTest() -- fails!
-- runTest() of my XLib -- fails!
tell my XLib to runTest() -- works!
end script
tell application "ScriptRunner" to runScript(myScript)
----
Why do
my XLib's runTest()
runTest() of my XLib
both fail, but
tell my XLib to runTest()
works! This makes no sense to me. Any help would be greatly appreciated!
David Weiss
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden