10

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

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

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

在此处输入图像描述

4

2 回答 2

5

还有另一种方法是通过截取当前壁纸的屏幕截图来获取图像。

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。

于 2015-10-03T01:38:57.027 回答
4

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

  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:@""];
于 2013-03-03T02:17:11.257 回答