从c ++程序执行Applescript

时间:2014-04-30 11:04:11

标签: 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

1 个答案:

答案 0 :(得分:2)

你可以打电话给" osascript"这是一个执行apple脚本的命令行工具。使用内置的C"系统"命令执行它。这可能听起来像一个平台依赖的黑客,它是!将代码包装到指定的类中应该是这样的。

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