How can I move / resize a window programmatically from another application?

I know that I can use the Apple Event object model to move and resize Cocoa application windows. But what can I use for Carbon apps?

+3
source share
3 answers

Same. You can use Apple Events for any scripting application, and Apple Events and scriptability are much older than Carbon.

+1
source

Peter was right, you can access the borders 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
+4
source

Accessibility 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 );
+1

Source: https://habr.com/ru/post/1775938/


All Articles