Post

Replies

Boosts

Views

Activity

Getting path of deleted item from Scripting Bridge SBObject in Swift
In the Swift function at the end of this post, I use Scripting Bridge to have Finder delete a path. The variable result is a SBObject returned by the delete() function. I know that result somehow contains the new path of the deleted item in the trash folder, but I don't know how to nicely extract it as a single String. If I print(String(describing: result)), I get output like: <SBObject @0x0123456789ab: <class 'appf'> "AppName.app" of <class 'cfol'> ".Trash" of <class 'cfol'> "user" of <class 'cfol'> "Users" of startupDisk of application "Finder" (822)> Is there any way to obtain the String "/Users/user/.Trash/AppName.app" from result without having to perform string parsing on the above output? The Finder* types in the code below are from https://github.com/tingraldi/SwiftScripting/blob/master/Frameworks/FinderScripting/FinderScripting/Finder.swift func trash(path: String) throws { guard let finder: FinderApplication = SBApplication(bundleIdentifier: "com.apple.finder") else { throw runtimeError("Failed to obtain Finder access: com.apple.finder does not exist") } guard let items = finder.items else { throw runtimeError("Failed to obtain Finder access: finder.items does not exist") } let object = items().object(atLocation: URL(fileURLWithPath: path)) guard let item = object as? FinderItem else { throw runtimeError( """ Failed to obtain Finder access: finder.items().object(atLocation: URL(fileURLWithPath: \ \"\(path)\") is a '\(type(of: object))' that does not conform to 'FinderItem' """ ) } guard let delete = item.delete else { throw runtimeError("Failed to obtain Finder access: FinderItem.delete does not exist") } let result = delete() }
2
0
221
Oct ’24
NSMetadataQuery threading issues
The code below is a simplified form of part of my code for my Swift Package Manager, Swift 5.6.1, PromiseKit 6.22.1, macOS command-line executable. It accepts a Mac App Store app ID as the sole argument. If the argument corresponds to an app ID for an app that was installed from the Mac App Store onto your computer, the executable obtains some information from Spotlight via a NSMetadataQuery, then prints it to stdout. I was only able to get the threading to work by calling RunLoop.main.run(). The only way I was able to allow the executable to return instead of being stuck forever on RunLoop.main.run() was to call exit(0) in the closure passed to Promise.done(). The exit(0) causes problems for testing. How can I allow the executable to exit without explicitly calling exit(0), and how can I improve the threading overall? I cannot currently use Swift Concurrency (await/async/TaskGroup) because the executable must support macOS versions that don't support Swift Concurrency. A Swift Concurrency solution variant would be useful as additional info, though, because, sometime in the future, I might be able to drop support for macOS versions older than 10.15. Thanks for any help. import Foundation import PromiseKit guard CommandLine.arguments.count > 1 else { print("Missing adamID argument") exit(1) } guard let adamID = UInt64(CommandLine.arguments[1]) else { print("adamID argument must be a UInt64") exit(2) } _ = appInfo(forAdamID: adamID) .done { appInfo in if let jsonData = try? JSONSerialization.data(withJSONObject: appInfo), let jsonString = String(data: jsonData, encoding: .utf8) { print(jsonString.replacingOccurrences(of: "\\/", with: "/")) } exit(0) } RunLoop.main.run() func appInfo(forAdamID adamID: UInt64) -> Promise<[String: Any]> { Promise { seal in let query = NSMetadataQuery() query.predicate = NSPredicate(format: "kMDItemAppStoreAdamID == %d", adamID) query.searchScopes = ["/Applications"] var observer: NSObjectProtocol? observer = NotificationCenter.default.addObserver( forName: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: query, queue: .main ) { _ in query.stop() defer { if let observer { NotificationCenter.default.removeObserver(observer) } } var appInfo: [String: Any] = [:] for result in query.results { if let result = result as? NSMetadataItem { var attributes = ["kMDItemPath"] attributes.append(contentsOf: result.attributes) for attribute in attributes { let value = result.value(forAttribute: attribute) switch value { case let date as Date: appInfo[attribute] = ISO8601DateFormatter().string(from: date) default: appInfo[attribute] = value } } } } seal.fulfill(appInfo) } DispatchQueue.main.async { query.start() } } }
7
0
296
Oct ’24
Are uninstall routines ever run for normal macOS app removal?
If you "uninstall" an app from your Mac using the standard "uninstall" methodologies, does macOS ever run an uninstaller in the background to clean up files and/or folders outside the app's .app folder? The standard "uninstall" methodologies that I know about are: trashing an application's .app folder, then emptying the trash long-clicking on an app in Launchpad, then clicking on its X while it jiggles Are there any other standard methodologies to "uninstall" a macOS app? Does any of the methodologies run any uninstall routine other than just deleting the .app folder from the file system (like maybe removing a folder under ~/Library/Caches)? Are there any problems with running rm -rf <path-to-.app-folder> instead of trashing the app folder/Lauchpad jiggling to death the app? Are there any differences between uninstall routines for apps installed from the Mac App Store versus apps installed by other means?
3
0
201
Oct ’24
Modifying AAC/M4A audio file metadata with Shortcuts on macOS
I want to use the Shortcuts app on macOS Monterey to modify M4A/AAC audio file metadata for files that I have ripped from CD to my local Music app library. How can I use a regular expression on track titles to replace text that matches a certain regex pattern? I have already written the regex, I just need help with the Shortcuts app. I've started my shortcut with a "Find Music" action connected to a "Repeat with Each" loop action. Within the loop, I use a "Get Details of Music" action to get the title, which is passed to a "Replace Text" action using my regex. I just don't know how to write the correct new title into the music file's title/name metadata field. I tried a "Rename File" action with type "iTunes media" to try to set the "Title" field, but the field wasn't updated either in the Music app or in the M4A/AAC file itself. I couldn't find any other actions that seemed like they might be able to modify the field. I'd prefer a solution that goes through the Music app / its APIs, rather than directly modifying the M4A on the file system, because the latter would force me to have the Music app scan for changed files after the files we're modified.
0
0
292
Sep ’24