Libraries of Subroutines

I’m trying to include a library of subroutines in each of the different scripts for my own ease of programming in my application.

property SRlib : load script file (POSIX file (path for script "SRlib"))

This is what I have, but this doesn’t return a value since the application is not built yet, and the script won’t compile without that value. Is there a way to do this without having to put a fixed path in the property?

Thanks.

I think you should define then at run-time. Eg:

property aLib : missing value
property bLib : missing value

on awake from nib --> attached to the app-owner
	loadLibs()
	--> etc
end awake

to loadLibs()
	set aLib to load script file (POSIX file (path for script "aLib"))
	set bLib to load script file (POSIX file (path for script "bLib"))
end

Since you’re binding the library at compile-time, there’s nothing wrong with hardwiring a fixed path here; it’s not used at runtime so won’t affect portability. You don’t need to include the original library file in the application bundle either. The only time you’ll need to change the hardwired path is if you rebuild the project after moving the library to a different location, which isn’t a big deal.

Thanks for the help, but how do I get the script to compile with my library code the way it is? I think it’s looking inside of Xcode itself for the script file…

Has’ explanation is rigth, so your code would be:

property SRlib : load script alias "path:to:SRlib.scpt"

Essentially, I use hhas’s approach (its a bit of a hybrid approach). In my main script:

global FunctionScript
global allFunctions

on will finish launching theObject
	loadScripts() -- you could put this where you needed it
end will finish launching

on loadScripts()
	set parent_path to the POSIX path of (path to me as string)
	set scriptPath to missing value
	set myBundle to call method "bundleWithPath:" of class "NSBundle" with parameter parent_path
	try
		tell myBundle
			set scriptPath to path for script "Functions" extension "scpt"
		end tell
		set FunctionScript to load script POSIX file (scriptPath)
		set allFunctions to allFunctions() of FunctionScript
	on error
		log "Problem getting Functions script."
	end try
end loadScripts 

and in my separate Functions.scpt file (which when I created it was the “Text file.applescript” kind but gets compiles as a .scpt - and is part of the same application, sitting in the “foo.app/Contents/Resources/Scripts” folder):

on allFunctions()
	script Functions

		on remove_list_from_list(a_working_list, the_position)
			set new_working_list to {}
			repeat with i from 1 to count of a_working_list
				set this_item to item i of a_working_list as list 
				if (i is not equal to the_position) then copy this_item to the end of new_working_list
			end repeat
			return new_working_list
		end remove_list_from_list
		
	end script
end allFunctions  

and to use something in Functions.scpt in my main script:

tell allFunctions to set new_original_list to remove_list_from_list(original_tags_for_files, position_in_list)

Thanks for that script anaxamander, it will really help me organise my project more logically. I’ve modified it slightly so that it’s possible to load multiple scripts. The main function:

on loadScript(scriptName)
	set parent_path to the POSIX path of (path to me as string)
	set scriptPath to missing value
	set myBundle to call method "bundleWithPath:" of class "NSBundle" with parameter parent_path
	try
		tell myBundle
			set scriptPath to path for script scriptName extension "scpt"
		end tell
		set FunctionScript to load script POSIX file (scriptPath)
		set allFunctions to allFunctions() of FunctionScript
	on error
		log "Problem getting script: " & scriptName
	end try
	
	return allFunctions
end loadScript

Then in the .applescript file “Library” I have:

on allFunctions()
	script Library
		
		on test()
			return "Library"
			--return contents of text field "project_path" of box "project" of tab view item "flash" of tab view "view" of window "main" as Unicode text
		end test
		
	end script
end allFunctions

And similarly in the “Preferences” file I’ve got:

on allFunctions()
	script Prefs
		
		on test()
			return "Preferences"
		end test
		
	end script
end allFunctions

And then I’m loading the scripts with:

global libScript
global prefScript

on will open theObject
	
	set libScript to loadScript("Library")
	tell libScript to set testLib to test()
	display alert testLib
	
	set prefScript to loadScript("Preferences")
	tell prefScript to set testPref to test()
	display alert testPref

	return true
end will open

Thanks to everyone here for all the information, it’s been invaluable while learning applescript.

Cheers,

Mischa

It would likely be more illustrative if one of the above posters would share an xcode project with us along with a sample scpt file and/or library. Thanks in advance.