Showing a list of all running windows - Swift

Joined
Feb 6, 2011
Messages
13
Reaction score
0
Points
1
Hi everyone! I am new to swift - and new in general to programming on mac osx.

I want to get an array of open windows - loop through them and print out to a console each of the names of the open windows.

So far I have made a new Cocoa application, in Swift, and in my AppDelegate.swift added the following code:

Code:
import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {



    func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Insert code here to initialize your application
        
        println("hello world");
        CFArray windows = CGWindowListCopyWindowInfo(CGWindowListOption.optionAll, CGWindowID.kCGNullWindowID);
        
    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }


}

Needless to say - it doesn't like my println line - can't find the write syntax to just write text to a console :Angry-Tongue: and also - it seems I am not declaring the CFArray correctly - to store the response array of dictionaries from my CGWindowListCopyWindowInfo call.

Am I on the right track? What should I change the syntax to? Any pointers would be ace!
 
OP
R
Joined
Feb 6, 2011
Messages
13
Reaction score
0
Points
1
Thanks Raz0rEdge - but that all looks wildly out of date - none of those answers work out the box. Do they work for you?

I tried changing to:

let options = CGWindowListOption(arrayLiteral: CGWindowListOption.excludeDesktopElements, CGWindowListOption.optionAll);
let windowListInfo = CGWindowListCopyWindowInfo(options, CGWindowID(0));
let infoList = windowListInfo as NSArray? as? [[String: AnyObject]]

Which compiles! However I need to know how to loop around this infoList and print to console to take a look at the output? How do I do the println? And loop over infoList?
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top