获取当前壁纸在可可 [英] Get the current wallpaper in Cocoa

查看:154
本文介绍了获取当前壁纸在可可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这段代码获取当前的墙纸:

I'm using this code to get the current wallpaper:

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

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

This works fine, but when i set a folder of pictures to be the wallpaper(As shown in the picture), the imageURL is a directory, so how can i get the current wallpaper's USURL in this situation?

推荐答案

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

I've been trying to do the exact same thing. You can get the current desktop picture URL by:


  1. 从com.apple.spaces属性列表获取当前空间UUID ,

  2. 搜索com.apple.desktop属性列表中的匹配空格,

  3. 从LastName属性中提取URL

我仍然在做这件事,但下面的代码获得正确的URL ...有时。
主要的问题是属性列表不够频繁地更新,我不能强迫他们刷新(杀死dock的时间短)。

I am still working on this, but the following code obtains the correct URL... sometimes. The main problem is that the property lists aren't updated frequently enough, and I haven't been able to force them to refresh (short of killing the dock). Let me know if you figure something out!

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:@""];

这篇关于获取当前壁纸在可可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆