SwiftDefaultApps Documentation

repository·master·Indexed 23 days ago

https://github.com/lord-kamina/swiftdefaultapps

A macOS System Settings preference pane and swda CLI utility for managing default applications for various file types (UTIs) and protocols (URI Schemes). It provides a centralized interface for customizing macOS file associations and includes a 'Do Nothing' dummy app for registering handlers without performing actions.

Tokens
1.8K
Snippets
4
Records
7
Agent score
83%

What's inside SwiftDefaultApps

  1. Install SwiftDefaultApps via Homebrew

    master

    You can install the SwiftDefaultApps preference pane using Homebrew. After installation, use Spotlight to open SwiftDefaultApps.prefpane. It will appear at the bottom of your System Preferences icons.

    brew install swiftdefaultappsprefpane
  2. How to use the "Do Nothing" app

    master

    The Do Nothing dummy app is used to register a handler for a URI Scheme or UTI without actually performing an action. It prints a line to the console and quits. You must launch it manually before it can be selected in the preference pane. Run the following commands in your terminal to remove the quarantine flag and open the app:

    appDir="/Library/PreferencePanes/SwiftDefaultApps.prefPane/Contents/Resources/ThisAppDoesNothing.app"
    
    if ! [[ -d "$appDir" ]]; then appDir="$HOME/$appDir"; fi
    
    # Remove quanntine flag
    xattr -d com.apple.quarantine "$appDir"
    # Open the app
    open "$appDir"
  3. Install or Uninstall SwiftDefaultApps manually

    master
    To install manually, download the latest release from the GitHub releases page and double-click the .prefpane file. To uninstall, Ctrl+Click the preference pane icon in System Preferences and remove it, or move the .prefpane file to the Trash.
  4. Understand URI Scheme and UTI association options

    master

    SwiftDefaultApps allows you to manage default application associations for URI Schemes and file types (represented by UTIs). When selecting an item, you have these options:

    • Valid Applications: A list of applications for each LaunchServices role, generated by LaunchServices.
    • Do Nothing: Registers the item to be handled by a dummy application (ThisAppDoesNothing.app). This is useful for
  5. Use the swda CLI to manage default applications

    master

    The swda command-line utility allows you to retrieve and manipulate default applications on macOS. It is built using SwiftCLI and provides several subcommands for interacting with application schemes and Uniform Type Identifiers (UTIs).

    Available commands:

    • read: (via ReadCommand) Likely used to read configuration or current states.
    • get-apps: (via GetApps) Retrieves information about installed applications.
    • get-schemes: (via GetSchemes) Retrieves application schemes.
    • get-utis: (via GetUTIs) Retrieves Uniform Type Identifiers.
    • set: (via SetCommand) Used to manipulate or set default applications.
  6. Show the About dialog in SWDAMainPrefPane

    master

    The showAboutDialog(_:) action can be connected to an NSButton to display an informational alert containing the current version, build number, and a hyperlink to the project repository. It retrieves versioning information from the bundle identifier cl.fail.lordkamina.SwiftDefaultApps.

    @IBAction func showAboutDialog(_ sender: NSButton) {
        let mainBundle = Bundle(identifier: "cl.fail.lordkamina.SwiftDefaultApps")
        let appVersionString: String = mainBundle?.object(forInfoDictionaryKey:"CFBundleShortVersionString") as! String
        let buildNumberString: String = mainBundle?.object(forInfoDictionaryKey:"CFBundleVersion") as! String
    
        let alert = NSAlert()
        alert.window.title = "About"
        alert.messageText = "SwiftDefaultApps, v. \(appVersionString) build \(buildNumberString)"
        alert.informativeText = "by Gregorio Litenstein."
        alert.icon = ControllersRef.appIcon
        alert.accessoryView = HyperlinkTextField(frame: NSRect(x: 0, y:10, width:330, height:18), url: "http://www.github.com/Lord-Kamina/SwiftDefaultApps")
    
        alert.alertStyle = .informational
        alert.addButton(withTitle: "OK")
        alert.layout()
    
        DispatchQueue.main.async {
            alert.runModal()
        }
    }
  7. Implement the SWDAMainPrefPane class for System Settings

    master

    The SWDAMainPrefPane class is the primary entry point for integrating SwiftDefaultApps into macOS System Settings (formerly System Preferences). It inherits from NSPreferencePane and manages the lifecycle of the preference pane's UI, including view assignment, tab controller initialization, and content loading.

    Key lifecycle methods to understand:

    • assignMainView(): Used to populate the ControllersRef singleton with references to the main view and the tab view controller.
    • mainViewDidLoad(): Used to configure the layout constraints for the tabViewController and add its tabView as a subview of the main view.
    • didSelect(): Triggered when the pane is first opened; it initializes the content array for the default tab using ControllersRef.TabData.getContentArray.
    class SWDAMainPrefPane: NSPreferencePane {
        @IBOutlet weak var mainCustomView: NSView!
        @IBOutlet weak var tabViewController: SWDATabViewController?
    
        override func assignMainView() {
            ControllersRef.sharedInstance.tabViewController = self.tabViewController
            ControllersRef.sharedInstance.thePrefPane = self
            ControllersRef.sharedInstance.theMainView = self.mainCustomView
            super.assignMainView()
        }
    
        override func mainViewDidLoad() {
            super.mainViewDidLoad()
            self.tabViewController!.view.translatesAutoresizingMaskIntoConstraints = false
            ControllersRef.sharedInstance.theMainView.addSubview(self.tabViewController!.tabView)
        }
    
        override func didSelect() {
            ControllersRef.TabData.getContentArray(for: (ControllersRef.sharedInstance.tabViewController?.tabViewItems[0].view as! SWDATabTemplate), initialSetup: true)
        }
    }