简体   繁体   中英

Getting Desktop Background in Cocoa

I'm needing to do something full-screen app, which would usually not be the problem. The problem now is that I need to have the user's desktop, but without icons, as the background of my full screen window, much like Launchpad in 10.7. I've gotten a reference to the desktop background in AppleScript:

tell application "Finder"
    set a to desktop picture
end tell

This gives me something like this: document file "100930-F-7910D-001.jpg" of folder "Pictures" of folder "Fighter Jet Stuff" of folder "Desktop" of folder "tristan" of folder "Users" of startup disk of application "Finder" which I just could not figure out to get into a regular path.

I tried doing set a to desktop picture as POSIX path but that throws up on me. Any idea of how I could do this in Cocoa, using the above Applescript to get the path, or even better, without an Applescript? I'd like to not rely on the specific format of any plist that might store this info, as it has the potential to break later on. I'm thinking there might be a framework that I just don't know about...

The methods you are looking for are available in NSWorkspace.

– desktopImageURLForScreen:
– setDesktopImageURL:forScreen:options:error:
– desktopImageOptionsForScreen:

Please take a look at the documentation here: NSWorkspace Class Reference

If you needs just the current wallpaper, you can take a screenshot of it:

extension NSImage {

    static func desktopPicture() -> NSImage {

        let windows = CGWindowListCopyWindowInfo(
            CGWindowListOption.OptionOnScreenOnly,
            CGWindowID(0))! as NSArray

        var index = 0
        for var i = 0; i < windows.count; i++  {
            let window = windows[i]

            // we need windows owned by Dock
            let owner = window["kCGWindowOwnerName"] as! String
            if owner != "Dock" {
                continue
            }

            // we need windows named like "Desktop Picture %"
            let name = window["kCGWindowName"] as! String
            if !name.hasPrefix("Desktop Picture") {
                continue
            }

            // wee need the one which belongs to the current screen
            let bounds = window["kCGWindowBounds"] as! NSDictionary
            let x = bounds["X"] as! CGFloat
            if x == NSScreen.mainScreen()!.frame.origin.x {
                index = window["kCGWindowNumber"] as! Int
                break
            }
        }

        let cgImage = CGWindowListCreateImage(
            CGRectZero,
            CGWindowListOption(arrayLiteral: CGWindowListOption.OptionIncludingWindow),
            CGWindowID(index),
            CGWindowImageOption.Default)!

        let image = NSImage(CGImage: cgImage, size: NSScreen.mainScreen()!.frame.size)
        return image
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM