Created
April 28, 2025 21:40
-
-
Save Wowfunhappy/59b165f9af5bfea590d82ea307fb2a95 to your computer and use it in GitHub Desktop.
Reveal Current Desktop Picture In Finder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Build with compiler flag `-lsqlite3`. | |
#import <Foundation/Foundation.h> | |
#import <AppKit/AppKit.h> | |
#import <sqlite3.h> | |
NSString *getDesktopImagePath() { | |
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
NSURL *imageURL = [[NSWorkspace sharedWorkspace] desktopImageURLForScreen:[NSScreen mainScreen]]; | |
NSString *urlPath = [imageURL path]; | |
// Check if it's a file that exists | |
BOOL isDirectory; | |
BOOL exists = [fileManager fileExistsAtPath:urlPath isDirectory:&isDirectory]; | |
if (exists) { | |
if (!isDirectory) { | |
return urlPath; | |
} | |
// Use SQLite to get the filename | |
NSString *appSup = [NSSearchPathForDirectoriesInDomains( | |
NSApplicationSupportDirectory, | |
NSUserDomainMask, | |
YES | |
) 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) { | |
NSMutableArray *sqliteData = [NSMutableArray array]; | |
while(sqlite3_step(sel) == SQLITE_ROW) { | |
NSString *data = [NSString stringWithUTF8String:(char *)sqlite3_column_text(sel, 0)]; | |
[sqliteData addObject:data]; | |
} | |
if ([sqliteData count] > 0) { | |
NSString *imageFileName = [sqliteData lastObject]; | |
NSString *imagePath = [urlPath stringByAppendingPathComponent:imageFileName]; | |
if ([fileManager fileExistsAtPath:imagePath]) { | |
sqlite3_close(database); | |
return imagePath; | |
} | |
} | |
} | |
sqlite3_close(database); | |
} | |
} | |
return nil; | |
} | |
int main(int argc, char *argv[]) { | |
@autoreleasepool { | |
NSString *imagePath = getDesktopImagePath(); | |
if (imagePath) { | |
[[NSWorkspace sharedWorkspace] selectFile:imagePath inFileViewerRootedAtPath:@""]; | |
} else { | |
NSLog(@"Could not find current desktop picture."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Code to get the filename from the sqlite database is from https://stackoverflow.com/a/31014090/6358721