4

According to Apple's Developer Docs the Library global allows one to import compiled scripts so they can be used as a library in one's current script. This works just fine if you were to do something like the below code with myLibName.scpt located at ~/Library/Script Libraries:

myLib = Library('myLibName');
myLib.myLibMethod() // Works just fine

But, the docs also claim that one can export an environment variable — OSA_LIBRARY_PATH containing a string of : delimited paths — and Library() would then defer to that list of paths before proceeding to it's default path: ~/Library/Script Libraries. Ya know, like the bash environment variable Path. Here's the relevant piece of documentation below; it describes the path hierarchy:

The basic requirement for a script to be a script library is its location: it must be a script document in a “Script Libraries” folder in one of the following folders. When searching for a library, the locations are searched in the order listed, and the first matching script is used:

  1. If the script that references the library is a bundle, the script’s bundle Resources directory. This means that scripts may be packaged and distributed with the libraries they use.
  2. If the application running the script is a bundle, the application’s bundle Resources directory. This means that script applications (“applets” and “droplets”) may be packaged and distributed with the libraries they use. It also enables applications that run scripts to provide libraries for use by those scripts.
  3. Any folders specified in the environment variable OSA_LIBRARY_PATH. This allows using a library without installing it in one of the usual locations. The value of this variable is a colon-separated list of paths, such as /opt/local/Script Libraries:/usr/local/Script Libraries. Unlike the other library locations, paths specified in OSA_LIBRARY_PATH are used exactly as-is, without appending “Script Libraries”. Supported in OS X v10.11 and later.
  4. The Library folder in the user’s home directory, ~/Library. This is the location to install libraries for use by a single user, and is the recommended location during library development.
  5. The computer Library folder, /Library. Libraries located here are available to all users of the computer.
  6. The network Library folder, /Network/Library. Libraries located here are available to multiple computers on a network.
  7. The system Library folder, /System/Library. These are libraries provided by OS X.
  8. Any installed application bundle, in the application’s bundle Library directory. This allows distributing libraries that are associated with an application, or creating applications that exist solely to distribute libraries. Supported in OS X v10.11 and later.

The problem is that it doesn't work. I've tried exporting the OSA_LIBRARY_PATH variable — globally via my .zshrc file — and then running a sample script just like the one above via both the Script Editor and the osascript executable. Nothing works; I get a "file not found" error. I found this thread-where-the-participants-give-up-hope online; it doesn't explain much. Any thoughts?

On a somewhat related note, the Scripting Additions suite provides two other methods — loadScript and storeScript — that seem like they might be useful here. Unfortunately, when you try to use them, osascript gives you the finger. Though, I did manage to return what looked like a hexadecimal buffer from a compiled script using loadScript. Anyway, any insight you guys can shed on this would be much appreciated. Thanks.

Mikis
  • 80
  • 2
  • 7
  • The wisdom of setting *shell* variables for something that's designed to run within *GUI* applications is beyond the ken of mortals, as is the logic of putting JXA libraries in the same folders as AS libraries even though they're 100% incompatible. The `load script` command provides a basic mechanism for loading `.scpt` files from any location, but does have the curious expectation that the OSA language component it's serving *isn't* a total sack of knackers. Which JXA is. Honestly, if you *must* use JXA, I suggest you try to find a way to use Node modules instead as JXA is crap at this stuff. – foo Feb 14 '16 at 16:13
  • 1
    Any node module attempting to speak to the Apple Event Manager will have to go through osascript anyway. I've missed the point of your comment; you haven't answered the question or added any new information. Maybe try and be more helpful? Or don't. – Mikis Feb 15 '16 at 05:11
  • There aren't any good answers to give. JXA is crap—live with it, or stick to AS. If you want to load general-purpose JS modules, see if you can do it using node's module system. 1. It's better than JXA's module loader. You'll get access to a ton of preexisting JS modules, whereas there's squat for JXA. Yeah, if you want to put AE/Cocoa code in modules, you'll have to use .scpt files for that, in which case either use the existing Library folders (assuming you don't have identically named AS modules in there, or they'll conflict), or save your script as a .scptd and put the modules inside that. – foo Feb 15 '16 at 11:38

2 Answers2

9

The OSA_LIBRARY_PATH environment variable is ignored by restricted executables when running with System Integrity Protection enabled.

To workaround this limitation you can either turn off SIP, or you can use an unrestricted executable.

For instance, to make osascript unrestricted, you should first make a copy, and then re-sign it with an ad-hoc signature:

cp /usr/bin/osascript ./osascript
codesign -f -s - ./osascript

Once you have the unrestricted osascript, you can run it with the OSA_LIBRARY_PATH environment variable set like this:

OSA_LIBRARY_PATH="/path/to/libs" ./osascript path/to/script.scpt
Community
  • 1
  • 1
bacongravy
  • 893
  • 8
  • 13
2

As a lousy alternative, you can put a symlink at one of the "Script Libraries" folders that osascript would look at and point it to the folder you want. Note that the symlink must be a replacement for the entire folder, it can't just exist inside of it.

rm -rf ~/Library/Script\ Libraries
ln -s "/Your/Custom/Path/Goes/Here/" ~/Library/Script\ Libraries

Tested on 10.13.2

Aaron
  • 3,209
  • 1
  • 12
  • 14