Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a password from the keychain within an applescript running within Mail.app?

I'm trying to set up an applescript that performs (via python imaplib...) certain manipulations on my gmail account (which isn't configured with IMAP in Mail.app, only POP).

For that, the script needs to login using my password. If it weren't for security considerations, I could simply hardcode my password into the python script...

Is there a way that such an applescript triggered inside Mail.app (e.g. by a rule) can use my password stored inside the keychain?

like image 737
GJ. Avatar asked May 13 '12 19:05

GJ.


People also ask

How do I access my keychain passwords on Mac?

In the Keychain Access app on your Mac, if you don't see a list of keychains, choose Window > Keychain Viewer or press Command-1. Select the keychain that you want to view. To see more information about an item, either double-click it or select it and click the Info button in the toolbar.

Why does my Mac keep asking for a keychain password?

Your keychain may be locked automatically if your computer has been inactive for a period of time or your user password and keychain password are out of sync.

Is Apple keychain A good password manager?

The iCloud keychain security tool allows you to save your website user names and passwords, credit card numbers, shipping addresses, and even private notes. And in my test, I found it to be excellent for filling forms and login details and to autofill passwords.


1 Answers

The following is copied out of a script in my script library...

-- If you need to use a password in a script you can use the keychain to store the password and have the script retrieve it. This way your password is protected because you don't need to store passwords in clear text in a script.

-- Create the password item - Open Keychain Access application and select the keychain in the left column. Then click File>New Password Item..., give it a name, put your account shortname in account, and enter the password. Highlight it in the password list and get information on it. Under the Attributes button enter its kind as generic key. This is chosen because there aren't many of them and the search is much faster.

-- NOTE: In 10.7 apple removed keychain scripting and thus we now use the security command line tool

getPW("name of keychain item")

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
like image 152
regulus6633 Avatar answered Oct 17 '22 09:10

regulus6633