为什么 OSA_LIBRARY_PATH 不能像为 JXA 记录的那样工作?

Why won't OSA_LIBRARY_PATH not work as documented for JXA?

根据 Apple's Developer DocsLibrary 全局允许导入已编译的脚本,因此它们可以用作当前脚本中的库。如果您使用位于 ~/Library/Script Libraries:

myLibName.scpt 执行类似以下代码的操作,此方法就可以正常工作
myLib = Library('myLibName');
myLib.myLibMethod() // Works just fine

但是,文档还声称可以导出一个环境变量——OSA_LIBRARY_PATH包含一串:分隔的路径——和Library() 然后会在 之前遵循该路径列表,然后 继续其默认路径:~/Library/Script Libraries。你知道,就像 bash 环境变量 Path。这是下面的相关文档;它描述了路径层次结构:

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.

问题是它不起作用。我已经尝试导出 OSA_LIBRARY_PATH 变量——通过我的 .zshrc 文件全局导出——然后 运行 一个示例脚本,就像上面的脚本一样,通过脚本编辑器和 osascript可执行。什么都不管用;我收到 "file not found" 错误。我在网上找到了这个 thread-where-the-participants-give-up-hope;它并没有解释太多。有什么想法吗?

在某种程度上相关的注释中,Scripting Additions 套件提供了另外两种方法 — loadScriptstoreScript — 看起来它们可能在这里很有用.不幸的是,当您尝试使用它们时,osascript 给了您手指。不过,我确实设法 return 使用 loadScript 从编译脚本中得到了一个看起来像十六进制缓冲区的东西。不管怎样,如果你们能就此发表任何见解,我们将不胜感激。谢谢。

当 运行 系统完整性保护启用时,OSA_LIBRARY_PATH 环境变量被受限的可执行文件忽略。

要解决此限制,您可以 turn off SIP,也可以使用不受限制的可执行文件。

例如,要使 osascript 不受限制,您应该先复制一份,然后使用临时签名重新签名:

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

一旦你拥有不受限制的 osascript,你可以 运行 它与 OSA_LIBRARY_PATH 环境变量设置如下:

OSA_LIBRARY_PATH="/path/to/libs" ./osascript path/to/script.scpt

作为一个糟糕的选择,您可以在 osascript 将查看的 "Script Libraries" 文件夹之一中放置一个符号链接,并将其指向您想要的文件夹。请注意,符号链接必须是整个文件夹的替代品,它不能只存在于其中。

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

10.13.2 测试