从C ++程序执行的AppleScript [英] Execute Applescript from c++ program

查看:314
本文介绍了从C ++程序执行的AppleScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要执行,检查,如果一些进程崩溃一个的AppleScript 命令,然后重新启动应用程序。我已经有剧本,但现在我需要从我的 C ++ code调用它。

该脚本是:

 告诉应用程序活动监视器运行 - 我们需要运行活动监视器
告诉应用程序系统事件告诉进程活动监视器
    告诉窗口1工具栏1 1组单选组1的单选按钮1单击--Using CPU视图
    告诉窗口1的滚动区域1的概要1 - 与工作清单
        集notResponding到第一行静态文本值中包含没有响应 - 寻找未响应过程
        与流
程重复notResponding
            组PID为流
程的文本字段5的价值 - 对于每一个非应诉过程中检索PID
            如果pid是不是然后执行shell脚本(杀-9和安培; PID) - 杀死PID。
        重复结束
    告诉结束
告诉结束


解决方案

您可以拨打osascript,这是执行脚本的苹果一个命令行工具。使用内置C系统命令来执行它。这听起来像是一个平台相关的黑客,以及它是!包裹code到指定的类应该是的方式。

顺便说一句:正在监听使用AppleScript一个很好的方式处理一些活动?你也许可能能够使用内置的UNIX功能,阅读过程的信息。想想吧

I want to execute a Applescript command that checks if some process is crashed and then restarts that app. I've already have the script but now i need to call it from my c++ code.

The script is:

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

解决方案

You can call "osascript" which is a command line tool to execute apple script. Use the built-in C "system" command to execute it. This may sound like a platform dependant hack, well it is! Wrapping the code into a designated class should be the way.

By the way: Is listening for some process activity using AppleScript a good way? You probably might be able to read process information using built-in unix features. Think about it

这篇关于从C ++程序执行的AppleScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆