我使用此代碼來獲取當前壁紙:獲取可可中的當前壁紙
NSURL *imageURL = [[NSWorkspace sharedWorkspace] desktopImageURLForScreen:[NSScreen mainScreen]];
這工作得很好,但是當我設置圖片的文件夾是壁紙(如圖片所示),imageURL
是一個目錄,所以在這種情況下如何獲得當前壁紙的USURL?
我使用此代碼來獲取當前壁紙:獲取可可中的當前壁紙
NSURL *imageURL = [[NSWorkspace sharedWorkspace] desktopImageURLForScreen:[NSScreen mainScreen]];
這工作得很好,但是當我設置圖片的文件夾是壁紙(如圖片所示),imageURL
是一個目錄,所以在這種情況下如何獲得當前壁紙的USURL?
我一直試圖做同樣的事情。 你可以得到當前桌面圖片網址:
我仍然在做這個,但下面的代碼獲得正確的網址...有時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? – scolfax
還有另一種方式可以通過截取當前壁紙來獲取圖像。
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中不再定義壁紙:從小牛開始,設置被移至〜/ Library/Application Support/Dock/desktoppicture.db。這是SQLite文件,「data」表包含url。
我已經打開https://openradar.appspot.com/radar?id=5782854294306816,因爲如果API做到了承諾,它會更好。 –