17

Is it possible to iterate over all open windows of any application in OSX and listen to their resize events in Swift? I want to create custom window manager that would move and resize events based on user behavior – so if user resizes a window, other windows get automatically re-arranged.

I'm Haskell, not a Swift developer, so I would be very thanful for any code snippet / example showing how to achieve this effect. (In fact I'll use it as a kind of Haskell -> Cocoa binding.)

Edit: I would be very interested in seing the solution in Objective-C also, but Swift is more important for me here.

4 Answers 4

9
+200

Is it possible to iterate over all open windows of any application in OSX and listen to their resize events in Swift?

It is possible to do this, a better way would be to have your custom window manager class implement the NSWindowDelegate protocol and set all window's delegate to your custom window manager. This will give you all the resize and move information you require.

I'm Haskell, not a Swift developer, so I would be very thanful for any code snippet / example showing how to achieve this effect.

Implementing protocol methods:

class CustomWindowManager: NSWindowController, NSWindowDelegate {

       // Resize Methods

       func windowWillResize(sender: NSWindow,toSize frameSize: NSSize) -> NSSize {

            // Your code goes here
       }

       func windowDidResize(notification: NSNotification) {

           // Your code goes here
       }
}

You can then just chose which protocol methods you want to implement as all methods in the protocol are optional.

You will have to be able to identify which window is calling the delegate method and track any windows you move or resize due to reacting to the initial window change. See this Stack Overflow question for help with this.

For getting a list of all windows currently on screen see this Stack Overflow question.

Edit


For getting a list of all windows of your application currently on screen see this Stack Overflow question.

2
  • 1
    I do not think that you can set the delegate f another app's window … Dec 14, 2015 at 8:35
  • @AminNegm-Awad - what do you mean? Again, I've got a very limited free time right now so I will be pushing this project very slowly - I do not know Objective-C / Swift nor Cocoa API so I was asking the most basic question here. Would you be so nice and elaborate on it more? Dec 15, 2015 at 0:25
3

first step,you should add NSWindowDelegate and function windowWillResize,full code like this:

class MainWindowController: NSWindowController, NSWindowDelegate {

    override var windowNibName: String? {
        return "MainWindowController"
    }

    override func windowDidLoad() {
        super.windowDidLoad()
    }

    func windowDidResize(notification: NSNotification) {
        //listen the event
    }

}

then you should set the delegate property,set MainWindowController.xib include file window's delegate property to File's Owner.

enter image description here

2

With Swift 3 the windowDidResize method should look like the following:

func windowDidResize(_ notification: Notification) {
    // Listen to a window resize event

}
1
  1. create an NSWindowController Class
  2. Open your storyboard and find your window controller then set the Class as custom Class (identity inspect tab)
  3. create an NSWindowDelegate Class with windowWillResize method
  4. set the NSWindowDelegate instance as the window.delegate property of your NSWindowController instance

When user change window size,the windowWillResize method will be called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.