Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Adding and changing order of Keychains

I would like to be able to write a script to add a new Keychain before my "login" Keychain. I can't even get this to work from terminal. Any help would be appreciated.


Thanks

MacBook Pro, OS X Yosemite (10.10.3), null

Posted on Jun 15, 2015 9:18 AM

Reply
19 replies

Jun 22, 2015 9:08 AM in response to dvrose

The following GUI script uses cliclick to move the new keychain before the “login” keychain. It seems to work properly most of the time. It might require some minor adjustments though.


set theKeychain to "My New Keychain"

set thePassword to "My New Password"


tell application "Keychain Access"

activate

repeat until frontmost is true

end repeat

end tell


tell application "System Events" to tell process "Keychain Access"

tell menu bar 1

clickmenu item "Keychain Viewer" of menu 1 of menu bar item "Window"

if existsmenu item "Show Keychains" of menu 1 of menu bar item "View" then ¬

keystroke "k" using {command down}

end tell

set theOutline to outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1

set theValue to (characterid 65532) & space & space & theKeychain

if not (exists (row 1 of theOutline whose value of text field 1 is theValue)) then

keystroke "n" using {option down, command down}

delay 1

keystroketheKeychain & return

delay 1

keystrokethePassword & tab & thePassword & return

set {x, y} to position of row 1 of theOutline whose value of text field 1 is theValue

set x to x + 21

do shell script "/usr/local/bin/cliclick dd:" & x & "," & y & " du:" & x & "," & (y - 20)

end if

keystroke "q" using {command down}

end tell

Hope it can help.

Jun 22, 2015 12:33 PM in response to dvrose

This new version of the script should do what you are asking for:

set theKeychain to "My New Keychain"

set thePassword to "My New Password"


tell application "Keychain Access"

activate

repeat until frontmost is true

end repeat

end tell


tell application "System Events" to tell process "Keychain Access"

tell menu bar 1

clickmenu item "Keychain Viewer" of menu 1 of menu bar item "Window"

if existsmenu item "Show Keychains" of menu 1 of menu bar item "View" then ¬

keystroke "k" using {command down}

end tell

set theOutline to outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1

set theValue to (characterid 65532) & space & space & theKeychain

if not (exists (row 1 of theOutline whose value of text field 1 is theValue)) then

keystroke "n" using {option down, command down}

delay 1

keystroketheKeychain & return

delay 1

keystrokethePassword & tab & thePassword & return

set {x, y} to position of row 1 of theOutline whose value of text field 1 is theValue

set x to x + 21

set n to 0

repeat with thisValue in (get value of text field 1 of rows of theOutline)

set n to n + 1

if thisValue contains " login" then exit repeat

end repeat

do shell script "/usr/local/bin/cliclick dd:" & x & "," & y & " du:" & x & "," & (y - n * 20)

end if

keystroke "q" using {command down}

end tell

Jun 23, 2015 11:27 AM in response to Pierre L.

Hello Pierre I am trying to make an all in one script to download "homebrew" then "cliclick" then run the script. I have an issue of the terminal needing the sudo password but the rest of the script completing before the user is able to type it in. Is there a way to have "wait" type command until one part of the script has completed to run the rest? I have tried the delay command but have not had any success.


set theKeychain to "testing12345"

set thePassword to "testing12345"


tell application "Terminal"

do script "ruby -e \"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)\"" & return & "brew install cliclick"

end tell




tell application "Keychain Access"

activate

repeat until frontmost is true

end repeat

end tell


tell application "System Events" to tell process "Keychain Access"

tell menu bar 1


clickmenu item "Keychain Viewer" of menu 1 of menu bar item "Window"

if existsmenu item "Show Keychains" of menu 1 of menu bar item "View" then ¬


keystroke "k" using {command down}

end tell

set theOutline to outline 1 of scroll area 1 of splitter group 1 of splitter group 1 of window 1

set theValue to (characterid 65532) & space & space & theKeychain

if not (exists (row 1 of theOutline whose value of text field 1 is theValue)) then


keystroke "n" using {option down, command down}


delay 1


keystroketheKeychain & return


delay 1


keystrokethePassword & tab & thePassword & return

set {x, y} to position of row 1 of theOutline whose value of text field 1 is theValue

set x to x + 21

set n to 0

repeat with thisValue in (get value of text field 1 of rows of theOutline)



if thisValue contains " testing12345" then exit repeat

set n to n + 1

end repeat


do shell script "/usr/local/bin/cliclick dd:" & x & "," & y & " du:" & x & "," & (y - n * 20)


end if


end tell










Thank you for your help.

Jun 23, 2015 12:52 PM in response to dvrose

First of all, it would be more elegant to replace

if thisValue contains " testing12345" then exit repeat

with

if thisValue contains theKeychain then exit repeat

in the last part of the script.


Now, why should you have to download and install cliclick each time you run the script. Why not instead save the script as a script bundle with cliclick as one of its resources (inside the bundle's Contents/Resources/ folder) ?

Adding and changing order of Keychains

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.