### Install and Manage Finder Extension via Command Line (Bash) Source: https://context7.com/suolapeikko/finderutilities/llms.txt These bash commands demonstrate how to install, remove, and list Finder Sync extensions using the `pluginkit` tool. This is useful for programmatic management of the extension. ```bash # Install and approve the extension pluginkit -a FinderUtilities.app/Contents/PlugIns/RightClickExtension.appex/ # Remove the extension pluginkit -r FinderUtilities.app/Contents/PlugIns/RightClickExtension.appex/ # List all installed Finder Sync extensions pluginkit -m -v -i com.apple.FinderSync # Check if extension is running ps aux | grep RightClickExtension ``` -------------------------------- ### Install and Remove Finder Extension via Pluginkit Source: https://github.com/suolapeikko/finderutilities/blob/master/README.md Use the pluginkit command-line utility to register or unregister the FinderUtilities extension bundle. This is an alternative to the standard GUI installation method. ```bash pluginkit -a FinderUtilities.app/Contents/PlugIns/RightClickExtension.appex/ ``` ```bash pluginkit -r FinderUtilities.app/Contents/PlugIns/RightClickExtension.appex/ ``` -------------------------------- ### AppDelegate - Register Finder Extension Source: https://context7.com/suolapeikko/finderutilities/llms.txt The AppDelegate class handles the initial registration and setup of the Finder extension. It checks if the extension is enabled in System Preferences and prompts the user if necessary. After setup, the application terminates as the extension runs independently. ```swift import Cocoa import FinderSync @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { @IBOutlet weak var window: NSWindow! func applicationDidFinishLaunching(_ aNotification: Notification) { // Show extensions, if FinderUtilities is not approved if !FIFinderSyncController.isExtensionEnabled { FIFinderSyncController.showExtensionManagementInterface() } // Terminate the application, as it is not needed anymore NSApplication.shared.terminate(self) } func applicationWillTerminate(_ aNotification: Notification) { // Insert code here to tear down your application } } ``` -------------------------------- ### Finder Extension Entitlements Configuration (XML) Source: https://context7.com/suolapeikko/finderutilities/llms.txt This XML configuration defines the necessary sandbox entitlements for the Finder extension. It grants read-write access to user-selected files and temporary exceptions for home-relative paths. ```xml com.apple.security.app-sandbox com.apple.security.files.user-selected.read-write com.apple.security.temporary-exception.files.home-relative-path.read-write / ``` -------------------------------- ### Copy Selected File/Folder Paths to Clipboard (Swift) Source: https://context7.com/suolapeikko/finderutilities/llms.txt This Swift function copies the full paths of all selected files and folders in Finder to the system clipboard. Each path is placed on a new line. It utilizes the NSPasteboard for clipboard operations and requires access to the FinderSyncController. ```swift /// Copies the selected file and/or directory paths to pasteboard @IBAction func copyPathToClipboard(_ sender: AnyObject?) { guard let target = FIFinderSyncController.default().selectedItemURLs() else { NSLog("Failed to obtain targeted URLs: %@") return } let pasteboard = NSPasteboard.general pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: nil) var result = "" // Loop through all selected paths for path in target { result.append(contentsOf: path.relativePath) result.append("\n") } result.removeLast() // Remove trailing \n pasteboard.setString(result, forType: NSPasteboard.PasteboardType.string) } ``` -------------------------------- ### FinderSync - Core Extension Logic Source: https://context7.com/suolapeikko/finderutilities/llms.txt The FinderSync class is the core component of the extension, inheriting from FIFinderSync. It monitors the entire file system and defines the context menu items that appear when a user right-clicks in Finder. These include options for opening Terminal, creating files, and copying paths. ```swift import Cocoa import FinderSync class FinderSync: FIFinderSync { override init() { super.init() NSLog("FinderSync() launched from %@", Bundle.main.bundlePath as NSString) // Set up the directory we are syncing - monitor entire filesystem FIFinderSyncController.default().directoryURLs = [URL(fileURLWithPath: "/")] } override func menu(for menuKind: FIMenuKind) -> NSMenu { // Produce a menu for the extension (to be shown when right clicking a folder in Finder) let menu = NSMenu(title: "") menu.addItem(withTitle: "Open Terminal", action: #selector(openTerminalClicked(_:)), keyEquivalent: "") menu.addItem(withTitle: "Create empty.txt", action: #selector(createEmptyFileClicked(_:)), keyEquivalent: "") menu.addItem(withTitle: "Copy selected paths", action: #selector(copyPathToClipboard), keyEquivalent: "") return menu } } ``` -------------------------------- ### Create Empty File in Finder (Swift) Source: https://context7.com/suolapeikko/finderutilities/llms.txt This Swift function creates an empty text file named 'newfile.txt' in the currently targeted Finder directory. If a file with the same name exists, it appends a counter to the filename to prevent overwriting. It requires access to the FinderSyncController and FileManager. ```swift /// Creates an empty file with name "untitled" under the user-chosen Finder folder. /// If file already exists, append it with a counter. @IBAction func createEmptyFileClicked(_ sender: AnyObject?) { guard let target = FIFinderSyncController.default().targetedURL() else { NSLog("Failed to obtain targeted URL: %@") return } var originalPath = target let originalFilename = "newfile" var filename = "newfile.txt" let fileType = ".txt" var counter = 1 // Auto-increment filename if file already exists while FileManager.default.fileExists(atPath: originalPath.appendingPathComponent(filename).path) { filename = "\(originalFilename)\(counter)\(fileType)" counter+=1 originalPath = target } do { try "".write(to: target.appendingPathComponent(filename), atomically: true, encoding: String.Encoding.utf8) } catch let error as NSError { NSLog("Failed to create file: %@", error.description as NSString) } } ``` -------------------------------- ### openTerminalClicked - Launch Terminal Action Source: https://context7.com/suolapeikko/finderutilities/llms.txt This action, triggered by the 'Open Terminal' context menu item, launches the macOS Terminal.app. It opens a new Terminal window with the working directory set to the folder currently selected in Finder, using the system's 'open' command. ```swift /// Opens a macOS Terminal.app window in the user-chosen folder @IBAction func openTerminalClicked(_ sender: AnyObject?) { guard let target = FIFinderSyncController.default().targetedURL() else { NSLog("Failed to obtain targeted URL: %@") return } let task = Process() task.executableURL = URL(fileURLWithPath: "/usr/bin/open") task.arguments = ["-a", "terminal", "\(target)"] do { try task.run() } catch let error as NSError { NSLog("Failed to open Terminal.app: %@", error.description as NSString) } } // Usage: Right-click on any folder in Finder > Select "Open Terminal" // Result: Terminal.app opens with working directory set to the selected folder // Example: Right-click on ~/Documents > "Open Terminal" // Terminal opens with prompt: user@mac Documents % ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.