Export keychain passwords to text file?

Discussion in 'Mac OS X Lion (10.7)' started by blueroom, Apr 16, 2012.

  1. blueroom macrumors 603

    blueroom

    Joined:
    Feb 15, 2009
    Location:
    Toronto, Canada
    #1
    I've done it before using a script on SL, can't recall how or even the script name.

    Is there an Applescript or terminal utility?
     
  2. blueroom thread starter macrumors 603

    blueroom

    Joined:
    Feb 15, 2009
    Location:
    Toronto, Canada
    #3
    That didn't seem to work. Maybe too old. I found this but not sure how to run it?
    Code:
    <pre style="
    font-family: Monaco, 'Courier New', Courier, monospace;
    font-size: 10px;
    font-weight: normal;
    margin: 0px;
    padding: 5px;
    border: 1px solid #000000;
    width: 720px; height: 340px; 
    color: #000000;
    background-color: #FFCE75;
    overflow: auto;"
    title="this text can be pasted into the AppleScript Editor">
    -- write keychain information to a text file
    
    
    property spacer : tab & tab -- some dividing text
    property theKeychain : "login.keychain" -- the name of the keychain
    
    
    on run
    -- set theKeychain to text returned of (display dialog "Keychain to use?" default answer theKeychain)
    getKeyInfo for theKeychain -- use handler(s) to avoid persistant global variables!
    end run
    
    
    to getKeyInfo for theKeychain
    (*
    get a list of names, accounts, and passwords for keys in the specified keychain
    parameters - theKeychain [text]: the name of the keychain to use
    returns nothing
    *)
    try
    tell application id "com.apple.KeychainScripting" -- get key info (same as application "Keychain Scripting")
    set current keychain to first item of (get keychains whose name is theKeychain)
    unlock current keychain
    end tell
    on error errorMessage number errorNumber -- error getting the keychain
    tell application id "com.apple.KeychainScripting" to quit
    tell me to activate
    display alert "There was an error in getting keychain " & quoted form of theKeychain message "(" & errorNumber & ") - " & errorMessage
    error number -128 -- cancel
    end try
    
    tell (current date) as «class isot» as string to set theDate to text 1 thru 10 & space & text 12 thru -1
    set keyInfo to {return & "Keychain information from " & (quoted form of theKeychain) & space & theDate & " - " & return}
    set {errorCount, thePrompt} to {0, ""}
    
    tell application id "com.apple.KeychainScripting"
    set the end of keyInfo to return & "generic keys: " & return
    repeat with aKey in (get every generic key of current keychain)
    try
    tell aKey to set the end of keyInfo to (name & spacer & account & spacer & password & return)
    on error errmess
    log errmess
    set errorCount to errorCount + 1
    end try
    end repeat
    
    set the end of keyInfo to return & "Internet keys: " & return
    repeat with aKey in (get every Internet key of current keychain)
    try
    tell aKey to set the end of keyInfo to (name & spacer & account & spacer & password & return)
    on error errmess
    log errmess
    set errorCount to errorCount + 1
    end try
    end repeat
    
    set the end of keyInfo to return & "AppleShare keys: " & return
    repeat with aKey in (get every AppleShare key of current keychain)
    try
    tell aKey to set the end of keyInfo to (name & spacer & account & spacer & password & return)
    on error errmess
    log errmess
    set errorCount to errorCount + 1
    end try
    end repeat
    quit
    end tell
    
    if errorCount is not 0 then
    if errorCount is 1 then
    set thePrompt to "One key was skipped."
    else
    set thePrompt to "There were " & errorCount & " keys skipped."
    end if
    tell me to activate
    display alert "There was an error while getting passwords from " & quoted form of theKeychain message thePrompt buttons {"Cancel", "OK"} cancel button "Cancel"
    end if
    set thePrompt to thePrompt & "* Save read keychain passwords to:"
    
    set theLocation to (choose file name with prompt thePrompt default name "Passwords.text" default location (path to desktop))
    writeResults for (return & (keyInfo as text)) into theLocation
    end getKeyInfo
    
    
    to writeResults for whatever into aTextFile
    (*
    write some results to a text file
    parameters - whatever [text]: something to write
    aTextFile [various]: the text file to write to (POSIX or Finder)
    returns [boolean]: true if successful, false if not
    *)
    set aTextFile to aTextFile as text
    if aTextFile begins with "/" then set aTextFile to aTextFile as POSIX file as text -- POSIX
    try
    set theOpenFile to (open for access file aTextFile with write permission)
    write (whatever as text) to theOpenFile starting at eof -- append
    close access theOpenFile
    return true -- success
    on error errmess
    log errmess
    try
    close access theOpenFile
    end try
    end try
    return false
    end writeResults</pre>
     
  3. Partron22 macrumors 68000

    Partron22

    Joined:
    Apr 13, 2011
    Location:
    Yes
    #4
    Apple's "Keychain Scripting" application disappeared in the transition from Snow Leopard to Lion, so there is no file named "com.apple.KeychainScripting". You need to work around that.
    Here's a start: Scripting Keychain Access in Lion
     
  4. blueroom thread starter macrumors 603

    blueroom

    Joined:
    Feb 15, 2009
    Location:
    Toronto, Canada
    #5
    Thanks but gaak, that's seems like a bear to implement.
     
  5. blueroom thread starter macrumors 603

    blueroom

    Joined:
    Feb 15, 2009
    Location:
    Toronto, Canada
  6. jblagden macrumors 65816

    jblagden

    Joined:
    Aug 16, 2013
    #7

Share This Page