简体   繁体   English

使用`runScript`函数运行JXA脚本不允许使用参数

[英]Using the `runScript` function to run a JXA script does not allow parameters

I use JXA to script workflows for Alfred 2 and recently tried to run a script from within another script. 我使用JXA编写Alfred 2的脚本工作流,最近尝试从另一个脚本中运行脚本。 I need to pass some text between the scripts, so I decided to use parameters, but whenever I try to pass a string, a number, an array or anything else that isn't an object to it, it gives the error " Error on line 4: Error: An error occurred. ". 我需要在脚本之间传递一些文本,所以我决定使用参数,但每当我尝试传递一个字符串,一个数字,一个数组或其他不是它的对象时,它会给出错误“ Error on line 4: Error: An error occurred. If I do pass an object, the second script (the one being run by the first script) receives an empty object rather than the one passed to it. 如果我确实传递了一个对象,则第二个脚本(由第一个脚本运行的脚本)接收一个空对象而不是传递给它的对象。 The same happens when the first script is an AppleScript, but if the second script is an AppleScript, it all works perfectly. 当第一个脚本是AppleScript时会发生同样的情况,但如果第二个脚本是AppleScript,那么它们都可以完美运行。 Passing arguments through osascript from the command line also works. 从命令行通过osascript传递参数也有效。 Is the API broken or is there something that I'm doing wrong? API被破坏了还是有些东西我做错了?

First script: 第一个脚本:

var app = Application.currentApplication();
app.includeStandardAdditions = true;

app.runScript(new Path("/path/to/second/script.scpt"), { withParameters: "Hello World!" });

Second script: 第二个脚本:

function run(args) {
    return args;
}

Edit: 编辑:

If the second script is edited as below, the dialogue is displayed but the runScript method of the first script still returns an error. 如果第二个脚本编辑如下,则会显示对话框,但第一个脚本的runScript方法仍会返回错误。

function run(args) {
    var app = Application.currentApplication();
    app.includeStandardAdditions = true;

    app.displayDialog(args.toString());

    return args;
}

Edit 2: 编辑2:

The runScript function actually seems to be working perfectly other than the problem with the parameters. 除了参数问题之外, runScript函数实际上似乎工作得很好。 The error isn't actually being thrown, just displayed by the Script Editor, and execution continues after the call to runScript as if nothing had happened. 实际上并没有抛出错误,只是由脚本编辑器显示,并且在调用runScript之后继续执行,就像没有发生任何事情一样。 The returned value also work perfectly, despite the parameters not working. 尽管参数不起作用,返回的值也可以完美地工作。

A note about Alfred 2 workflows 关于Alfred 2工作流程的说明

To run some code in Alfred 2 (triggered by a search, keyboard command, etc.), it must be typed into a box in the app, not in a file. 要在Alfred 2中运行一些代码(由搜索,键盘命令等触发),必须将其键入应用程序的框中,而不是文件中。 The box to enter code in is very small and does not provide syntax highlighting, and this makes editing code difficult and annoying. 输入代码的框非常小,不提供语法高亮,这使编辑代码变得困难和烦人。 For smaller files, it is okay, but for larger files it is easier to use a short script to run a script file. 对于较小的文件,它是可以的,但对于较大的文件,使用短脚本来运行脚本文件更容易。 I've tried Bash, which would be the simplest option, but Alfred 2 does not provide an option to escape single quotes. 我尝试过Bash,这是最简单的选择,但Alfred 2没有提供逃避单引号的选项。 I also cannot use script libraries (to my knowledge, correct me if I'm wrong), as the code is not in a script bundle and all of the required files need to be within the same folder (for exportation reasons). 我也不能使用脚本库(据我所知,如果我错了就纠正我),因为代码不在脚本包中,并且所有必需的文件都需要在同一个文件夹中(出于出口的原因)。

I don't know how to avoid the runScript error, but I can suggest an alternative approach: load the script as a script library. 我不知道如何避免runScript错误,但我可以建议一种替代方法:将脚本加载为脚本库。

Using a script library 使用脚本库

Turning a script into a library can be as simple as saving the script to ~/Library/Script Libraries . 将脚本转换为库可以像将脚本保存到~/Library/Script Libraries一样简单。 If your script file is named script.scpt and has a run handler, and you save it to the Script Libraries folder, then you can then invoke it from another script like so: 如果您的脚本文件名为script.scpt且有一个运行处理程序,并将其保存到Script Libraries文件夹,那么您可以从另一个脚本调用它,如下所示:

Library("script").run(["Hello, world!"])

Script libraries are documented in the JXA release notes for OS X 10.10 , in the WWDC 2014 session video introducing JXA , and in the AppleScript Language Guide . 脚本库记录在OSX 10.10JXA发行说明, 介绍JXAWWDC 2014会话视频AppleScript语言指南中

Embedding a script library inside of a script bundle 在脚本包中嵌入脚本库

According to the AppleScript Language Guide documentation for script libraries, there is a search policy for finding Script Libraries folders. 根据脚本库的AppleScript语言指南文档,有一个用于查找Script Libraries文件夹的搜索策略。 The first place it searches is: 它搜索的第一个地方是:

If the script that references the library is a bundle, the script's bundle Resources directory. 如果引用该库的脚本是一个bundle,则该脚本的bundle资源目录。 This means that scripts may be packaged and distributed with the libraries they use. 这意味着脚本可以与他们使用的库打包和分发。

To apply this to the example given in the question, you would need to re-save the first script as a script bundle, and then embed the second script inside of the first script. 要将此应用于问题中给出的示例,您需要将第一个脚本重新保存为脚本包,然后将第二个脚本嵌入到第一个脚本中。

For example, if you re-save the first script as script.scptd , then you could save the second script embedded.scpt to script.scptd/Resources/Script Libraries/embedded.scpt . 例如,如果将第一个脚本重新保存为script.scptd ,则可以将第二个脚本embedded.scpt保存到script.scptd/Resources/Script Libraries/embedded.scpt You should then be able to use Library('embedded') to access the script library. 然后,您应该能够使用Library('embedded')来访问脚本库。

To re-save an existing script as a script bundle, you can either use the File > Export... menu item in Script Editor, or you can hold down option while selecting the File menu to reveal the File > Save As... menu item. 要将现有脚本重新保存为脚本包,可以使用“脚本编辑器”中的“ File > Export...菜单项,也可以在选择“ File菜单时按住选项以显示“ File > Save As...菜单项。 The File Format pop-up menu lets you choose the Script bundle format. File Format弹出菜单允许您选择“ Script bundle格式。

Once you have a script bundle open, you can reveal the bundle content panel by using the Show Bundle Contents menu item or toolbar button. 打开脚本包后,可以使用“ Show Bundle Contents菜单项或工具栏按钮Show Bundle Contents包内容面板。 You can then use the gear menu to create the Script Libraries folder inside of the Resources folder, and then you can drag a script into that folder. 然后,您可以使用齿轮菜单在Resources文件夹内创建Script Libraries文件夹,然后将脚本拖动到该文件夹​​中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM