通过 ScriptMonitor.app 实用程序运行 AppleScript [英] Running AppleScripts through the ScriptMonitor.app Utility

查看:17
本文介绍了通过 ScriptMonitor.app 实用程序运行 AppleScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 AppleScript 通过系统范围的脚本菜单运行时,它们的进度会使用 ScriptMonitor 小程序(位于 /System/Library/CoreServices/ScriptMonitor.app,在OS X 优胜美地).这使您可以随时了解正在运行的脚本、监控进度并轻松停止运行脚本.

When AppleScripts are run through the system-wide Script Menu, their progress is displayed in the menu bar using the ScriptMonitor applet (located in /System/Library/CoreServices/ScriptMonitor.app, introduced in OS X Yosemite). This allows you to stay aware of running scripts, monitor progress, and easily stop running scripts.

是否可以通过 ScriptMonitor 从脚本菜单外部运行 AppleScript,例如从终端或从其他应用程序的系统调用?

Is it possible to run AppleScripts through the ScriptMonitor from outside the Script Menu, for example from Terminal or from system calls from other applications?

我尝试了以下命令的各种排列,都没有成功:

I have tried various permutations of the following commands, all without success:

/System/Library/CoreServices/ScriptMonitor.app/Contents/MacOS/ScriptMonitor /PATH/TO/SCRIPT

open -a /System/Library/CoreServices/ScriptMonitor.app --args /PATH/TO/SCRIPT

这很有用的原因是有许多帮助应用程序运行 AppleScripts 以响应事件,但往往不太擅长通知用户他们的成功或失败.

The reason this would be useful is that there are many helper applications that run AppleScripts in response to events, but tend not to be very good at notifying the user about their success or failure.

推荐答案

所以,事实证明这可以使用 Cocoa 框架中的 NSUserScriptTask 来完成,或者作为编译的命令行应用程序的一部分或通过 AppleScript/Objective-C (ASObjC).

So, it turns out this can be done using NSUserScriptTask from the Cocoa frameworks, either as part of a compiled command-line application or through AppleScript/Objective-C (ASObjC).

此解决方案允许从 System ScriptMonitor.app 实用程序运行 AppleScripts、Automator 工作流程和 shell 脚本.

This solution allows AppleScripts, Automator workflows, and shell scripts to be run from the System ScriptMonitor.app utility.

此处理程序将在 OS X 10.10 Yosemite 及更高版本上本机运行.它接受一个参数,一个包含 POSIX 样式(斜线分隔)的脚本路径的字符串.脚本在后台立即执行,不返回任何结果.

This handler will run natively on OS X 10.10 Yosemite and later. It takes a single parameter, a string containing the POSIX-style (slash-delimited) path to the script. The script is executed immediately in the background, and no result is returned.

use framework "Foundation"

to runInScriptMonitor(script_path)

    set {script_task, url_error} to current application's NSUserScriptTask's alloc()'s initWithURL:(script_path as POSIX file) |error|:(reference)
    if url_error is not missing value then error (url_error's localizedDescription as string) number (url_error's code as integer)
    script_task's executeWithCompletionHandler:(missing value)

    # The following delay was increased due to a system hang on Mojave after installation of Security Update 2020-004 (previously, the delay was 0.05).
    delay 10 -- Necessary to allow NSUserScriptTask to be dispatched before execution terminates.

    return

end runInScriptMonitor

调用如下:runInScriptMonitor("/PATH/TO/FILE")

这允许您从 AppleScript 中运行 ScriptMonitor 中的脚本.如果调用放置在包装器 AppleScript 中,则可以使用 osascript 从命令行调用包装器脚本.

This allows you to run scripts in ScriptMonitor from within AppleScript. If the call is placed in a wrapper AppleScript, the wrapper script can then be called from the command line using osascript.

按照这些说明创建一个命令行程序,该程序将脚本路径作为输入并使用 ScriptMonitor 运行脚本.您必须安装 Xcode 命令行工具(或完整的 Xcode)以编译代码.

Follow these instructions to create a command-line program that takes the script path as input and runs the script using ScriptMonitor. You must have the Xcode Command Line Tools (or the full Xcode) installed in order to compile the code.

  1. 在桌面文件夹中将以下代码保存为 osascriptmonitor.m:

 #import <Foundation/Foundation.h>

 int main(int argc, const char *argv[]) {
     if (argc < 2) {
         printf("usage: osascriptmonitor /path/to/script\n");

     } else {
         NSString *script_path = [NSString stringWithUTF8String:argv[1]];
         NSUserScriptTask *script_task = [[NSUserScriptTask alloc] initWithURL:[NSURL fileURLWithPath:script_path] error:nil];
         [script_task executeWithCompletionHandler:nil];
         [NSThread sleepForTimeInterval:60.0f];
     }

     return 0;
 }

  • 通过从终端运行以下命令来编译程序:

  • Compile the program by running the following commands from Terminal:

     cd ~/Desktop
     gcc -framework Foundation osascriptmonitor.m -o osascriptmonitor
    

  • 现在您的桌​​面上将有一个名为 osascriptmonitor 的可执行文件.您可以从终端运行该程序,传递要在 ScriptMonitor 中运行的脚本的路径.

  • You will now have an executable file named osascriptmonitor on your Desktop. You can run that program from Terminal, passing the path of the script that you want to run in ScriptMonitor.

    示例(将/PATH/TO/SCRIPT 替换为您要运行的脚本的路径):

    Example (replace /PATH/TO/SCRIPT with the path of the script you want to run):

        ~/Desktop/osascriptmonitor "/PATH/TO/SCRIPT"
    

    如果随后将可执行文件移动到 /usr/local/bin,则无需指定其整个路径即可运行该程序.

    If you then move the executable file to /usr/local/bin, you can run the program without having to specify its entire path.

        osascriptmonitor "/PATH/TO/SCRIPT"
    


    编辑 2020 年 1 月 3 日:

    一个愉快的意外,我偶然发现了 osascript 的一个未记录选项,这使得 AppleScripts 基本上不需要上述选项:-P 开关.

    By happy accident, I stumbled across an undocumented option for osascript that makes the above largely unnecessary for AppleScripts: the -P switch.

    用法:osascript -P/PATH/TO/SCRIPT"

    就其本身而言,这将使脚本出现在菜单中仅当脚本监视器已在运行时.脚本监视器可以提前(或在脚本运行时)启动,并在所有脚本完成后自动退出.

    On its own, this will make the script appear in the menu only if Script Monitor is already running. Script Monitor can be launched ahead of time (or while the script is running) and will quit automatically once all scripts have finished.

    启动脚本监视器的最佳方式是使用 -g 选项:

    The best way to launch Script Monitor is using the -g option:

    open -g /System/Library/CoreServices/ScriptMonitor.app
    

    使用此方法,除了让脚本出现在脚本监视器中之外,还可以轻松地将参数传递给脚本.

    Using this method, it is possible to easily pass arguments to the script in addition to having it appear in Script Monitor.

    这篇关于通过 ScriptMonitor.app 实用程序运行 AppleScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆