上面OS X 10.10
有一个SQLite
名为 .x 的 3.x 数据库desktoppicture.db
。当发生定时随机桌面图片转换或系统偏好设置 > 桌面发生任何更改时,此 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 CurrentDesktop[72591:87862519] 桌面图片:Poppies.jpg 2015-06-23 14:36:04.470 CurrentDesktop[72591:87862519] ("60.0", 1, "Poppies.jpg" )
您可以通过多种不同的方式从该文件中获取数据(例如NSTask
、Bash
、AppleScript
等。这是我的首选解决方案,因为它是本机 mac 代码;不过它很简单,可以移植到其他东西上。