c++ - 从 C++ 程序执行 Applescript

标签 c++ macos applescript

我想执行一个 Applescript 命令来检查某个进程是否崩溃,然后重新启动该应用程序。我已经有了脚本,但现在我需要从我的 c++ 代码中调用它。

脚本是:

tell application "Activity Monitor" to run  --We need to run Activity Monitor
tell application "System Events" to tell process "Activity Monitor"
    tell radio button 1 of radio group 1 of group 1 of toolbar 1 of window 1 to click --Using the CPU View 
    tell outline 1 of scroll area 1 of window 1 -- working with the list 
        set notResponding to rows whose value of first static text contains "Not Responding" -- Looking for Not responding process
        repeat with aProcess in notResponding
            set pid to value of text field 5 of aProcess  -- For each non responding process retrieve the PID 
            if pid is not "" then do shell script ("kill -9 " & pid) -- KILL the PID. 
        end repeat
    end tell
end tell

最佳答案

您可以调用“osascript”,这是一个命令行工具来执行苹果脚本。使用内置的 C“系统”命令来执行它。这听起来像是依赖于平台的 hack,确实如此!将代码包装到指定的类中应该是这种方式。

顺便说一下:使用 AppleScript 监听某些进程事件是一种好方法吗?您可能能够使用内置的 unix 功能读取进程信息。想想看

关于c++ - 从 C++ 程序执行 Applescript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23386394/

相关文章:

C++ - 模板函数调用运算符重载 - 错误 C2064

c++ - 如何为运算符正确编写 R 值重载

macos - Mac OS X 中的环境变量

applescript - 如何获取应用程序包中的 Scripts 文件夹?

c++ - 获取指针的地址

c++ - Boost::Test——Main() 的生成?

macos - 使用 sips 从一个 png 文件创建一个 icns 文件

macos - 为什么 OS X 10.9 上运行的版本 2 信封的应用程序不被 OS X 10.10 上的网守接受?

macos - OS X Applescript 检查驱动器是否已安装,如果未安装则安装

text - Applescript,将行读取为变量