简体   繁体   English

在可可中获取桌面背景

[英]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. 现在的问题是,我需要拥有用户桌面,但没有图标作为全屏窗口的背景,就像10.7中的启动板一样。 I've gotten a reference to the desktop background in AppleScript: 我已经参考了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. 这给了我类似的信息: 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"我根本想不通的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"

I tried doing set a to desktop picture as POSIX path but that throws up on me. 我尝试set a to desktop picture as POSIX path但这使我不安。 Any idea of how I could do this in Cocoa, using the above Applescript to get the path, or even better, without an Applescript? 是否知道如何使用上述Applescript在Cocoa中做到这一点,或者甚至在没有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. 我不想依赖任何可能存储此信息的plist的特定格式,因为它有可能在以后中断。 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. NSWorkspace中提供了您要寻找的方法。

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

Please take a look at the documentation here: NSWorkspace Class Reference 请在此处查看文档: NSWorkspace类参考

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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