Sub Libraries

When I got this computer, I knew that the amount of scripts would start to get out of hand. So, I was working with something like this to get sub libraries because I didn’t want to load the whole library. Later, I’ll have to find a way to either load the whole library or just part of it. Something like this example library:


on SayHello()
	say "Hello"
	return
end SayHello

on SayGoodBye()
	say "Goodbye"
	return
end SayGoodBye

on DisplayHello()
	display dialog "Hello"
	return
end DisplayHello

on DisplayGoodbye()
	display dialog "Goodbye"
	return
end DisplayGoodbye

property Sub1 : {SayHello, SayGoodBye}
property Sub2 : {DisplayHello, DisplayGoodbye}
property lib_indices : {Sub1, Sub2}

on run {lib_index}
	return (item lib_index of lib_indices)
end run

The name of this compiled script is TestLib. Here’s the script to get a sub library from the file:


set dp to path to desktop as string
set greetings_lib to (dp & "TestLib.scpt") as alias
-- load the sub library
set sub_lib to run script greetings_lib with parameters {1}
set handler1 to item 1 of sub_lib
set handler2 to item 2 of sub_lib
handler1()
handler2()

Can anyone see any pros and cons with this. This is still a work in progress.

Thanks,

BTW, I’m thinking of writing the library like this, although it might be better the other way. Still deciding.


on SayHello()
	say "Hello"
	return
end SayHello

on SayGoodBye()
	say "Goodbye"
	return
end SayGoodBye

property Sub1 : {SayHello, SayGoodBye}

on DisplayHello()
	display dialog "Hello"
	return
end DisplayHello

on DisplayGoodbye()
	display dialog "Goodbye"
	return
end DisplayGoodbye

property Sub2 : {DisplayHello, DisplayGoodbye}

property lib_indices : {Sub1, Sub2}

on run {lib_index}
	return (item lib_index of lib_indices)
end run

BTW, earlier I was thinking of using script objects this way as sub libs. That works also, but it’s about the same thing.

Hi. I would never recall what those libraries were, if all I had was their index as “handler1” or “handler2”. Condensing the library seems easier to me, but I’m not sure if that suits your purpose. Why don’t you want to load the whole library?

The library:


on Speak(this)
	say this
end Speak

on Display(this)
	display dialog this
end Display

The reader:


(load script ((path to desktop as text) & "TestLib.scpt") as alias)'s Speak("friend, and enter")

Hi Marc,

I used to have a lot of scripts build up until it was unmanageable. The other day, the desktop ended up with about twenty sed scripts. So, I was thinking of combining small libraries into a bigger one. For instance, the sed scripts might go into unixLib along with the others.

Think I need to drop the index thing and just send module names. The library would look them up in some kind of table and send the right ones according to their name, something like python modules. I better do this fast, because already can’t find things. :slight_smile:

I’ve got to take a look at Adam’s article on script libraries.

Condensing the handlers sounds like a good idea now while it’s still fairly easy.

Editted: come to think of it, condensing might not be as good as getting the subroutines themselves. Then I’ll have to deal with script objects.

Thanks a lot,