Framework to aovid hardcoding of libreferences and so on


(*

This is the appframework I use. The appframework really only concists of checking some paths from my ~/.MacOsX/environment.plist file.
- And a loader to load scripts from the environmentvariable specified in that file..

It will add some 25k to your script without this comment. But it is robust, and it only does so under loading. If the loading time
annoys you You can hardcode the path to the appFrameWork, but then you have basically broken the reason for it.


-- *** Preparation:

I use a variable named APPLESCRIPTOBJECTS for my standard collections of loadable handlers. (scriptObjectPath)
--  the routine loadScriptObject will try to load a script from that path when called with a script-name.
You must make a directory named .MacOsX in your homefolder.
open Property List Edtior and create an entry in a new propertylist file with key: APPLESCRIPTOBJECTS
and the posix path to your libraryscripts as the value. Then save it.

  
-- *** Usage

copy the template including the runhandler for a toplevelscript into a new script.
declare variables to hold your various scriptobjects as properties.


The properties declared in the toplevel objects *must be* initialized with "missing value" that way we avoid
re-initializing when things are running. 

*Warning* this implies that you have to edit the calling script in order
to reload a library script during debugging of the library script in order to reflect the changes.


POLITICS FOR OPTIMUM FLEXIBILITY
I prefer this method to load libraries to any other method, because I then can easily change the contents of the environment-variable
if i choose to move my libraryscripts or persistent scripts in some context to another directory, without having to edit the dependent scripts.
-The only thing I intend to steer clear of is changing the names of the environment variables declared in ~/.MacOsX/environment.plist.


POLITICS FOR OPTIMUM SPEED: -- EnviromentVariables addressed outside of the appFrameWork:

	I intend also to put in hardcoded paths to unix-binaries like sort, in order to make them go as fast as possible.
	This is really of greatest benefit for those who still use 5400rpm disks.
	it is easy to set it up. You just use "which" in a terminal window together with the command name: see man which.
	and then name your variable appropriately. Then you avoid superfluos disk access in order to find your utility.
	then You get out the value of this environmentvariable and hardcode it with the commandname when you do a "do shell script".


	Please see : Technical Q&A QA1067 "Setting environment variables for user processes".
	You can read apple technical note 2065 - tn2065 for more info on shell script
	
	
	
	All in all do I hope that you found this usable, or at least gave you some ideas about how you can avoid that dreaded hardcoding of paths in your scripts.
	Enjoy!
	
	McUsr:D.
*)

-- path properties for the various paths to your various (propriatory)  collections of objects you use in this script ...
property posixPnPersistenObjectsFolder : missing value -- observe: missing value !

-- script object properties 
property HPFSFunctions : missing value
property persistentObject : missing value
-- Your scripts sample run handler ...

on run
	
	my appInit("PATH_TO_PERSISTENT_SCRIPT_OBJECTS_ENV_VAR") -- Container of persistant objects.
	-- lets pretend we have done stuff to this script so that it's state is changed.
	-- we want to save the state, and after having set the properties in the persistent object we:
	store script my persistentObject in a reference to POSIX file (posixPnPersistenObjectsFolder & "/" & "persistentObject.scpt" as Unicode text) replacing yes
	
	appFrameWork's cleanup
end run



-- sample init routine which fires up the appframework
on appInit(sysEnvVar)
	log "initing"
	-- 
	run my appFrameWork
	appFrameWork's initAstid()
	if (my posixPnPersistenObjectsFolder is missing value) then
		set my posixPnPersistenObjectsFolder to my appFrameWork's validateSystemProperty(sysEnvVar)
		
		set my PersistenObjectsFolder to a reference to POSIX file posixPnPersistenObjectsFolder as alias
		-- Her kommer de tileggene jeg ska bruke
	else
		tell my appFrameWork to checkIfPathExists of (my posixPnPersistenObjectsFolder) with posixstyle
	end if
	
	-- EXAMPLE OF loading a script from the standard location.
       --EDITED: I had errantly removed the parameter for the variable.
	set my HPFSFunctions to my appFrameWork's loadScriptObject(HPFSFunctions,"HPFS_files.scpt")
	
	--EXAMPLE OF loading a script from a propriatory location:
	set my persistentObject to load script of alias (POSIX file (posixPnPersistenObjectsFolder & "/" & "persistentObject.scpt" as Unicode text))
	
	
	-- here goes the loading of all the other stuff 
	
	my appFrameWork's cleanup()
	
end appInit




script appFrameWork
	property errorBADPATH : 3100
	property errorEMPTYENVVAR : 3099
	property astid : ""
	property scriptObjectPath : missing value
	--This method must be run from the script in order to set up the env.
	on run
		initAstid()
		if scriptObjectPath is missing value then
			set scriptObjectPath to validateSystemProperty("APPLESCRIPTOBJECTS")
			-- The variable are APPLESCRIPTOBJECTS is defined in ~/.MacOsX/environment.plist 
		else
			checkIfPathExists of scriptObjectPath with posixstyle
			
		end if
	end run
	
	--loadScriptObject: This handler returns an object from our standard script object path (scriptObjectPath)
	
	-- *** EDITED: i edited the "theObject" variable away, which I shouldn't have done in previous post.
		on loadScriptObject(theObject,theObjectFileName)
		local n, e
		if (theObject is missing value) then
			if (scriptObjectPath is missing value) then
				display dialog "You are trying to load a script object without having initialized the app skeleton ..." with title my appName buttons {"Ok"} with icon 2
				error number -128
			end if
			try
				set theObject to load script ((my scriptObjectPath as string) & "/" & theObjectFileName)
			on error e number n
				display dialog "Error encountered under loading of script: Error : & " & e & "NUmber : " & n & "\nTerminates" with title my appName buttons {"Ok"} with icon 2
				error number -128
				-- TODO: her kommer det spørsmål om å velge objekt.
			end try
		end if
		return theObject
	end loadScriptObject

	
	
	-- This handler is used when we want need to retrieve paths wich are stored
	-- in our ~/.MacOsX/environment.plist file.
	-- It validates the contents of an environment variable and returns it if successful.
	on validateSystemProperty(envKey)
		local n
		local errstr
		local userDefinedPath
		set n to 0
		set errstr to ""
		try
			set userDefinedPath to getExistingEnvVar(envKey)
			-- log "sco path = " & userDefinedPath
			checkIfPathExists of userDefinedPath with posixstyle
		on error number n
			-- log "" & n & "  number "
			
			if n = (my errorEMPTYENVVAR) then
				display dialog "The environmentvariable for  " & envKey & " are unintialized ... Aborts " with title my appName buttons {"Ok"} with icon 2
			else if n = (my errorBADPATH) then
				display dialog "The path  for " & envKey & " is wrong (" & userDefinedPath & ") ... Aborts " with title my appName buttons {"Ok"} with icon 2
			else
				display dialog "Unanticipated error Please do tell programmer that error number " & n & " encountered during initialization." with title my appName buttons {"Ok"} with icon 2
				error errstr
			end if
			error number -128
		end try
		return userDefinedPath
	end validateSystemProperty
	
	
	
	-- sets and restores applescripts text item delimeters.
	on initAstid()
		set my astid to (AppleScript's text item delimiters)
	end initAstid
	
	on getAstid()
		return my astid
	end getAstid
	
	on SetASDelim(theDelim)
		set AppleScript's text item delimiters to theDelim
	end SetASDelim
	
	on restoreAstid()
		set AppleScript's text item delimiters to my astid
		-- restores the delimeters to its original state  
	end restoreAstid
	
	
	-- restores applescripts text items delimeters
	on cleanup()
		restoreAstid()
	end cleanup
	
	-- gets a value from an existing environment variable declared in environment.plist.
	on getExistingEnvVar(strEnvVar)
		local retVal
		set retVal to do shell script "/bin/echo $" & strEnvVar
		if retVal = "" then
			error number (my errorEMPTYENVVAR as number)
		end if
		return retVal
	end getExistingEnvVar
	
	
	-- (c) Matt Neuberg. I use this to validate the contents of and environment-variable.
	on checkIfPathExists of s given posixstyle:b
		try
			if b then
				POSIX file s as alias
			else
				s as alias
			end if
		on error e number n
			-- display alert e & n
			error number (my errorBADPATH as number)
		end try
	end checkIfPathExists
end script

the line with do shell script “echo $” & strEnvVar should have been:
"do shell script “/bin/echo $” & strEnvVar in order to enforce my aforementioned “politics for optimum speed”
:wink: McUsr