Sam*_*ami 12 macos cocoa macos-carbon objective-c
我正在尝试从我的应用程序控制外部OSX应用程序的窗口.我想1.移动屏幕上的窗口2.调整屏幕上的窗口大小3.更改应用程序的当前活动窗口4.获取当前活动的窗口.
(我想通过ObjC/C/C++ apis这样做).
考虑到我有想要控制的窗口的CGWindowID,我应该寻找什么样的API调用?也就是说,我希望找到的功能与类似的东西签名:MoveWindow(CGWindowID winId, int x, int y)
,ResizeWindow(CGWindowID winId, int width, int height)
,Activatewindow(CGWindowID winId)
,CGWindowID GetCurrentlyActivatedWindow()
.
对于3,我已经习惯SetFrontProcess
将一个进程拉到前面,但是这不允许我选择一个进程的特定窗口,如果它有多个.
Joh*_*her 11
实现此目的的一种方法确实是使用辅助功能API.
我正在开发的应用程序就是这样做以获得前窗,前窗的文档路径和许多其他属性.
我这样做的方式是通过AppleScript.它有时可能很笨拙,但似乎相当可靠.我使用AppScript从我的Cocoa应用程序中发送AppleScript.它的线程安全且比其他选择更稳定 - 无论是Scripting Bridge还是NSAppleScript.
困难的一点是在AppleScript中使用它的窗口ID识别窗口 - AppleScript似乎没有与CGWindowID匹配的窗口ID属性.但是,您可以使用AppleScript获得所需的任何窗口.
将最前面的窗口移动到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
Run Code Online (Sandbox Code Playgroud)将最前面的窗口调整为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
Run Code Online (Sandbox Code Playgroud)更改最前面的应用程序的当前窗口
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
Run Code Online (Sandbox Code Playgroud)活跃的窗口
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
Run Code Online (Sandbox Code Playgroud)有一个名为ASTranslate的AppScript应用程序可以将AppleScript转换为在AppScript中调用相关命令的Objective C代码.
有关如何获取窗口大小和边界的更多信息(据我所知,这些只读),请参阅Son of Grab示例应用程序.
归档时间: |
|
查看次数: |
4679 次 |
最近记录: |