8

How can I use keychain scripting to write a script in Applescript that retrieves the login and password of a website from the login keychain, given the website's URL?

2
  • 1
    On an unrelated note, if you want this to have all your passwords organized and be able to automatically fill forms with user/pass/data/creditcards/Etc. in a secure way, I'd suggest you try 1password: (i don't work for them) agilewebsolutions.com/onepassword Commented Mar 3, 2011 at 17:01
  • @Martin: I am a happy 1Password customer. ;) Commented Mar 3, 2011 at 17:04

3 Answers 3

7

If you know the exact name of the keychain item, you could use the following:

tell application "Keychain Scripting" to tell keychain "login.keychain" to get {account, password} of (first Internet key whose name is "www.google.com")

Thing is, Keychain Scripting is slow and quite buggy. For example, searching for a specific keychain item in the example above using name contains instead of name is does not work. You would have to use a repeat statement similar to what @Philip posted:

tell application "Keychain Scripting" to tell keychain "login.keychain"

    repeat with x from 1 to (count every Internet key)
        if name of Internet key x contains "Google" then
            return {account, password} of Internet key x
        end if

    end repeat
end tell

If you´re okay to use the command line and just want to look up stuff, I´d rather use: security find-internet-password -g -s www.google.com and then grep what you want.

2
  • +1 for the command line. Those are typically much more streamlined and optimized ways of doing things. It can even be included in an Applescript with do shell script Commented Mar 3, 2011 at 20:34
  • 2
    For the security command, there is now a -w option to print only the password (no need to grep.) security find-internet-password -w -s www.google.com or, e.g., for manually added keychain entries, security find-generic-password -w -s bikelockcombo Commented Aug 4, 2018 at 21:30
3

Keychain scripting is pretty well broken in Lion, so the security command-line tool is your best bet. Alternately, use Red Sweater's scripting addition, which is faster and easier to script for than the old Keychain Access scripts.

Red Sweater Blog: Usable Keychain Scripting for Lion

2

Keychain is exposed to Applescript via the Keychain Scripting application. There are numerous examples on the web, this being the most basic usage:

set theShortUserName to do shell script "/usr/bin/whoami" --get the short
userid. This is how your default keychain is labled.

tell application "Keychain Scripting"
    set myKeyChain to keychain theShortUserName
    set theKeyList to every Internet key of myKeyChain --email keys are
normally Internet Keys
    repeat with x from 1 to (length of theKeyList)
        set theKey to item x of theKeyList
        if the name of theKey is "name of key here" then
            set thePassword to password of theKey --grab the password
            set theUserID to the account of theKey  --grab the userid
        end if
    end repeat
end tell

From MacScripter

3
  • If I try to run this from the AppleScript Editor, I get the error: error "Keychain Scripting got an error: Can’t get keychain \"lorin\"." number -1728 from keychain "lorin" Commented Mar 3, 2011 at 17:37
  • -1728 can be a lot of things, but it typically comes up when a string has a character it doesn't understand (like a character that should be escaped), but it could also simply be the key chain "lorin" doesn't exist and another value is required (I don't have a keychain on my Mac that has my user name). I only offered the above as an example, but it is dated from 2002, so you're going to have to do some tweaking on your end to make it work in something up-to-date and particular to your needs. Commented Mar 3, 2011 at 18:40
  • 1
    Indeed, a users keychain is called login.keychain and not username.keychain (unless changed).
    – Asmus
    Commented Mar 3, 2011 at 20:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .