【发布时间】:2010-11-20 03:43:14
【问题描述】:
我知道,我可以使用 Apple 事件对象模型来移动和调整 Cocoa 应用程序窗口的大小。但是我可以将什么用于 Carbon 应用程序?
【问题讨论】:
标签: macos macos-carbon
我知道,我可以使用 Apple 事件对象模型来移动和调整 Cocoa 应用程序窗口的大小。但是我可以将什么用于 Carbon 应用程序?
【问题讨论】:
标签: macos macos-carbon
Peter 是对的,您可以使用以下 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 );
【讨论】:
同样的事情。您可以在任何可编写脚本的应用程序上使用 Apple Events,而且 Apple Events 和可编写脚本比 Carbon 早了很多。
【讨论】: