从c++程序执行Applescript

Execute Applescript from c++ program

本文关键字:Applescript 执行 程序 c++      更新时间:2023-10-16

我想执行一个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"system"命令来执行它。这听起来可能像是一个依赖于平台的黑客攻击,事实确实如此!将代码封装到指定的类中应该是一种方法。

顺便问一下:使用AppleScript听一些流程活动是个好方法吗?您可能能够使用内置的unix功能读取进程信息。想想看