Jump to content
gthiruva

Can Alfred search the Mac Keychain

Recommended Posts

Anyone know if Alfred has a way to search the Mac Keychain Access app? Or, is this something I can build with the workflow tool?

 

Alfred doesn't support that natively. It may be achievable with a workflow but I would be careful with this.

Share this post


Link to post

The Alp library has some keychain integration, if I remember correctly. So if you're good with python, look into that one.

 

Slick! Thanks for the tip.

Share this post


Link to post

URL schemes are poor man's automation used by iOS developers. AppleScript dictionaries is where it's at!

 

Unfortunately, Keychain Access doesn't have one of those either :(

 

Realistically, I think your best bet is "generic" AppleScript automation to simulate the input:

 

tell application "Keychain Access"

    activate

    delay 0.5 -- wait for app to open/activate

    tell application "System Events"

        keystroke "f" using {command down, option down} -- ensure search bar is focussed

        keystroke "my search terms" -- enter search query

    end tell

end tell

 

Share this post


Link to post

AppleScript's "KeyChain Access" is pretty terrible.  However, the kind bloke(s) at Red-Sweater software (maker of FastScripts) have made a scripting addition aptly referred to as the Usable Keychain Scripting

 

It allows you to search using whose filters as with other enumerated AppleScript collections, e.g.

 

tell application "Usable Keychain Scripting" to tell ¬

        the current keychain to get the ¬

        password of every internet password ¬

        whose name contains "google.com"

Edited by CJK

Share this post


Link to post

Are such additions also usable via JXA?

 

It's a lot easier to generate Script Filter JSON from JS than from AppleScript, which is presumably what you'd want to do (i.e. show the results in Alfred).

 

If you're messing about with passwords, this pasteboard script might be helpful. It marks the pasteboard data as "concealed", which Alfred's own Copy to Clipboard doesn't do. That's important to stop clipboard managers remembering the password.

 

Share this post


Link to post
On 10/6/2018 at 7:46 PM, deanishe said:

Are such additions also usable via JXA?

 

Yes, e.g.:

 

var app = Application('Usable Keychain Scripting')

app.currentKeychain.keychainItems.whose({name: {_contains: 'CK'} })()

 

I don't have Mojave but I'm hearing lots and lots of reports that it's ballsed everything up for AppleScripters, particularly as it no longer permits the use of scripting additions (people wonder why I never use them, and this is why).  However, as the Usable Keychain Scripting is an application, it can be scripted just like any other scriptable application that one downloads.

Share this post


Link to post
On 10/6/2018 at 7:14 PM, CJK said:

AppleScript's "KeyChain Access" is pretty terrible.  However, the kind bloke(s) at Red-Sweater software (maker of FastScripts) have made a scripting addition aptly referred to as the Usable Keychain Scripting

 

It allows you to search using whose filters as with other enumerated AppleScript collections, e.g.

 

tell application "Usable Keychain Scripting" to tell ¬

        the current keychain to get the ¬

        password of every internet password ¬

        whose name contains "google.com"

This is very cool!

 

I have been unable to figure out the scripting to access "Web form password" (Keychain parlance) which is what all the passwords I want to search are stored as. Basically, website passwords.

 

Also, I recently discovered - likely somewhat belatedly - that I can copy the password of a keychain item by selecting its row and using the context menu. No need to open the item, unlock, select text, copy - which is what I have been doing for decades! Oops.

Share this post


Link to post
On 10/29/2018 at 12:10 PM, gingerbeardman said:

I have been unable to figure out the scripting to access "Web form password"

 

Web form passwords aren't part of the accessible keychain, either through Usable Keychain Scripting or regular Keychain Access apple scripting, or even through bash security program.  I believe those are stored in the iCloud keychain, and not the login keychain.  Such a pain, but possibly good from a security standpoint.

Share this post


Link to post

So I finally put together a workaround for this using UI scripting.

 

https://www.gingerbeardman.com/alfred/Search Keychain.alfredworkflow

 

  1. Accept keyword and argument
  2. Launch Keychain Access via Alfred Workflow
  3. AppleScript focus search box, enter argument, copy password prompt, quit Keychain Access
  4. (user enter password and confirm)

The core of it is the AppleScript:

on alfred_script(q)
	activate application "Keychain Access"
	tell application "System Events"
		tell process "Keychain Access"
			set value of text field 1 of group 2 of toolbar 1 of window "Keychain Access" to q
			click button 1 of text field 1 of group 2 of toolbar 1 of window "Keychain Access"
			click menu item "Copy Password to Clipboard" of menu 1 of menu bar item "Edit" of menu bar 1
		end tell
	end tell
	quit application "Keychain Access"
end alfred_script

 

Edited by gingerbeardman

Share this post


Link to post

Thanks gingerbeardman!!  I was able to tweak yours just a bit to get it working with Mojave.  Quick question.  Do you know of a way to pass in an account with a service/platform to filter results? It tends to be sorted alphabetically if I just pass in "google.com" for example.  Trying to filter out specific accounts.

Share this post


Link to post
On 1/21/2019 at 7:41 PM, gingerbeardman said:

So I finally put together a workaround for this using UI scripting.

 

https://www.gingerbeardman.com/alfred/Search Keychain.alfredworkflow

 

  1. Accept keyword and argument
  2. Launch Keychain Access via Alfred Workflow
  3. AppleScript focus search box, enter argument, copy password prompt, quit Keychain Access
  4. (user enter password and confirm)

The core of it is the AppleScript:


on alfred_script(q)
	activate application "Keychain Access"
	tell application "System Events"
		tell process "Keychain Access"
			set value of text field 1 of group 2 of toolbar 1 of window "Keychain Access" to q
			click button 1 of text field 1 of group 2 of toolbar 1 of window "Keychain Access"
			click menu item "Copy Password to Clipboard" of menu 1 of menu bar item "Edit" of menu bar 1
		end tell
	end tell
	quit application "Keychain Access"
end alfred_script

 

 

I have to modify "group 2" to "group 1" to make it work on Mojave

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×