1

我需要做一些全屏应用程序,这通常不是问题。现在的问题是我需要有用户的桌面,但没有图标,作为我全屏窗口的背景,很像 10.7 中的 Launchpad。我在 AppleScript 中获得了对桌面背景的引用:

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

这给了我这样的东西: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"我只是想不出要进入常规路径。

我试着做set a to desktop picture as POSIX path,但这让我很失望。知道如何在 Cocoa 中做到这一点,使用上面的 Applescript 来获取路径,甚至更好,没有 Applescript?我不想依赖任何可能存储此信息的 plist 的特定格式,因为它有可能在以后中断。我在想可能有一个我不知道的框架......

4

2 回答 2

8

您正在寻找的方法在 NSWorkspace 中可用。

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

请在此处查看文档:NSWorkspace 类参考

于 2011-03-15T03:01:59.717 回答
0

如果您只需要当前的壁纸,您可以对其进行截图:

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
    }
}
于 2015-10-03T01:43:07.800 回答