Ask Different is a question and answer site for power users of Apple hardware and software. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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) – Martín Marconcini Mar 3 '11 at 17:01
@Martin: I am a happy 1Password customer. ;) – Lorin Hochstein Mar 3 '11 at 17:04
up vote 5 down vote accepted

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 "")

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 and then grep what you want.

share|improve this answer
+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 – Philip Regan Mar 3 '11 at 20:34

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

share|improve this answer

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

share|improve this answer
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" – Lorin Hochstein Mar 3 '11 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. – Philip Regan Mar 3 '11 at 18:40
Indeed, a users keychain is called login.keychain and not username.keychain (unless changed). – Asmus Mar 3 '11 at 20:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.