这是我到目前为止
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设置应用程序更新背景,它也不会更改预先存在的空格.是不可能吗
在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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。