运行 AppleScripts 通过 ScriptMonitor.app 实用程序
Running AppleScripts through the ScriptMonitor.app Utility
当 AppleScript 运行 通过系统范围的脚本菜单时,它们的进度会使用 ScriptMonitor 小程序(位于 /System/Library/CoreServices/ScriptMonitor.app
,在 OS X 中引入)显示在菜单栏中Yosemite)。这使您可以随时了解 运行ning 脚本、监控进度并轻松停止 运行ning 脚本。
是否可以 运行 从脚本菜单外部通过 ScriptMonitor 使用 AppleScripts,例如从终端或来自其他应用程序的系统调用?
我尝试了以下命令的各种排列,但都没有成功:
/System/Library/CoreServices/ScriptMonitor.app/Contents/MacOS/ScriptMonitor /PATH/TO/SCRIPT
或
open -a /System/Library/CoreServices/ScriptMonitor.app --args /PATH/TO/SCRIPT
之所以有用,是因为有许多辅助应用程序 运行 AppleScripts 响应事件,但往往不太擅长通知用户他们的成功或失败。
因此,事实证明这可以使用 Cocoa 框架中的 NSUserScriptTask 来完成,可以作为已编译命令行应用程序的一部分或通过 AppleScript/Objective-C (ASObjC).
此解决方案允许 AppleScripts、Automator 工作流程和 shell 脚本来自系统 ScriptMonitor.app 实用程序 运行。
ASObjC 解决方案
此处理程序将 运行 在 OS X 10.10 Yosemite 及更高版本上本地运行。它采用单个参数,一个包含脚本 POSIX 样式(斜线分隔)路径的字符串。脚本立即在后台执行,不返回任何结果。
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
.
从命令行调用包装器脚本
已编译Objective-C解决方案
按照这些说明创建将脚本路径作为输入并使用 ScriptMonitor 运行s 脚本的命令行程序。您必须有Xcode命令行工具(或完整的Xcode) 为了编译代码而安装。
在桌面文件夹中将以下代码另存为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;
}
通过运行从终端输入以下命令编译程序:
cd ~/Desktop
gcc -framework Foundation osascriptmonitor.m -o osascriptmonitor
您的桌面上现在将有一个名为 osascriptmonitor
的可执行文件。您可以从终端 运行 该程序,在 ScriptMonitor 中传递您想要 运行 的脚本路径。
示例(将/PATH/TO/SCRIPT
替换为您要运行的脚本路径):
~/Desktop/osascriptmonitor "/PATH/TO/SCRIPT"
如果您随后将可执行文件移动到 /usr/local/bin
,您可以 运行 程序而无需指定其整个路径。
osascriptmonitor "/PATH/TO/SCRIPT"
编辑 2020 年 1 月 3 日:
直接解
幸运的是,我偶然发现了 osascript
的一个未记录的选项,这使得 AppleScripts 基本上不需要以上内容:-P
开关。
用法:osascript -P "/PATH/TO/SCRIPT"
仅当脚本监视器已经 运行ning 时,这将使脚本出现在菜单中 。 Script Monitor 可以提前启动(或在脚本 运行ning 时启动),并会在所有脚本完成后自动退出。
启动脚本监视器的最佳方式是使用 -g 选项:
open -g /System/Library/CoreServices/ScriptMonitor.app
使用此方法,除了让脚本出现在脚本监视器中之外,还可以轻松地将参数传递给脚本。
当 AppleScript 运行 通过系统范围的脚本菜单时,它们的进度会使用 ScriptMonitor 小程序(位于 /System/Library/CoreServices/ScriptMonitor.app
,在 OS X 中引入)显示在菜单栏中Yosemite)。这使您可以随时了解 运行ning 脚本、监控进度并轻松停止 运行ning 脚本。
是否可以 运行 从脚本菜单外部通过 ScriptMonitor 使用 AppleScripts,例如从终端或来自其他应用程序的系统调用?
我尝试了以下命令的各种排列,但都没有成功:
/System/Library/CoreServices/ScriptMonitor.app/Contents/MacOS/ScriptMonitor /PATH/TO/SCRIPT
或
open -a /System/Library/CoreServices/ScriptMonitor.app --args /PATH/TO/SCRIPT
之所以有用,是因为有许多辅助应用程序 运行 AppleScripts 响应事件,但往往不太擅长通知用户他们的成功或失败。
因此,事实证明这可以使用 Cocoa 框架中的 NSUserScriptTask 来完成,可以作为已编译命令行应用程序的一部分或通过 AppleScript/Objective-C (ASObjC).
此解决方案允许 AppleScripts、Automator 工作流程和 shell 脚本来自系统 ScriptMonitor.app 实用程序 运行。
ASObjC 解决方案
此处理程序将 运行 在 OS X 10.10 Yosemite 及更高版本上本地运行。它采用单个参数,一个包含脚本 POSIX 样式(斜线分隔)路径的字符串。脚本立即在后台执行,不返回任何结果。
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
.
已编译Objective-C解决方案
按照这些说明创建将脚本路径作为输入并使用 ScriptMonitor 运行s 脚本的命令行程序。您必须有Xcode命令行工具(或完整的Xcode) 为了编译代码而安装。
在桌面文件夹中将以下代码另存为
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; }
通过运行从终端输入以下命令编译程序:
cd ~/Desktop gcc -framework Foundation osascriptmonitor.m -o osascriptmonitor
您的桌面上现在将有一个名为
osascriptmonitor
的可执行文件。您可以从终端 运行 该程序,在 ScriptMonitor 中传递您想要 运行 的脚本路径。
示例(将/PATH/TO/SCRIPT
替换为您要运行的脚本路径):
~/Desktop/osascriptmonitor "/PATH/TO/SCRIPT"
如果您随后将可执行文件移动到 /usr/local/bin
,您可以 运行 程序而无需指定其整个路径。
osascriptmonitor "/PATH/TO/SCRIPT"
编辑 2020 年 1 月 3 日:
直接解
幸运的是,我偶然发现了 osascript
的一个未记录的选项,这使得 AppleScripts 基本上不需要以上内容:-P
开关。
用法:osascript -P "/PATH/TO/SCRIPT"
仅当脚本监视器已经 运行ning 时,这将使脚本出现在菜单中 。 Script Monitor 可以提前启动(或在脚本 运行ning 时启动),并会在所有脚本完成后自动退出。
启动脚本监视器的最佳方式是使用 -g 选项:
open -g /System/Library/CoreServices/ScriptMonitor.app
使用此方法,除了让脚本出现在脚本监视器中之外,还可以轻松地将参数传递给脚本。