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

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

问题描述

在系统级脚本菜单中运行AppleScript时,使用ScriptMonitor小程序(位于OS X Yosemite中的/System/Library/CoreServices/ScriptMonitor.app中)在菜单栏中显示其进度.这样一来,您就可以随时了解正在运行的脚本,监视进度并轻松地停止运行脚本.

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实用程序运行AppleScript,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. 在Desktop文件夹中将以下代码另存为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;
 }

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

  • 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实用程序运行AppleScripts的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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