我知道,我可以使用Apple事件对象模型来移动和调整Cocoa应用程序的窗口大小。但是我可以在Carbon应用程序中使用什么?
一样。您可以在任何可编写脚本的应用程序上使用Apple Events,Apple Events和可编写脚本的功能比Carbon早得多。
彼得是对的,您可以使用以下AppleScript访问任何窗口的边界:
tell application "System Events"
set allProcesses to application processes
repeat with i from 1 to count allProcesses
tell process i
repeat with x from 1 to (count windows)
position of window x
size of window x
end repeat
end tell
end repeat
end tell
您还可以使用辅助功能API。这就是我认为Optimal Layout正在做的事情。
首先,您要确保您的应用有权使用它。
BOOL checkForAccessibility()
{
NSDictionary *options = @{(__bridge id) kAXTrustedCheckOptionPrompt : @YES};
return AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef) options);
}
接下来,使用NSWorkspace :: RunningApplications获取要操作其窗口的应用程序的PID。
NSArray<NSRunningApplication *> *runningApps =[[NSWorkspace sharedWorkspace] runningApplications];
for( NSRunningApplication *app in runningApps )
{
if( [app bundleIdentifier] != nil && [[app bundleIdentifier] compare:@"IdentifierOfAppYouWantToFindHere"] == 0 )
{
PID = [app processIdentifier];
}
}
然后使用PID使用Accessibility API访问主窗口引用。
AXUIElementRef app = AXUIElementCreateApplication( PID );
AXUIElementRef win;
AXError error = AXUIElementCopyAttributeValue( app, kAXMainWindowAttribute, ( CFTypeRef* )&win );
while( error != kAXErrorSuccess ) // wait for it... wait for it.... YaY found me a window! waiting while program loads.
error = AXUIElementCopyAttributeValue( app, kAXMainWindowAttribute, ( CFTypeRef* )&win );
现在您可以使用以下内容设置大小和位置:
CGSize windowSize;
CGPoint windowPosition;
windowSize.width = width;
windowSize.height = height;
windowPosition.x = x;
windowPosition.y = y;
AXValueRef temp = AXValueCreate( kAXValueCGSizeType, &windowSize );
AXUIElementSetAttributeValue( win, kAXSizeAttribute, temp );
temp = AXValueCreate( kAXValueCGPointType, &windowPosition );
AXUIElementSetAttributeValue( win, kAXPositionAttribute, temp );
CFRelease( temp );
CFRelease( win );