Skip to content

Instantly share code, notes, and snippets.

@jonycgn
Last active April 13, 2023 20:00
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jonycgn/fa8bb9f1513af54baa95aecfa2e569eb to your computer and use it in GitHub Desktop.
Save jonycgn/fa8bb9f1513af54baa95aecfa2e569eb 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
@slassen
Copy link

slassen commented Aug 22, 2020

For MacOS 10.15.6 Catalina the @youniki solution worked. I only added a bit in between where it copies Account and Password to copy the Where as well.

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 "Keychain Access"
	activate
	delay 0.4
	tell application "System Events" to key code 48
	tell application "System Events" to keystroke "c" using command down
end tell

@pratyushchowdhary
Copy link

Nidwan's script worked for 10.14.6 macOS Mojave. Thanks

@pinksnow1970
Copy link

I've been trying to do this for days, struggling. I am not sure what is going wrong, I'm running 10.15.6.

The first script just pasted as someone else said, one password then it repeats.

The second script just copied the script into the password.csv.

I haven't don't any coding for years and then it wasn't on Mac. So please excuse my ignorance.

@slassen
Copy link

slassen commented Oct 5, 2020

@pinksnow1970 did you read my experience?

@slassen
Copy link

slassen commented Oct 5, 2020

Also I believe that you might need to increase the delay if your computer is slower.

@pinksnow1970
Copy link

Yes I did but by that point, I wasn’t sure which script was the one you changed.

@slassen
Copy link

slassen commented Oct 5, 2020

I worked off everything in youniki’s post.

@pinksnow1970
Copy link

I’ll have another look tomorrow. Maybe fresh eyes I might get it. Thanks

@eupheng
Copy link

eupheng commented Oct 8, 2020

@youniki's script worked pretty good. I did run into one issue as the script that I copied was pasted into TextEdit.
Luckily, I have a clipboard manager, Clipy, https://github.com/Clipy/Clipy , and I cleared my clipboard prior to running youniki's script.
I would recommend that using a clipboard manager to clear your copied script.

@Ant76623
Copy link

Hello everyone, I have tried @jonycgn method and it seems like it would work but I am on MacOS 10.15.7, and it just keeps copying the script to the text edit.

As for the keychain script I believe there was an update or a setting I am unaware of because in order to see ANY password I have to double click and then check the "show password" box, followed by a system password prompt to reveal. Which I don't think is reflected in the script.

I just bought Dashlane to save all my passwords from Safari to my new PC so I could use both but didn't realize I cannot just IMPORT MY DARN PASSWORDS from safari.

@lalallalalallala
Copy link

hello I tried the script and actually the various elements are copied.
I tried to import it with firefox but it doesn't seem to import anything. Could you tell me why? thanks for the help

Copy link

ghost commented Nov 19, 2020

Can someone update a final script that works with Big Sur 11.0.1 please?

@valeman71
Copy link

Can someone update a final script that works with Catalina 10.15.7 please?

@lalallalalallala
Copy link

maybe excist other method to export from Safari every password an then import them at Firefox?

@Johand-alt
Copy link

Johand-alt commented Dec 9, 2020

Hi everyone,
I just tried @aelsharawi version and this is the only one that works perfectly for me with Mac OS Big Sur 11.0.1 !
Copy paste the full code, even the text not identified as code in @aelsharawi post.

Follow the previous instructions by other contributors (create csv file, allow Script Editor, ...) Replace "your password" with your keychain password in the script. Also replace 1 by the number of passwords you have to export.

Run the script, wait and smile ! :)

https://gist.github.com/Johand-alt/5ba4996cf82c2992bab82b29f5db970b

@psustr
Copy link

psustr commented Dec 11, 2020

The scrip by @aelsharawi indeed works. However, the reason why so many people (myself included) are confused is that the original instructions were about opening the Password dialog in Safari. However, @aelsharawi's script is designed to export the passwords from Keychain Access (same thing, this is where Safari actually stores its passwords).

If you open the Password dialog in Safari, the scrip will find exactly nothing, and it will merely keep pasting the current clipboard contents - in most cases it is the script itself or whatever else may be in your current clipboard.

In order for the script to work, you must open Keychain Access, choose iCloud or wherever you store your passwords by default, and select the first password. The rest of the old instructions are still applicable here.

@dgrospelier @pinksnow1970

@The-Archives
Copy link

The-Archives commented Dec 30, 2020

Hello. Thanks for this.
My only additional pointer is to SORT your Keychain items so that it shows website passwords from the top of the list. Then, like @youniki mentions, shift-select all applicable entries to see the count then paste this into the number of repetitions (REPEATNO). You don't want it taking application/other information, as I've noticed some have encrypted/jumbled lines of text which stretch tens of lines.

If you're using LastPass, here's a guaranteed slightly tweaked script working on MacOS 10.16.6. The only difference to the above scripts is:

  • added comments to divide each section for visibility, in case you want to tweak and tailor the script for your own purposes
  • changed it so it reads and pastes in the order: url, user,password, then name (LastPass formatting) (don't know if this is important)

You still need to manually set REPEATNO and YOURPASSWORD. Just CTRL+F for these in the script.
Copy everything, including the bits that aren't detected as code

tell application "TextEdit"
activate
tell application "System Events" to keystroke "url,username,password,name"
tell application "System Events" to keystroke return
end tell
repeat REPEATNO times
--URL
tell application "Keychain Access"
activate
tell application "System Events" to keystroke "i" using command down
delay 0.4
tell application "System Events" to key code 48
delay 0.1
tell application "System Events" to key code 48
delay 0.1
tell application "System Events" to key code 48
delay 0.1
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
--USERNAME
tell application "Keychain Access"
activate
delay 0.4
tell application "System Events" to key code 48 using shift down
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
--PASSWORD
tell application "Keychain Access"
activate
delay 0.4
tell application "System Events" to keystroke "c" using {shift down, command down}
delay 0.4
tell application "System Events" to keystroke "YOURPASSWORD"
tell application "System Events" to keystroke return
delay 0.4
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
--NAME
tell application "Keychain Access"
activate
delay 0.4
tell application "System Events" to key code 48 using shift down
tell application "System Events" to key code 48 using shift down
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 return
	delay 0.4
end tell

tell application "Keychain Access"
	activate
	delay 0.4
	tell application "System Events" to keystroke "w" using command down
end tell

tell application "Keychain Access"
	activate
	delay 0.4
	tell application "System Events" to key code 125
end tell

end repeat

@macpwd
Copy link

macpwd commented Dec 30, 2020

For those that could not use the previously posted scripts to Export passwords from Safari, because the script was copying only passwords, or only usernames and passwords without the site name - below is the version that worked for me in Safari 14.0.1 on MacOS Mojave 10.14.6.
Instead of opening a pop-up that seems to be changing UI from time to time, the script utilizes three available context menu actions: Copy Website, Copy User Name, Copy Password. Basically that's all the information you have in the Safari password storage, so script takes that and adds it to open csv file in TextEdit in csv format. You may then copy info from there using excel and paste into a template
csv file of your choice (1password, dashlane, lastpass etc.) enriching with additional data as needed.

Again, pre-requisites are:

  • Open and unlocked Safari password list with active password record selected (export script will process them one by one moving down the list)
  • Open Textedit with empty file in plain edit mode and utf-8 format
  • Open Apple Script Editor that has been given required permissions in system settings (Accessibility)

Execution:

  • Run script once (it has one repetition be default) - validate the results, should get something like:
    "website.com","user_name","pa$$word"
  • If it looks good so far - change 1 to 10 in the first line, run the script, validate the results
  • If your TextEdit file still looks good - just change it to 100 and run in chunks, controlling the results, until you get all your passwords exported
    A reminder: resulting file that you get is just a plain-text list of all your accounts and passwords. Use it immediately to import the data into the password manager of your choice, and then erase completely(!) from your system. If you are not ready to run the import yet - just delete it (from trash too!) now, and once you ready to run the import - run the script again to extract passwords from Safari.
repeat 1 times
	tell application "Safari" --open Safari window
		activate
		delay 0.4
		tell application "System Events" --invoke context menu on active element
			tell process "Safari"
				set _selection to value of attribute "AXFocusedUIElement"
				tell _selection to perform action "AXShowMenu"
			end tell
			keystroke "Copy Website" --select righ-click menu item in the list
			keystroke return --'click' on selected menu item
		end tell
	end tell
	tell application "TextEdit" --open TextEdit and paste the quoted result and a comma
		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 "\",\""
	end tell
	tell application "Safari"
		activate
		delay 0.4
		tell application "System Events"
			tell process "Safari"
				set _selection to value of attribute "AXFocusedUIElement"
				tell _selection to perform action "AXShowMenu"
			end tell
			keystroke "Copy User Name"
			keystroke return
		end tell
	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"
			tell process "Safari"
				set _selection to value of attribute "AXFocusedUIElement"
				tell _selection to perform action "AXShowMenu"
			end tell
			keystroke "Copy Password"
			keystroke return
		end tell
		tell application "System Events" to key code 125 --move to the next item in Safari list
	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 --move to the new line in textedit file
	end tell
end repeat

@deBallerMann
Copy link

deBallerMann commented Jan 1, 2021

Hey guys,
I my case, its just coyping the execution code 5 times inside the csv.. Hope there is anyone who can help me out. I have the newest version of macOS

@The-Archives
Copy link

The-Archives commented Jan 1, 2021

Hey guys,
I my case, its just coyping the execution code 5 times inside the csv.. Hope there is anyone who can help me out. I have the newest version of macOS

In my opinion, you should just try to figure out what exactly the script is erring at and tweak it yourself. Luckily this coding isn't too opaque, so laymen can still figure it out with a bit of time. For example, if it's copying the code 5 times is it not successfully tabbing to Keychain? Or is it not successfully copying once the information window is open?

"keystroke "i" using command down" is another way of doing Right click -> Get Info, which brings up the name/user
"key code 48" is TAB.
"keystroke "c" using {shift down, command down}" niftily directly copies your password once the Get Info screen is up, with the requirement for you to enter your login password.
(look up key codes here)

I'd recommend you manually do the process once, vocally saying out the different keyboard commands you're doing, and seeing whether your manual process is the same as what the script is trying to automate.

@seb-jimenez
Copy link

seb-jimenez commented Jan 20, 2021

I want to thank you @macpwd, your script works perfectly.
For those who have issues with it, I figured the script doesn't work properly if you have passwords with missing username or password.
Try to clean your list first (who needs passwords without password) or sort them so that the lines without password or username comes at the end. Then run the script as explained and it should be ok.

I used it for Bitwarden.
You will need to remove all the quotation marks from your csv (using "find and replace", make sure you don't have passwords using them), your output will then become like this
website,username,password
In order to import it to Bitwarden you have to add the following line on top of the other ones
login_uri,login_username,login_password
Then paste the whole csv text into Bitwarden import tool selecting "Bitwarden (csv)" as the format of imported file.
You will miss the "name" field for all of your passwords indeed, but you can fix this if you know some Excel tricks and add "name" in the first line of your csv.

@amitjethani
Copy link

@macpwd's script worked well for me on Big Sur 11.2.2 but I had to add a few more delays to slow the whole process down for it to work reliably. Notably, delays were necessary for the Safari Passwords panel so my modifications looked like this,

		tell application "System Events" --invoke context menu on active element
			tell process "Safari"
				set _selection to value of attribute "AXFocusedUIElement"
				tell _selection to perform action "AXShowMenu"
			end tell
			keystroke "Copy User Name" --select righ-click menu item in the list
			delay 0.4
			keystroke return --'click' on selected menu item
			delay 0.4
		end tell

@TalentDepotAdmin
Copy link

March 10, 2021

Bravo and Kudos to everyone that commented on the original code and modified the original (or other's) code.
I know zero about scripting. After going down the list of scripts, I finally got one that worked.

The @The-Archives script worked perfectly for me. No changes were needed.
Again, thanks to all!

macOS Big Sur
version 11.2.2

MacBook Pro (Retina, 15-inch, Mid 2014)
2.8 GHz Quad-Core Intel Core i7
16 GB 1600 MHz DDR3

@fkoliveira
Copy link

The script worked well for me thanks so much! :)
1password asked me 4 columns on the import file layout so I modified the script to add that column.
Also made small changes for those with non EN-US keyboard layout.
thanks for sharing!

repeat 1 times
	tell application "Safari" --open Safari window
		activate
		delay 0.4
		tell application "System Events" --invoke context menu on active element
			tell process "Safari"
				set _selection to value of attribute "AXFocusedUIElement"
				tell _selection to perform action "AXShowMenu"
			end tell
			keystroke "Copy Website" --select righ-click menu item in the list
			keystroke return --'click' on selected menu item
		end tell
	end tell
	tell application "TextEdit" --open TextEdit and paste the quoted result and a comma
		activate
		delay 0.4
		tell application "System Events" to keystroke "\""
		tell application "System Events" to keystroke space
		tell application "System Events" to keystroke "v" using command down -- title
		tell application "System Events" to keystroke "\",\""
		tell application "System Events" to keystroke space -- for non EN-US keyboards
		tell application "System Events" to keystroke "v" using command down -- website
		tell application "System Events" to keystroke "\",\""
		tell application "System Events" to keystroke space -- for non EN-US keyboards
	end tell
	tell application "Safari"
		activate
		delay 0.4
		tell application "System Events"
			tell process "Safari"
				set _selection to value of attribute "AXFocusedUIElement"
				tell _selection to perform action "AXShowMenu"
			end tell
			keystroke "Copy User Name"
			delay 0.4
			keystroke return
			delay 0.4
		end tell
	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 space -- for non EN-US keyboards
	end tell
	tell application "Safari"
		activate
		delay 0.4
		tell application "System Events"
			tell process "Safari"
				set _selection to value of attribute "AXFocusedUIElement"
				tell _selection to perform action "AXShowMenu"
			end tell
			keystroke "Copy Password"
			keystroke return
		end tell
		tell application "System Events" to key code 125 --move to the next item in Safari list
	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 --move to the new line in textedit file
	end tell
end repeat

@T3CH1035
Copy link

Super noob, so please don't be too hard on me.... the script is working great, but I have 1 problem. I have a couple re-used passwords (whole reason I want to get everything into a CSV, to organize and update). A popup appears when this is the case, see below or attached. Any ideas??

Screen Shot 2021-04-30 at 3 44 40 PM

@amitjethani
Copy link

@T3CH1035 If you only have a few of these passwords with warnings, I recommend copying them and moving them over manually. Then delete the entries in your keychain and run the script. It is manual work, but presumably just a one-time thing.

@T3CH1035
Copy link

@T3CH1035 If you only have a few of these passwords with warnings, I recommend copying them and moving them over manually. Then delete the entries in your keychain and run the script. It is manual work, but presumably just a one-time thing.

I was hoping to avoid that. I brought in all of my passwords from Lastpass, IE, Chrome, and Edge into keychain. Therefore, there may be many. If this is my only option, I will go through them manually. Thanks!!

@grossmusic1
Copy link

A year later & this Mojave fix WORKED! Thank you so much, macpwd.
https://gist.github.com/jonycgn/fa8bb9f1513af54baa95aecfa2e569eb#gistcomment-3577452

@grantjanssen
Copy link

this was so helpful in my password migration from Mojave -> Monteray - but none of the scripts here were exactly right for me.
on my Monteray system I exported some passwords so I could get the csv format correct
EXAMPLE:

grant@grants-EFILM-MacBookPro-M1:~/Downloads[20220205-9:12][#391585]% gpg -d 202202050904.grants-EFILM-MacBookPro-M1.passwords.csv.gpg | grep -e Title -e 10.1.132.166
gpg: encrypted with 4096-bit RSA key, ID D30BB0166A5F0F2F, created 2013-09-07
      "Grant Janssen (gmail) <grant.janssen@gmail.com>"
Title,Url,Username,Password,OTPAuth
10.1.132.166 (manage),http://10.1.132.166/,manage,!manage,
grant@grants-EFILM-MacBookPro-M1:~/Downloads[20220205-9:13][#392585]%

so I needed the parentheses added, and the username pasted in two places
I tweeked the script a bit:

tell application "TextEdit"
	activate
	tell application "System Events" to keystroke "Title,Url,Username,Password,OTPAuth"
	tell application "System Events" to keystroke return
end tell
repeat 16 times
	tell application "Safari" --open Safari window
		activate
		delay 0.8
		tell application "System Events" --invoke context menu on active element
			tell process "Safari"
				set _selection to value of attribute "AXFocusedUIElement"
				tell _selection to perform action "AXShowMenu"
			end tell
			keystroke "Copy Website" --select right-click menu item in the list
			keystroke return --'click' on selected menu item
		end tell
	end tell
	tell application "TextEdit" --open TextEdit and paste the result and a comma
		activate
		delay 0.8
		tell application "System Events" to keystroke "v" using command down -- title
	end tell
	tell application "Safari"
		activate
		delay 0.8
		tell application "System Events"
			tell process "Safari"
				set _selection to value of attribute "AXFocusedUIElement"
				tell _selection to perform action "AXShowMenu"
			end tell
			keystroke "Copy User Name"
			delay 0.8
			keystroke return
			delay 0.8
		end tell
	end tell
	tell application "TextEdit"
		activate
		delay 0.8
		tell application "System Events" to keystroke space
		tell application "System Events" to keystroke "(" -- add before user
		tell application "System Events" to keystroke "v" using command down
		tell application "System Events" to keystroke ")," -- after user
	end tell
	
	tell application "Safari" --open Safari window
		activate
		delay 0.8
		tell application "System Events" --invoke context menu on active element
			tell process "Safari"
				set _selection to value of attribute "AXFocusedUIElement"
				tell _selection to perform action "AXShowMenu"
			end tell
			keystroke "Copy Website" --select right-click menu item in the list
			keystroke return --'click' on selected menu item
		end tell
	end tell
	
	tell application "TextEdit" -- copy website path
		activate
		delay 0.8
		tell application "System Events" to keystroke "http://" -- prepend the URL
		tell application "System Events" to keystroke "v" using command down
		tell application "System Events" to keystroke "/," -- URL
	end tell
	
	tell application "Safari"
		activate
		delay 0.8
		tell application "System Events"
			tell process "Safari"
				set _selection to value of attribute "AXFocusedUIElement"
				tell _selection to perform action "AXShowMenu"
			end tell
			keystroke "Copy User Name"
			delay 0.8
			keystroke return
			delay 0.8
		end tell
	end tell
	
	tell application "TextEdit" -- copy username
		activate
		delay 0.8
		tell application "System Events" to keystroke "v" using command down
		tell application "System Events" to keystroke "," -- URL
	end tell
	
	tell application "Safari"
		activate
		delay 0.8
		tell application "System Events"
			tell process "Safari"
				set _selection to value of attribute "AXFocusedUIElement"
				tell _selection to perform action "AXShowMenu"
			end tell
			keystroke "Copy Password"
			keystroke return
		end tell
		tell application "System Events" to key code 125 --move to the next item in Safari list
	end tell
	tell application "TextEdit"
		activate
		delay 0.8
		tell application "System Events" to keystroke "v" using command down
		tell application "System Events" to keystroke "," -- comma here last OTPAuth field is blank
		tell application "System Events" to keystroke return --move to the new line in textedit file
	end tell
end repeat

brought in 400+ passwords - ran perfectly

@xldan
Copy link

xldan commented May 26, 2022

my old mac no longer supports many app updates, so it was time to migrate.
jonycgn's OP was a big help to find a way to get Passwords etc out of safari.
Wanted to learn a bit about apple scripts and get more flexibility for code re-use.
Added option for Tab Separated Variables, Though not relevant for the passwords,
I've found it useful for moving databases where fields contained commas and/or quotes.

# This captures passwords stored in Safari (mine is 11.1.2) and writes them to a text file.
# Open Safari select show preferences/passwords (tick show & use login password); 
# make sure all other safari windows are closed.
# open TextEdit, set as text and save empty file as *.csv or *.tsv
# run this script after modifying to suit your needs
# save the text file to a safe place (consider encrypting)
# Based on github post: jonycgn / Export Safari passwords.md (thanks Johannes)


set delimcsv to ","        #  for file.csv
set delimtsv to "	" #  for file.tsv  - entered as "\t", converted to tab on script save
set sloth to 0.3           # to give time for GUI process to complete

#  unrem delimiter type to be used for output file
set delim to delimcsv
# set delim to delimtsv

#  makes data file less susceptible to discovery by key words
set txt1 to "forest"      # url
set txt2 to "wood"      # user name
set txt3 to "leaves"     # password
set fields to 3             # number of fields per record containing data
set txtlast to ""

# load with headers for first write
set txtrow to txt1 & delim & txt2 & delim & txt3 & return

repeat while txtrow is not txtlast
	tell application "TextEdit"
		activate
		delay sloth
		tell application "System Events" to keystroke txtrow
	end tell
	set txtlast to txtrow
	set txtrow to ""
	tell application "Safari"
		activate
		set n to fields
		repeat n times                                                      # once for each field to capture
			set the clipboard to ""
			tell application "System Events" to keystroke tab               # set focus on next field
			delay sloth
			tell application "System Events" to keystroke "c" using command down        # copy
			if n > 1 then
				set txtrow to txtrow & (the clipboard) & delim
			else
				set txtrow to txtrow & (the clipboard) & return
			end if
			set n to n - 1       #  loop terminates when n < 1
		end repeat
		
		# got the data, set focus for read on next row
		delay sloth
		tell application "System Events" to key code 53         # escape, to de-select text
		delay sloth
		tell application "System Events" to key code 125       # down arrow, focus to next row	
	end tell
end repeat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment