【问题标题】:Get the current wallpaper in Cocoa在 Cocoa 中获取当前壁纸
【发布时间】:2012-12-31 08:33:00
【问题描述】:

我正在使用此代码获取当前壁纸:

NSURL *imageURL = [[NSWorkspace sharedWorkspace] desktopImageURLForScreen:[NSScreen mainScreen]];

这工作正常,但是当我将图片文件夹设置为壁纸时(如图所示),imageURL 是一个目录,那么在这种情况下如何获取当前壁纸的 USURL?

【问题讨论】:

标签: cocoa wallpaper


【解决方案1】:

还有另一种方法可以通过截取当前壁纸来获取图像。

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

恕我直言,这种方法看起来要简单得多,如果您需要图片,而不是网址。

请注意,com.apple.dektop plist 中不再定义墙纸:从 Mavericks 开始,设置已移至 ~/Library/Application Support/Dock/desktoppicture.db。这是 SQLite 文件,“数据”表包含 url。

【讨论】:

    【解决方案2】:

    我一直在尝试做同样的事情。 您可以通过以下方式获取当前桌面图片网址:

    1. 从 com.apple.spaces 属性列表中获取当前空间 UUID,
    2. 在 com.apple.desktop 属性列表中搜索匹配空间,
    3. 从 LastName 属性中提取 URL

    我仍在处理此问题,但以下代码有时会获得正确的 URL。 主要问题是属性列表更新不够频繁,而且我无法强制它们刷新(没有杀死码头)。如果你想出什么办法,请告诉我!

    NSDictionary *spacesPLIST = (__bridge NSDictionary *)(CFPreferencesCopyAppValue(CFSTR("SpacesConfiguration"), CFSTR("com.apple.spaces")));
    NSDictionary *desktopPLIST = (__bridge NSDictionary *)(CFPreferencesCopyAppValue(CFSTR("Background"), CFSTR("com.apple.desktop")));
    
    NSArray *monitors = [spacesPLIST valueForKeyPath:@"Management Data.Monitors"];
    NSInteger monitorIndex = 0;
    if ([monitors count] > 1) {
        //search for main (or ask user to select)
    }
    NSDictionary *monitor = [monitors objectAtIndex:monitorIndex];
    NSDictionary *spaces = [desktopPLIST valueForKey:@"spaces"];
    NSString *currentSpaceUUID = [monitor valueForKeyPath:@"Current Space.uuid"];
    NSDictionary *currentSpace = [spaces valueForKey:currentSpaceUUID];
    NSURL *desktopPicturesDirectory = [NSURL fileURLWithPath:[currentSpace valueForKeyPath:@"default.ChangePath"] isDirectory:true];
    NSString *desktopPictureName = [currentSpace valueForKeyPath:@"default.LastName"];
    NSURL *desktopPictureURL = [NSURL URLWithString:desktopPictureName relativeToURL:desktopPicturesDirectory];
    [[NSWorkspace sharedWorkspace] selectFile:[desktopPictureURL path] inFileViewerRootedAtPath:@""];
    

    【讨论】:

    • 您希望 __bridge_transfer 在这些演员表中,因为 CFPreferencesCopyAppValue 返回一个拥有的引用。此外,NSWorkspace 有activateFileViewerSelectingURLs:,无需从 URL 中提取路径,NSURL 有URLByAppendingPathComponent:
    • 有没有人想出一种方法来刷新属性列表而不必杀死 Dock?
    猜你喜欢
    • 2012-04-13
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 2022-12-14
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    相关资源
    最近更新 更多