Get user's username / password from keychain?

I’m writing a little script/app that will turn off all network interfaces and then launch Google Drive and then turn the network interfaces back on.
This is for my olde macbook pro running El Capitan which if there is an active network connection, Google Drive balks and refuses to launch complaining about how I need to update the OS first, which I can’t. The macbook pro can’t go beyond 10.11.6.
I’ve discovered that if i turn off all internet connections, then launch Google Drive, then reenable internet access, Google Drive basically still functions.

For Wi-Fi I can use
do shell script “networksetup -setairportpower en1 off”
and
do shell script “networksetup -setairportpower en1 on”
and they don’t require a username/password.

However for Ethernet, I’m using
do shell script “networksetup -setnetworkserviceenabled Ethernet off” user name “me” password “mypass” with administrator privileges
and
do shell script “networksetup -setnetworkserviceenabled Ethernet on” user name “me” password “mypass” with administrator privileges

I’ve been tinkering with using PrefsStorageLib to store the username and password, but that’s not secure, and I’m thinking I’ll share this effort once it works.

But it’d be way better to use the keychain, but that’s a complete mystery to me.
Can it be done from in an Applescript Applet ?
(Access the keychain safely / securely ?)

tia

furbies

I have been using this for years for my own stored passwords

on getPW(keychainItemName)
	do shell script "security 2>&1 >/dev/null find-generic-password -gl " & quoted form of keychainItemName & " | awk '{print $2}'"
	return (text 2 thru -2 of result)
end getPW

But of course this is not safe in the script either, as anyone could execute it. To be on the safe side, I would export this to a Run-Only script.

Hello @furbies :wave:

Here is a snippet I have in use for a long time now …

property KeychainPasswordName : "YOUR_KEYCHAIN_PASSWORD" -- name of your password item in the Keychain

set my_password to getPassword(KeychainPasswordName)
set my_username to getUsername()


-- Retrieve the administror password you saved on your Keychain
on getPassword(keychainItemName)
	local password
	set password to do shell script ("/usr/bin/security 2>&1 >/dev/null find-generic-password -gl " & quoted form of keychainItemName & " | cut -c 11-99 | sed 's/\"//g'")
	if password contains "could not be found in the keychain" or password as string is equal to "" then
		display alert "Password not found in the keychain" message "Certain tasks in this script need the administrator password to work.
You must create a new password in the OS X Keychain with a custom name, and set it with your administrator password, then edit this script." as critical
		error "Password could not be found in the keychain."
	else
		return password
	end if
end getPassword

-- Get the short username (name of your home folder)
on getUsername()
	#tell application "System Events" to return the name of current user
	short user name of (system info)
end getUsername

as far as I am aware this code works without issues…

Greetings from Germany :de:

Tobias

Thank you to Dirk & Tobias !

Is there a way to just request access to the human’s “user name” & “password” which will likely be an Administrator username/password for the Mac ?

It’s easy enough to tell the user to create a new entry in Keychain Access for my Applet so it can know what the user name and password of an admin account is, but there’s going to be that percentage that just can’t do it right, and so the Applet won’t work as expected…

ps: the getUsername function needs a small correction

– Get the short username (name of your home folder)
on getUsername()
# tell application “System Events” to return the name of current user
return short user name of (system info) ## Added the return
end getUsername

You could perhaps ask “Piyomaru”. He is also in the forum and has two entries on the subject on his site:

http://piyocast.com/as/archives/category/keychain
http://piyocast.com/as/archives/category/keychain-2

However, the frameworks used are from 2015/16 and therefore do not run on an Apple Silicon. Perhaps someone could recompile them (unfortunately I failed).

1 Like

Hello @furbies

No it doesn’t matter if you write it like I‘ve done it or like you did.

It’s an example of the AppleScript Syntax where more than one form means absolutely the same.

If you prefer it this way - just go for it …

Greetings from Germany :de:

Tobias

Hello @Dirk :wave:

Many thanks for sharing these… I didn’t know they existed…

Very useful Stuff.

Greetings from Germany :de:

Tobias