我正在尝试使用 Accessibility API 来更改其他应用程序窗口的位置。我想做的是从所有应用程序中获取屏幕上的所有窗口,然后将它们全部移动给定的偏移量(比如说 5 或 10或任何值)。我很难做到这一点,因为这是我在 Objective-C 编程的第一天。
这就是我现在正在做的事情。首先,我使用CGWindowListCopyWindowInfo
. 然后,对于我AXUIElementCreateApplication
用来获取窗口的每个AXUIElementRef
窗口。之后,我应该AXUIElementCopyAttributeValue
与属性一起使用kAXPositionAttribute
(我无法获得正确的位置,总是得到零)。最后,我应该将想要的偏移量添加到位置并AXUIElementSetAttributeValue
与属性kAXPositionAttribute
和新位置点一起使用(即使我设置了绝对值,例如 0,0,也会出现运行时错误)。
有人可以帮我做一个我上面描述的片段,因为我尝试了很多事情都没有运气。此外,它不应该完全像我决定在上面实现它一样。如果有更好的方法来做到这一点,那么我很乐意改变它。
更新: 根据评论中的要求,这是其中一种尝试的代码片段:
// 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);
}