dan
dan

Reputation: 5417

Objective C - how to resize a window programmatically with given window id?

How to resize a window of any application programmatically with objective-c / cocoa? So far I've got the app name and the window number but don't know how to access the window.

I could do it with AppleScript but want to learn it with objective c.

AppleScript example:

tell application "System Events"
    set frontApp to name of first application process whose frontmost is true
end tell


tell application frontApp
    set bounds of window 1 to {(screenWidth / 2), 0, screenWidth, screenHeight}
end tell

Thanks for any advice.

Upvotes: 1

Views: 1403

Answers (2)

milkypostman
milkypostman

Reputation: 3043

You're going to need to access the accessibility system somehow. I have no idea what function or object or whatever you need to look at how to send messages to the accessibility of windows.

Oh, but I can give you a link to software I know that does the same thing,
https://github.com/fikovnik/ShiftIt

Look in there for your answer.

I bet if you look around in that code for the accessibilitySetValue function you might strike gold.
http://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Protocols/NSAccessibility_Protocol/Reference/Reference.html#//apple_ref/occ/instm/NSObject/accessibilitySetValue:forAttribute:

Upvotes: 1

Vincent Gable
Vincent Gable

Reputation: 3465

In this case, AppleScript really is the right tool for the job. Telling another application to resize it's window in is an automation problem. Writing that code "in Cocoa" would just mean more work building/sending AppleEvents from a lower level.

I would try using NSAppleScript to load and execute that script you have from within a Cocoa program. If you learn how to do that, you'll know how to embed AppleScript in your Objective-C programs, and that's a handy trick to have in your utility belt.

Good luck!

Upvotes: 2

Related Questions