由于AppKit版本10.7,NSWorkspace.desktopImageForScreen可能返回一个文件夹的路径,而不是当前的墙纸文件的URL。这个文件夹是一个地方,壁纸将从那里顺序拾取,以供显示。(在setDesktopImageURL
中搜索发布说明)。
如果用户将桌面图像设置为每隔30分钟随机更改一次,那么有什么方法可以确定OSX中每个屏幕当前活动的壁纸是什么?
Update:基于@l‘l的答案,我创建了一个小型Mac应用程序,以方便地找到当前活动的壁纸:https://github.com/musically-ut/whichbg
发布于 2015-06-23 21:45:04
在OS X 10.10
上有一个名为desktoppicture.db
的SQLite
3.x数据库。此db文件存储当前桌面图片、目录、空间、间隔等信息,当发生时间随机桌面图片转换或系统首选项>桌面发生任何更改时:
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 CurrentDesktop72591:87862519桌面图片: Poppies.jpg 2015-06-23 14:36:04.470 CurrentDesktopPoppies.jpg
有很多其他的方法,你可以从这个文件获得数据(例如。NSTask
,Bash
,AppleScript
等。这是我最喜欢的解决方案,因为它是本地的mac代码;它很简单,可以移植到其他东西上。
https://stackoverflow.com/questions/30954492
复制相似问题