控制OSX窗口 [英] Controlling OSX windows

查看:441
本文介绍了控制OSX窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从我的应用程序控制外国OSX应用程序的窗口。我想要

1.移动屏幕上的窗口
2.调整屏幕上的窗口大小
3.更改应用程序的当前活动窗口
4.获取当前活动的窗口。



(我想通过ObjC / C / C ++ apis来实现)。



我应该寻找什么API调用,考虑到我有想要控制的窗口的CGWindowID?也就是说,我希望找到具有类似签名的函数: MoveWindow(CGWindowID winId,int x,int y) ResizeWindow(CGWindowID winId,int width,int height) Activatewindow(CGWindowID winId) CGWindowID GetCurrentlyActivatedWindow()



对于3,我已经使用 SetFrontProcess 将一个进程拉到前面,这不允许我选择一个过程的具体窗口,如果它有多个。

解决方案

以使用辅助API。



我正在开发的应用程序只是为了获取前窗口,前窗口的文档路径和许多其他属性。 / p>

我这样做的方式是通过AppleScript。它有时可能是笨拙,但它似乎是相当可靠的。我使用 AppScript 在我的Cocoa应用程序内发送AppleScript。



困难的一点是使用AppleScript中的窗口ID来标识窗口 - AppleScript不支持似乎有一个窗口ID属性匹配CGWindowID。


  1. 将最前面的窗口移动到100,100



    告诉应用程序系统事件
    将进程设置为最前面为真的第一个进程
    将窗口设置为属性 AXFocusedWindow的过程
    将窗口的位置设置为{100,100}
    end tell


  2. 将最前面的窗口调整为200,300

     告诉应用程序系统事件
    将进程设置为最前面为真的第一个进程
    将窗口设置为进程的属性AXFocusedWindow的值
    将窗口的大小设置为{200,300}
    end tell


  3. 更改最前面应用程序的当前窗口

     告诉应用程序系统事件
    将进程设置为最前面为true的第一个进程
    将窗口设置为进程的属性AXFocusedWindow的值
    设置窗口的大小到{200,300}
    进程的窗口
    - 获取要激活的窗口的ID的代码
    告诉进程的窗口2 - 假设它是窗口2
    执行操作AXRaise
    end tell
    end tell


  4.  告诉应用程序系统事件
    将进程设置为最前面为真的第一个进程
    设置窗口到过程的属性AXFocusedWindow的值
    end tell


有一个可用于AppScript的应用程序名为 ASTranslate 将此AppleScript转换为调用AppScript中相关命令的Objective C代码。



有关如何获取窗口大小和边界的更多信息(这些只读为就我所知),请参阅>子之父示例应用程序。


I'm trying to control windows of a foreign OSX applications from my application. I'd like to 1. move the windows on the screen 2. resize the windows on the screen 3. change the currently active window of the application 4. get the currently active window.

(And I'd like to do this either through ObjC/C/C++ apis).

What are the API calls that I should be looking for, considering that I have the CGWindowIDs of the windows that I want to control? That is, I'd expect to find functions with signatures of something like: MoveWindow(CGWindowID winId, int x, int y), ResizeWindow(CGWindowID winId, int width, int height), Activatewindow(CGWindowID winId), CGWindowID GetCurrentlyActivatedWindow().

For 3, I'm already using SetFrontProcess to pull a process to be up front, but this doesn't let me select the specific window of a process if it has multiple.

解决方案

One way of doing this is indeed to use the accessibility APIs.

An application I'm developing is doing just this to get the front window, the document path of the front window and many other attributes.

The way I'm doing this is through AppleScript. It can be clumsy at times, but it seems to be fairly reliable. I use AppScript to send AppleScript from within my Cocoa app. It's thread safe and more stable than the alternatives - either Scripting Bridge or NSAppleScript.

The difficult bit will be identifying a window using it's window ID in AppleScript - AppleScript doesn't seem to have a window ID property that matches up to CGWindowID. However, you can get any window you want using AppleScript.

  1. Move frontmost window to 100, 100

    tell application "System Events"
     set theprocess to the first process whose frontmost is true
     set thewindow to the value of attribute "AXFocusedWindow" of theprocess
        set position of thewindow to {100, 100}
    end tell
    

  2. Resize frontmost window to 200, 300

    tell application "System Events"
      set theprocess to the first process whose frontmost is true
      set thewindow to the value of attribute "AXFocusedWindow" of theprocess
         set size of thewindow to {200, 300}
    end tell
    

  3. Change current window of the frontmost application

    tell application "System Events"
      set theprocess to the first process whose frontmost is true
      set thewindow to the value of attribute "AXFocusedWindow" of theprocess
         set size of thewindow to {200, 300}
         windows of theprocess
         -- Code to get ID of window you want to activate
         tell window 2 of theprocess -- assuming it's window 2
               perform action "AXRaise"
         end tell
    end tell
    

  4. Window that's active

    tell application "System Events"
     set theprocess to the first process whose frontmost is true
     set thewindow to the value of attribute "AXFocusedWindow" of theprocess
    end tell
    

There's an application available for AppScript called ASTranslate that will turn this AppleScript into the Objective C code that calls the relevant commands in AppScript.

For more information on how to get the size and bounds of windows (these are read only as far as I'm aware) see the Son of Grab sample application.

这篇关于控制OSX窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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