我想执行一个 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/