我正在为所有屏幕和空格(预先存在和新建)设置桌面背景.然而,我似乎找不到一种方法来设置所有现有空格的背景(并且任何使用旧背景创建的新空格).
这是我到目前为止
let sqlData = NSMutableArray() let paths = NSSearchPathForDirectoriesInDomains(.ApplicationSupportDirectory,.UserDomainMask,true) let appSupportDirectory = paths.first! as NSString let dbPath = appSupportDirectory.stringByAppendingPathComponent("Dock/desktoppicture.db") as NSString var db: COpaquePointer = nil if sqlite3_open(dbPath.UTF8String,&db) == sqlITE_OK { var statement: COpaquePointer = nil if sqlite3_exec(db,"DELETE FROM data",nil,nil) != sqlITE_OK { let errmsg = String.fromCString(sqlite3_errmsg(db)) print("error deleting table row: \(errmsg)") } if sqlite3_exec(db,"INSERT INTO DATA (VALUE) VALUES ('\(getBackgroundImagePath())');",nil) != sqlITE_OK { let errmsg = String.fromCString(sqlite3_errmsg(db)) print("error inserting table row: \(errmsg)") } let workspace = NSWorkspace.sharedWorkspace() for screen in NSScreen.screens()! { do { let options = workspace.desktopImageOptionsForScreen(screen) try workspace.setDesktopImageURL(NSURL(fileURLWithPath: getBackgroundImagePath()),forScreen: screen,options: options!) } catch let error as NSError { NSLog("\(error.localizedDescription)") } } system("/usr/bin/killall Dock") } sqlite3_close(db)
注意:我更新〜/ Library / Application Support / Dock / desktoppicture.db中的.db文件.由于这并没有实际更新背景,所以我继续循环遍历每个屏幕并手动设置.
虽然这会更改屏幕的所有背景,但任何非活动空格都不会更改,而创建的任何新空格都将使用旧的背景.
我在GitHub的一个小应用程序中使用这个代码,这是用户报告的问题. You can find the issue here (with a terminal solution).
苹果有一个看似相关的项目here,但即使他们不更新多个空间.
此外,如果您通过默认的Mac设置应用程序更新背景,它也不会更改预先存在的空格.是不可能吗
OS X桌面图片跨越空间全局更新
在desktoppicture.db中,可以使用新图像(值)的路径在数据表上运行更新查询.不必删除,然后插入值或使用循环.使用无范围的查询调用更新,并通过这样做更新数据表中的每一行.
(以下示例使用SQLite.swift语言层,它使用类型安全的纯Swift界面)
func globalDesktopPicture(image: String) { let paths: [String] = NSSearchPathForDirectoriesInDomains(.ApplicationSupportDirectory,true) let appSup: String = paths.first! let dbPath: String = (appSup as NSString).stringByAppendingPathComponent("Dock/desktoppicture.db") let dbase = try? Connection("\(dbPath)") let value = Expression<String?>("value") let table = Table("data") try! dbase!.run(table.update(value <- image)) system("/usr/bin/killall Dock") }
更改db的相同概念/原则适用的一样,如果您决定使用传统/传统(桥接头)做sqlite3查询的方法:
func legacyGlobalDesktopPicture(image: String) { let paths: [String] = NSSearchPathForDirectoriesInDomains(.ApplicationSupportDirectory,true) let appSup: String = paths.first! let dbPath: String = (appSup as NSString).stringByAppendingPathComponent("Dock/desktoppicture.db") var db: COpaquePointer = nil if sqlite3_open(dbPath,&db) == sqlITE_OK { if sqlite3_exec(db,"UPDATE DATA SET VALUE = ('\(image)');",nil) != sqlITE_OK { let errmsg = String.fromCString(sqlite3_errmsg(db)) print("error inserting table row: \(errmsg)") } system("/usr/bin/killall Dock") } sqlite3_close(db) }
附加信息:
> How to get the path to the current space wallpaper?
> SQLite.swift Documentation : Updating Rows
> SQLite.swift / A type-safe,Swift-language layer over SQLite3