【发布时间】:2014-01-11 23:07:42
【问题描述】:
我正在尝试使用 Accessibility API 来更改其他应用程序窗口的位置。我想做的是从所有应用程序中获取屏幕上的所有窗口,然后将它们全部移动给定的偏移量(假设5 或 10 或任何值)。我很难做到这一点,因为这是我使用 Objective-C 编程的第一天。
这就是我现在正在做的事情。首先,我使用CGWindowListCopyWindowInfo 找到窗口列表及其PID。然后,对于每个窗口,我使用AXUIElementCreateApplication 来获取窗口的AXUIElementRef。之后,我应该使用 AXUIElementCopyAttributeValue 和属性 kAXPositionAttribute (我未能获得正确的位置,总是得到零)。最后,我应该将想要的偏移量添加到位置并使用 AXUIElementSetAttributeValue 与属性 kAXPositionAttribute 和新的位置点(即使我设置绝对值如 0,0,也会出现运行时错误)。
有人可以帮我做我上面描述的sn-p,因为我尝试了很多事情都没有运气。此外,它不应该完全像我在上面决定实现的那样。如果有更好的方法来做到这一点,那么我很乐意改变它。
更新: 根据评论中的要求,这是其中一种尝试的代码 sn-p:
// Get all the windows
CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
NSArray* arr = CFBridgingRelease(windowList);
// Loop through the windows
for (NSMutableDictionary* entry in arr)
{
// Get window PID
pid_t pid = [[entry objectForKey:(id)kCGWindowOwnerPID] intValue];
// Get AXUIElement using PID
AXUIElementRef elementRef = AXUIElementCreateApplication(pid);
CFTypeRef position;
CGPoint point;
// Get the position attribute of the window (maybe something is wrong?)
AXUIElementCopyAttributeValue(elementRef, kAXPositionAttribute, (CFTypeRef *)&position);
AXValueGetValue(position, kAXValueCGPointType, &point);
// Debugging (always zeros?)
NSLog(@"point=%@", point);
// Create a point
NSPoint newPoint;
newPoint.x = 0;
newPoint.y = 0;
position = (CFTypeRef)(AXValueCreate(kAXValueCGPointType, (const void *)&newPoint));
// Set the position attribute of the window (runtime error over here)
AXUIElementSetAttributeValue(elementRef, kAXPositionAttribute, (CFTypeRef *)&position);
}
【问题讨论】:
-
发布您的一次尝试中的代码,以便人们可以看到您从什么开始。
-
它是分步解释的,我所拥有的不起作用,所以我认为不需要一个不起作用的。我已经用 sn-p 更新了它。
标签: objective-c macos accessibility-api window-position