【问题标题】:Getting Desktop Background in Cocoa在 Cocoa 中获取桌面背景
【发布时间】:2015-10-05 17:00:44
【问题描述】:

我需要做一些全屏应用程序,这通常不是问题。现在的问题是我需要有用户的桌面,但没有图标,作为我全屏窗口的背景,很像 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 的特定格式,因为它有可能在以后中断。我在想可能有一个我不知道的框架......

【问题讨论】:

    标签: cocoa applescript desktop-background


    【解决方案1】:

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

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

    请在此处查看文档:NSWorkspace Class Reference

    【讨论】:

    • 谢谢,我完全忽略了这一点。我应该先完全看那里,而不是让我的生活过于复杂。我用谷歌搜索...什么都没有。谢谢! =P
    • 如果桌面图像设置为每 x 秒“更改图片”,这不起作用,海报的 applescript 也不起作用。在这种情况下,我无法弄清楚如何获得正确的路径。
    • 注意:OS 10.7 之后 Sandbox 下,你无法通过 desktopImageURLForScreen 获取图片文件的正确 URL,你得到的将是你的沙盒容器中的那个。
    • 回答@regulus6633 评论,有一种更改文件夹的hacky方法,请参阅stackoverflow.com/a/15181763/172690
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-17
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多