Skip to content

Instantly share code, notes, and snippets.

@joaocarmo
Last active February 25, 2021 00:44
Show Gist options
  • Save joaocarmo/ae175f520fccdc6ec9bfb3b3e855fa35 to your computer and use it in GitHub Desktop.
Save joaocarmo/ae175f520fccdc6ec9bfb3b3e855fa35 to your computer and use it in GitHub Desktop.
Imports Website Credentials from a CSV file into Apple's Keychain using macOS and Safari
#!/usr/bin/osascript
-- Adapted from https://github.com/PaperKup/csv-toicloudkeychain
-- For macOS Big Sur
-- Select the csv to import to iCloud keychain
-- Expects a CSV without headers and the following format:
-- WEBSITE,USERNAME,PASSWORD
set theFile to (choose file with prompt "Select the CSV file")
-- Read csv file
set f to read theFile
-- Split lines into records
set recs to paragraphs of f
-- Open safari passwords screen, check it is unlocked and do not allow to
-- proceed until it is unlocked or user clicks cancel.
-- If this doesn't open Safari, open it manually.
tell application "System Events"
tell application process "Safari"
set frontmost to true
keystroke "," using command down
tell window 1
click button "Passwords" of toolbar 1 of it
repeat until (exists button "Add" of group 1 of group 1 of it)
if not (exists button "Add" of group 1 of group 1 of it) then
display dialog "To begin importing, unlock Safari passwords then click OK. Please do not use your computer until the process has completed!" with title "CSV to iCloud Keychain"
end if
end repeat
end tell
end tell
end tell
-- Getting values for each record
set vals to {}
set AppleScript's text item delimiters to ","
set N to length of recs
repeat with i from 1 to N
set end of vals to text items of (item i of recs)
set kcURL to text item 1 of (item i of recs)
set kcUsername to text item 2 of (item i of recs)
set kcPassword to text item 3 of (item i of recs)
-- Write kcURL, kcUsername and kcPassword into text fields of safari passwords
tell application "System Events"
tell application process "Safari"
set frontmost to true
tell window "Passwords"
delay 1
click button "Add" of group 1 of group 1 of it
-- Write fields
tell sheet 1 of it
set focused of text field 1 to true
keystroke kcURL
set focused of text field 2 to true
keystroke kcUsername
set focused of text field 3 to true
keystroke kcPassword
click button "Add Password" of it
end tell
end tell
end tell
end tell
end repeat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment