objective-c – 使用Accessibility API在Mac OS X上移动其他窗口

前端之家收集整理的这篇文章主要介绍了objective-c – 使用Accessibility API在Mac OS X上移动其他窗口前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用Accessibility API来更改其他应用程序的窗口.我想要做的是从屏幕上获取所有窗口的所有应用程序,然后将它们全部移动给定的偏移量(可以说5或10或任何值).我在这方面遇到困难,因为这是Objective-C为我编程的第一天.

这是我现在在做什么首先,我使用CGWindowListCopyWindowInfo找到窗口及其PID的列表.然后,对于每个窗口,我使用AXUIElementCreateApplication获取窗口的AXUIElementRef.之后,我应该使用AXUIElementCopyAttributeValue与属性kAXPositionAttribute(我没有得到正确的位置,总是得到零).最后,我应该将所需的偏移量添加到位置,并使用属性kAXPositionAttribute和新的位置点(即使我设置了绝对值,如0,0),我得到运行时错误的AXUIElementSetAttributeValue).

有人可以用我上面描述的代码片段来帮助我,因为我尝试了许多事情,没有任何运气.此外,它不应该像我决定在上面实现的那样完全正确.如果有更好的方法去做,那么我会很乐意改变它.

更新:
根据评论的要求,以下是一个尝试的代码段:

  1. // Get all the windows
  2. CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly,kCGNullWindowID);
  3. NSArray* arr = CFBridgingRelease(windowList);
  4. // Loop through the windows
  5. for (NSMutableDictionary* entry in arr)
  6. {
  7. // Get window PID
  8. pid_t pid = [[entry objectForKey:(id)kCGWindowOwnerPID] intValue];
  9. // Get AXUIElement using PID
  10. AXUIElementRef elementRef = AXUIElementCreateApplication(pid);
  11. CFTypeRef position;
  12. CGPoint point;
  13. // Get the position attribute of the window (maybe something is wrong?)
  14. AXUIElementCopyAttributeValue(elementRef,kAXPositionAttribute,(CFTypeRef *)&position);
  15. AXValueGetValue(position,kAXValueCGPointType,&point);
  16. // Debugging (always zeros?)
  17. NSLog(@"point=%@",point);
  18. // Create a point
  19. NSPoint newPoint;
  20. newPoint.x = 0;
  21. newPoint.y = 0;
  22. position = (CFTypeRef)(AXValueCreate(kAXValueCGPointType,(const void *)&newPoint));
  23. // Set the position attribute of the window (runtime error over here)
  24. AXUIElementSetAttributeValue(elementRef,(CFTypeRef *)&position);
  25. }

解决方法

根据您的示例代码(稍微修改,因为您发布的内容不会编译,并且将不会修改),我做了一些实验.

这里有几个注意事项:

>您正在通过PID检索应用程序,然后作为窗口执行.这是您问题的核心,但它只是解决方案的开始.
>您将需要走出可访问性应用程序对象的窗口列表,以便找到可以使用辅助功能框架移动的可重定位窗口.
> CGWindowListCopyWindowInfo将在询问您的方式时返回“全屏幕”窗口,但不能保证这些窗口是“用户窗口”或具有可访问性的窗口.大多数菜单栏项目都有一个根窗口,它是“屏幕上的”,大多数菜单栏是不可访问的(当您尝试走出您检索的PID的辅助功能树时显示).
>您可能会发现AXRole的测试是有帮助的,或者您可能会发现其他窗口辅助功能属性在确定是否移动窗口方面更有用.

我已经在这里修改了你的代码(这将运行而不会崩溃),这将从您通过PID检索的应用程序获取相关的窗口信息,然后移动窗口.我有一个睡眠声明,所以我可以停止执行,因为我只是测试运动的效果

  1. #import <Foundation/Foundation.h>
  2. #import <CoreFoundation/CoreFoundation.h>
  3. #import <ApplicationServices/ApplicationServices.h>
  4.  
  5. int main(int argc,char *argv[]) {
  6. @autoreleasepool {
  7. // Get all the windows
  8. CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly,kCGNullWindowID);
  9. NSArray* arr = CFBridgingRelease(windowList);
  10. // Loop through the windows
  11. for (NSMutableDictionary* entry in arr)
  12. {
  13. // Get window PID
  14. pid_t pid = [[entry objectForKey:(id)kCGWindowOwnerPID] intValue];
  15. // Get AXUIElement using PID
  16. AXUIElementRef appRef = AXUIElementCreateApplication(pid);
  17. NSLog(@"Ref = %@",appRef);
  18.  
  19. // Get the windows
  20. CFArrayRef windowList;
  21. AXUIElementCopyAttributeValue(appRef,kAXWindowsAttribute,(CFTypeRef *)&windowList);
  22. NSLog(@"WindowList = %@",windowList);
  23. if ((!windowList) || CFArrayGetCount(windowList)<1)
  24. continue;
  25.  
  26.  
  27. // get just the first window for now
  28. AXUIElementRef windowRef = (AXUIElementRef) CFArrayGetValueAtIndex( windowList,0);
  29. CFTypeRef role;
  30. AXUIElementCopyAttributeValue(windowRef,kAXRoleAttribute,(CFTypeRef *)&role);
  31. CFTypeRef position;
  32. CGPoint point;
  33.  
  34. // Get the position attribute of the window (maybe something is wrong?)
  35. AXUIElementCopyAttributeValue(windowRef,(CFTypeRef *)&position);
  36. AXValueGetValue(position,&point);
  37. // Debugging (always zeros?)
  38. NSLog(@"point=%f,%f",point.x,point.y);
  39. // Create a point
  40. CGPoint newPoint;
  41. newPoint.x = 0;
  42. newPoint.y = 0;
  43. NSLog(@"Create");
  44. position = (CFTypeRef)(AXValueCreate(kAXValueCGPointType,(const void *)&newPoint));
  45. // Set the position attribute of the window (runtime error over here)
  46. NSLog(@"SetAttribute");
  47. AXUIElementSetAttributeValue(windowRef,position);
  48. sleep(5);
  49. }
  50. }
  51. }

猜你在找的C&C++相关文章