Subroutine/Library

Hi all
I have the following script (note I’ve left most of it out, but it does work perfectly). What I’d like to be able to do is take out the functions CR_email() and Normal_email and stick them into a separate library - I have several scripts with the same functions in them and it’d be much easier if I could just update them from one script/library. Can someone please tell me

  • what should I stick in the library file?
  • how do I then load the library back into this script?

Any advice greatly appreciated!!

on adding folder items to this_folder after receiving the_files
repeat with this_file in the_files
set thisFileName to name of (info for (this_file))
if first character of thisFileName is equal to “C” then
CR_email(thisFileName)
else
Normal_email(thisFileName)
end if
try
tell application “Microsoft Entourage”
–blah blah blah
end tell
end try
end repeat
end adding folder items to

on CR_email(the_name)
if second word of the_name is “1” then
set default_address to {“user1@domain.com”}
else if second word of the_name is “2” then
set default_address to {“user2@domain.com”}
else if second word of the_name is “3” then
set default_address to {“user3@domain.com”}
else
set default_address to {“user4@domain.com”}
end if
end CR_email

on Normal_email(the_name)
if first word of the_name is “1” then
set default_address to {“user1@domain.com”}
else if first word of the_name is “2” then
set default_address to {“user2@domain.com”}
else if first word of the_name is “3” then
set default_address to {“user3@domain.com”}
else
set default_address to {“user4@domain.com”}
end if
end Normal_email

Basic info, here:
http://macscripter.net/faq/get_the_faq.php?id=170_0_5_0_C
Note that loading the libraries using the “static” method will embed the library in the script itself (eg, in a property) every time the script is compiled.
If you prefer loading the library every time the script is run (just in case there was updates), use instead the “dynamic” method, loading the library at run time.

Thanks for your reply but the link doesn’t really tell me any more than what I knew already - to be more specific:
Say I have this script saved as a library:

on expire_date()
set the_expire_date to “Expired” as string
end expire_date

… and the main script is something like this:

on run
set myLibrary to (load script alias “path:to:my"script:expired.scpt”)
tell myLibrary to expire_date()
end run

When I run it I can see in “Expired” in the result window of Script Editor, but how to I access it via the main script? I want to set the result of the library as a variable but can’t figure out how to do it
Thanks in advance!!

Then, you may be a totally different person from the one of the first question, with the same nickname :wink:

What do you mean with “main script”? Hmmm… Are you asking about variable scope? → and not really about libraries…
To enclose the result of a handler in a variable (wherever is this handler located), you can:

set x to foo() --> same scope handler
set x to foo() of lib --> handler in script object (ie, loaded library)
set x to lib's foo()
tell lib to set x to foo()

I think these are the more frequent syntaxes (use appart of “copy” instead of “set”), if this is what you were asking…

Your library needs to return the data you want, and you need to assign the result to some variable.

In this case:

on expire_date()
	set the_expire_date to "Expired" as string
	return the_expire_date -- this passes the data back to the caller
end expire_date

then your active script says:

on run
	set myLibrary to (load script alias "path:to:my:script:expired.scpt")
	set my_expired to myLibrary's expire_date()
end run

Thanks for your reply - that’s exactly what I came up with in the end and it works perfectly!!