Last active
August 3, 2021 16:23
-
-
Save kolpanic/b1b73c123c617b6d1cd013de76c1224a to your computer and use it in GitHub Desktop.
For use in a macOS app to launch 1Password and display all login items for a particular domain. Use onePasswordAvailable in Cocoa Bindings to enable or hide a UI element. Use onePasswordIcon as the image for the UI element.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// set the domain in the search URL appropriately for your site | |
private let onePasswordSearchURL = URL(string: "onepassword://search/EXAMPLE.COM", relativeTo: nil)! | |
// bind this to the NSButton's Hidden value, with a Value Transformer of NSNegateBoolean | |
@objc dynamic var onePasswordAvailable : Bool { | |
return (NSWorkspace.shared.urlForApplication(toOpen: onePasswordSearchURL) != nil) | |
} | |
// bind this to the NSButton's Image value | |
@objc dynamic var onePasswordIcon : NSImage? { | |
guard let applicationURL = NSWorkspace.shared().urlForApplication(toOpen: onePasswordSearchURL) else { return nil } | |
return NSWorkspace.shared.icon(forFile: applicationURL.path) | |
} | |
// bind the NSButton's action to this function | |
@IBAction func launchOnePasswordSearch(_ sender:AnyObject? = nil) { | |
NSWorkspace.shared.open(onePasswordSearchURL) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've confirmed with AgileBits that this is the best approach currently possible.