How to get window list from core-grapics API with swift

心已入冬 提交于 2020-01-02 06:47:07

问题


I´m trying to get a list of windows on OSX from the core-graphics API with Swift (to capture an image of them later on). After some research I found the CGWindowListCopyWindowInfo Objective-C API call with the following signature:

CFArrayRef CGWindowListCopyWindowInfo(
   CGWindowListOption option,
   CGWindowID relativeToWindow
);

Parameters

option: The options describing which window dictionaries to return. Typical options let you return dictionaries for all windows or for windows above or below the window specified in the relativeToWindow parameter. For more information, see Window List Option Constants.

relativeToWindow: The ID of the window to use as a reference point when determining which other window dictionaries to return. For options that do not require a reference window, this parameter can be kCGNullWindowID.

https://developer.apple.com/library/mac/documentation/Carbon/Reference/CGWindow_Reference/Reference/Functions.html

In my swift application I tried to use it like this:

import Cocoa
import CoreFoundation

let option: CGWindowListOption = kCGWindowListOptionOnScreenOnly
let relativeToWindow: CGWindowID = kCGNullWindowID

let info = CGWindowListCopyWindowInfo(option, relativeToWindow)

But XCode (playground) tells me

  • it can´t use an int as CGWindowListOption (kCGWindowListOptionOnScreenOnly == 0)
  • kCGNullWindowID is an unresolved identifier

What am I doing wrong here?


回答1:


  • kCGWindowListOptionOnScreenOnly is an Int, you have to convert that to CGWindowListOption aka UInt32.
  • The C definition

    #define kCGNullWindowID ((CGWindowID)0) 
    

    is not imported into Swift, therefore you have to use the constant 0.

  • In addition, CGWindowListCopyWindowInfo() returns an Unmanaged<CFArray>!, therefore you have to call takeRetainedValue() on the returned value (as documented in "Working with Cocoa Data Types").

Together:

let option = CGWindowListOption(kCGWindowListOptionOnScreenOnly)
let relativeToWindow = CGWindowID(0)
let info = CGWindowListCopyWindowInfo(option, relativeToWindow).takeRetainedValue()

Then you can enumerate this array of dictionaries with

for dict in info as! [ [ String : AnyObject] ] {
    // ...
}

Update for Swift 3:

if let info = CGWindowListCopyWindowInfo(.optionOnScreenOnly, kCGNullWindowID) as? [[ String : Any]] {
    for dict in info {
        // ...
    }
}


来源:https://stackoverflow.com/questions/30336740/how-to-get-window-list-from-core-grapics-api-with-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!