Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vovandodev/769d48688973a425b58c85045738f296 to your computer and use it in GitHub Desktop.
Save vovandodev/769d48688973a425b58c85045738f296 to your computer and use it in GitHub Desktop.
Exporting Safari website passwords to a CSV file (1Password-compatible)

I'm a new 1Password user and recently tried to export all my passwords saved in iCloud Keychain to import them in 1Password. Unfortunately, none of the existing recipes worked for me, because moving the saved passwords from iCloud Keychain to a local keychain seems to be broken as of today (28 Aug 2018). So I came up with a different method.

This method works entirely using AppleScript, and only for website passwords (not for other kinds of logins such as file servers, etc.). Basically, the script toggles back and forth between Safari's Preferences window and TextEdit, copying each username and password combination via the clipboard.

Full instructions:

  1. If you have iCloud Keychain enabled, you might want to disable it temporarily, so that on the off chance that your passwords get corrupted, the changes won't propagate to the cloud.
  2. Make sure you have an up-to-date backup of your computer, including the keychain.
  3. Ensure that automatic backups are turned off, so that the plain text file we're going to create containing your passwords won't end up being backuped.
  4. Open Script Editor, and create a new file.
  5. Paste the contents attached below.
  6. Go to System Preferences -> Security & Privacy -> Privacy -> Accessibility, and make sure that Script Editor is allowed to control the computer.
  7. Open TextEdit and create a new file.
  8. Select Format -> Make Plain Text.
  9. Save the file as "Passwords.csv" (or any other name, the .csv extension is what is important).
  10. Make sure that file is the only open file in TextEdit.
  11. Open Safari, and make sure all its windows are closed.
  12. Open Safari -> Preferences, navigate to the Passwords tab, and enter your password to unlock the list.
  13. Select the first item in the list. This will be the first item imported to the text file, and the script will work its way down.
  14. Go back to Script Editor and press the play button.

This should import the first 5 passwords. When it's done, go to TextEdit and verify the contents. They should be in the format that 1Password expects for CSV files: name, URL, username, password. (Since there are no names for password items in Safari, this script just uses the URL as the name, so the first two fields will be identical.)

If you are confident the passwords got exported correctly, do the following to export the next batch of passwords:

  1. In TextEdit, ensure the caret in the text file is at the end of the file, on a line of its own.
  2. Go to Script Editor, adjust the command "repeat 5 times" to however many passwords you want to process at a time (say, 100, depending on how many you have). If you don't have that many passwords in Safari, it will keep exporting the last list entry, so no harm will be done except for a few duplicate lines in the text file, which you can delete manually after you're done. Be careful with selecting a very high number of repetitions though, because it's hard to stop the script once it's running.
  3. Save the script.
  4. Go to Safari, and make sure the passwords are still unlocked (Safari tends to auto-lock them after a few seconds), and that the next list item you want to export is selected. Be sure not to change the sort order of the list while going through this procedure, or you may end up with some passwords lost and some duplicate.
  5. Go back to Script Editor and press the play button.

This should export the next batch of passwords into the file. Repeat this as often as necessary.

After you've imported the file into 1Password, don't forget to turn backups back on, re-enable iCloud Keychain, and/or un-allow Script Editor to control your computer.

Script contents to paste into Script Editor:

repeat 5 times
  tell application "Safari"
    activate
    delay 0.4
    tell application "System Events" to keystroke "\t"
    tell application "System Events" to keystroke "c" using command down
  end tell
  tell application "TextEdit"
    activate
    delay 0.4
    tell application "System Events" to keystroke "\""
    tell application "System Events" to keystroke "v" using command down
    tell application "System Events" to keystroke "\",\""
    tell application "System Events" to keystroke "v" using command down
    tell application "System Events" to keystroke "\",\""
  end tell
  tell application "Safari"
    activate
    delay 0.4
    tell application "System Events" to keystroke "\t"
    tell application "System Events" to keystroke "c" using command down
  end tell
  tell application "TextEdit"
    activate
    delay 0.4
    tell application "System Events" to keystroke "v" using command down
    tell application "System Events" to keystroke "\",\""
  end tell
  tell application "Safari"
    activate
    delay 0.4
    tell application "System Events" to keystroke "\t"
    tell application "System Events" to keystroke "c" using command down
    tell application "System Events" to keystroke "\t"
    tell application "System Events" to key code 125
  end tell
  tell application "TextEdit"
    activate
    delay 0.4
    tell application "System Events" to keystroke "v" using command down
    tell application "System Events" to keystroke "\""
    tell application "System Events" to keystroke return
  end tell
end repeat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment