Skip to content

Commit

Permalink
Fix case if the wallpaper is set to change every X minutes (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
rawgni authored and sindresorhus committed Jul 24, 2016
1 parent 5f2a761 commit 397287d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

filename=wallpaper

clang $filename.m -fmodules -mmacosx-version-min=10.6 -o $filename
clang $filename.m -fmodules -mmacosx-version-min=10.6 -lsqlite3 -o $filename
37 changes: 36 additions & 1 deletion wallpaper.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

@import AppKit;
#import <sqlite3.h>

int main() {
@autoreleasepool {
Expand Down Expand Up @@ -38,7 +39,41 @@ int main() {
return 1;
}
} else {
printf("%s\n", [sw desktopImageURLForScreen:screen].path.UTF8String);
NSString *url = [sw desktopImageURLForScreen:screen].path;
BOOL isDir;
NSFileManager *fm = [NSFileManager defaultManager];

// check if file is a directory
[fm fileExistsAtPath:url isDirectory:&isDir];

// if directory, check db
if (isDir) {
NSArray *dirs = [fm URLsForDirectory:NSApplicationSupportDirectory
inDomains:NSUserDomainMask];
if ([dirs count] > 0) {
NSURL *dbLoc = [[dirs objectAtIndex:0] URLByAppendingPathComponent:@"Dock/desktoppicture.db"];
const char *dbPath = [[dbLoc path] UTF8String];
sqlite3 *db = nil;

if (sqlite3_open(dbPath, &db) == SQLITE_OK) {
sqlite3_stmt *statement;
const char *sql = "SELECT * FROM data";

if (sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK) {
NSString *file;
while (sqlite3_step(statement) == SQLITE_ROW) {
file = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
}

printf("%s/%s\n", url.UTF8String, file.UTF8String);
sqlite3_finalize(statement);
}
sqlite3_close(db);
}
}
} else {
printf("%s\n", url.UTF8String);
}
}
}

Expand Down

0 comments on commit 397287d

Please sign in to comment.