如何获取 OSX 当前 workspace/screen 的壁纸路径?

How to get the path to the current workspace/screen's wallpaper on OSX?

从 AppKit 版本 10.7 开始,NSWorkspace.desktopImageForScreen may return a path to a folder instead of a file's URL which is currently the wallpaper. This folder is the the place from where the wallpapers will be sequentially picked up for display. (Search for setDesktopImageURL in the release notes)。

如果用户将桌面图像设置为每 30 分钟左右随机更改一次,是否有任何方法可以确定 OSX 中每个屏幕的当前活动壁纸是什么?


更新:根据@l'L'l 的回答,我创建了一个小的 Mac OSX 应用程序来方便地找到当前活动的壁纸:https://github.com/musically-ut/whichbg

OS X 10.10 上有一个名为 desktoppicture.dbSQLite 3.x 数据库。此 db 文件存储当前桌面图片、目录、space、间隔等信息,当发生定时随机桌面图片转换或系统偏好设置 > 桌面有任何更改时:

Objective-C

// Get Current Desktop Picture

- (IBAction)getDesktopPicture:(id)sender {

    [self getCurrentDesktop];
}

-(void)getCurrentDesktop {

    NSMutableArray *sqliteData = [[NSMutableArray alloc] init];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
    NSString *appSup = [paths firstObject];
    NSString *dbPath = [appSup stringByAppendingPathComponent:@"Dock/desktoppicture.db"];

    sqlite3 *database;
    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {

        const char *sql = "SELECT * FROM data";
        sqlite3_stmt *sel;
        if(sqlite3_prepare_v2(database, sql, -1, &sel, NULL) == SQLITE_OK) {

            while(sqlite3_step(sel) == SQLITE_ROW) {
                NSString *data = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sel, 0)];
                [sqliteData addObject:data];
            }
        }
    }
    NSUInteger cnt = [sqliteData count] - 1;
    NSLog(@"Desktop Picture: %@", sqliteData[cnt]);
    NSLog(@"%@",sqliteData);

    sqlite3_close(database); 
}

结果:

2015-06-23 14:36:04.470 CurrentDesktop[72591:87862519] Desktop Picture: Poppies.jpg 2015-06-23 14:36:04.470 CurrentDesktop[72591:87862519] ( "60.0", 1, "Poppies.jpg" )

您可以通过多种其他方式从该文件中获取数据(例如 NSTaskBashAppleScript 等。这是我的首选解决方案,因为它是本机 mac 代码;它足够简单,可以移植到其他地方。