Keychain Scripting, yet again.

OK, here’s the deal. I’m writing an A.S.S. app that requires a password and I want it to store the password in the system keychain. So far I’ve managed to get the app to create it’s own keychain, store and retreive it’s password. (which you would think would be the hard part.) Here’s the problems I’m having.

  1. I want to be able to delete the key if the user unselects the “store password” option in the application’s preferences. I know how to delete keys by index number, but I need to delete the key by name.

  2. I also need to be able to update the password in the key if it has changed. If this isn’t do’able, I suppose I could delete the key and recreate it with the new password, but that brings us back to problem number 1.

I’ve done several hours of googling to no avail. Any help at all will be greatly appreciated.

Thanx in advance,

Don’t you love it when people answer their own question. Here’s what I came up with and it seems to be working for now.

tell application "Keychain Scripting"
	tell current keychain
		set numkeys to count keys
		repeat with thecount from 1 to numkeys
			set keyname to get name of key thecount
			if keyname is "The Key to Kill" then
				set keytokill to thecount
			end if
		end repeat
		delete generic key keytokill
	end tell
end tell

As far as updating the password, I’m having the app delete the key and write a new one.

This will be a bit faster since it doesn’t have to count the keys first and it exits the repeat once it deletes the key:

This had been answered in an earlier thread but I think it has been lost in recent site maintenance.


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]