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

Sorry for the late response!

 

Interesting the UI has changed in Mojave. A better way to do it in this instance, that will work in Mojave and earlier, is to say "last group" rather than "group X". I've changed the workflow to do this.

 

Next, I have modified the workflow (same download) to have two keywords:

 

pw = automatically copy first matching password (as it was before)

kc = search and display all matches (I often use this)

 

On 1/21/2019 at 6:38 PM, rapatt said:

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.

I do not know how to filter search from the search field.

 

One thing that helps is if you set your default column sorts in Keychain before you use this workflow.

 

- click column header "Kind" (arrow points down = descending)

 

this gives me a list with web form passwords at the top, a good match for use with "pw" keyword

 

Another alternative is to create a new set of keychain entries for commonly searched for passwords with unique/shortcut names.

 

Let me know how you get on!

 

 

Edited by gingerbeardman

Share this post


Link to post

@gingerbeardman Thanks for this great workflow, this is exactly what I needed!
Are there any new developments since?

I was looking at your script and have a suggestion which might be less prone to changes in the UI.

You could replace the search/input part:

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"


To something like this, using the menu bar instead:

click menu item "Find" of first menu of menu bar item "Edit" of first menu bar
keystroke q

 

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...