I know, that I can use Apple Event Object Model for moving and resizing windows of Cocoa applications. But what can I use for Carbon applications?
Asked
Active
Viewed 2,906 times
3 Answers
4
Peter was right, you can access to bounds of any window using the following 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

Kentzo
- 3,881
- 29
- 54
2
You can also use the Accessibility API. This is how I think Optimal Layout is doing it.
First you have make sure your app has permission to use it.
BOOL checkForAccessibility()
{
NSDictionary *options = @{(__bridge id) kAXTrustedCheckOptionPrompt : @YES};
return AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef) options);
}
Next, use the NSWorkspace::RunningApplications to get the PID of the app whose window you want to manipulate.
NSArray<NSRunningApplication *> *runningApps =[[NSWorkspace sharedWorkspace] runningApplications];
for( NSRunningApplication *app in runningApps )
{
if( [app bundleIdentifier] != nil && [[app bundleIdentifier] compare:@"IdentifierOfAppYouWantToFindHere"] == 0 )
{
PID = [app processIdentifier];
}
}
Then use the PID to get access to the main window reference using the 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 );
Now you can set the size and position using something like this:
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 );

Phiveaces
- 21
- 1
-
I had similar code, I was trying to control resize based on mouse-movement, but I could not get it to be as smooth/fast as just pulling the corners; any idea why? – Karric Sep 07 '19 at 16:17
-
I don't recommend using the code above anymore. Since Mojave, I have serious performance issues when using the above code with my application. It results in what appears to be the whole system freezing. Apple has said to try using the Applescript method, but I am still working that out. I don't think this method will work all that well for doing what you are trying to do with the mouse resizing. – Phiveaces Sep 09 '19 at 17:32
-
The code I alluded to is practically what you have above but is for moving application windows around — I don't see any performance issues there; it's practically instant. But resizing was a problem when I tried it; I might just snap to multiples of a value to hide the lag instead. Thanks for replying! – Karric Sep 10 '19 at 18:05
1
Same thing. You can use Apple Events on any scriptable application, and Apple Events and scriptability are a lot older than Carbon.

Peter Hosey
- 95,783
- 15
- 211
- 370
-
But what can I do with application that doesn't support scrip-ability (like cross-platform apps)? – Kentzo Nov 20 '10 at 10:30
-
Cross-platform doesn't mean unscriptable. But anyway: Nothing. The application is in control of its own windows, so you have to ask it to move or resize them. If it doesn't provide a way for you to do that, then there is nothing you can do to move or resize that application's windows. – Peter Hosey Nov 20 '10 at 10:41
-
I disagre. E.g. I have application called "PyCharm" — cross-platform python IDE. When I ask for 'has scripting terminology' via 'System Events', it returns false. However, it's windows can be moved/resized via apps like "Optimal Layout". – Kentzo Nov 20 '10 at 10:54
-
Then I suggest contacting the developers of Optimal Layout to ask how they do it. (Perhaps pass them the link to your question so they can post their answer for everyone.) I can't think of any public API, aside from the Apple Event Manager and its Cocoa and OSAKit equivalents, to move another application's windows. – Peter Hosey Nov 20 '10 at 11:33