The handler abstracts away the details of how the environment is constructed from the rest of the AppleScript. We can thus just focus on the internals of the handler, without worrying about how the rest of the AppleScript will be affected. Put another way, we'll replace the string defining the environment by a function call that returns the string.
The approach I'll take will be to store the environment settings in a property list file, read them into lists representing variables and values, and format the list contents appropriately. This is pretty easy, thanks to the property list suite in System Events. I'll encapsulate reading the environment into a handler of its own:
plistparameter is the path to the property list file containing our environment.
prependEnvironmenthandler is pretty straightforward. We just define the path to the environment file and let
readEnvironmentdo the work. All that remains is to decide where to store the environment settings. The Preferences folder seems like a natural choice, so let's use a file called
de.codingmonkeys.SubEthaEdit.LaTeX_environment.plist, which is similar to how the LaTeX mode settings are treated in TextMate. The handler becomes:
To be clear, it is not necessary to have the plist file present at all, since we defined our shell script to use default values when no environment variables are set. We'll handle the case of an absent environment file below.
We now need to provide
environmentStringhandlers. The former draws on the property list suite of System Events, returning two lists of equal length. The first list contains the environment variable names, while the second contains the corresponding values. The handler is complicated a bit by checking whether the plist exists, but has a single
getat its core:
To format the list contents into an appropriate string, we define the
This is a straightforward, ugly function that just iterates through the lists, formatting the contents. Each variable-value pair is turned into a shell-style export statement and accumulated in a list. The accumulated export statements are then joined together into one big string that defines the environment.
To test all of this out, it is easier to just call the base
prependEnvironmenthandler, instead of working through a SEE mode. I used
prependEnvironment onto "buildScript", and ran it with the environment plist both present and absent. It works as expected, so could be simply dropped into the LaTeX mode bundle. However, I prefer to make another, relatively minor change that requires an extra parameter for the
prependEnvironmenthandler. Specifically, I will pass in the active mode from SubEthaEdit:
Later extensions could make use of more properties of the mode, so I passed in the entire mode, instead of just the name. The advantage of passing the mode as a parameter is that all of the stuff I've shown in this post can be used without change for other scripts and for other modes. It will only be necessary to change the line defining the string holding the shell script and to make appropriate redefinitions of the
Next time, I'll take a look at how to easily access and make changes to the environment.
For completeness, here's the entire AppleScript: