Skip to content

Instantly share code, notes, and snippets.

@kolpanic
Last active August 3, 2021 16:23
Show Gist options
  • Save kolpanic/b1b73c123c617b6d1cd013de76c1224a to your computer and use it in GitHub Desktop.
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.
// 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)
}
@kolpanic
Copy link
Author

kolpanic commented Jan 16, 2017

I've confirmed with AgileBits that this is the best approach currently possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment