Keychain Manipulation

I have an NSTableView with 3 columns in Interface Builder. My Applescript code is looping through the keys in the active keychain looking for a match to text that was entered into a textbox also put there by IB.

I want to add a new row each time a match is found in the loop and fill in the name, address, and password of the key that was found in the keychain. I am getting Applescript errors with numbers. Can someone take a look at the code:


-- KeySearch.applescript
-- KeySearch



on end editing theObject -- the text box for the search string
	set theSearchKey to string value of text field "TextBox1" of window "Window1"
	my findtheKey(theSearchKey)
end end editing

to findtheKey(theSearchKey)
	set keysFoundData to make new data source at end of data sources
	set tv to table view "TableView1" of scroll view "ScrollView1" of window "Window1"
	make new data column at end of data columns of keysFoundData with properties {name:"Name"}
	make new data column at end of data columns of keysFoundData with properties {name:"Account"}
	make new data column at end of data columns of keysFoundData with properties {name:"Password"}
	
	set theText to theSearchKey
	set theFoundKeys to {}
	tell application "Keychain Scripting"
		launch
		tell keychain "login.keychain"
			set theKeys to every key
			repeat with theKey in theKeys
				if name of theKey contains theText then makeARow(theKey, keysFoundData)
			end repeat
			set data source of tv to keysFoundData
			
		end tell
	end tell
end findtheKey

to makeARow(theFoundKey, theDataSource)
	set arow to make new data row at end of data rows of theDataSource
	set contents of data item "Name" of arow to name of theFoundKey
	set contents of data item "Account" of arow to account of theFoundKey
	set contents of data item "Password" of arow to password of theFoundKey
end makeARow

I’m not quite sure I follow what you are trying to accomplish. In any event. I find it easiest to either assign the data source of the table in IB or in the “awake from nib” handler and then be done with it. As to adding a row, it is easiest to just use the “append” command. Take a look at this quick mock-up project that I think does what you are trying to accomplish:

http://homepage.mac.com/jonn8/as/dist/keylister.sit

Jon

You sir, are to be commended. That is a superb piece of work. All I had to do is hard code 'login.keychain" and clean the sources because I don’t have a keychain with my current username on it.

It works perfectly and is an excellent teaching tool.

Thanks so very much for your help!

–Hal Holbrook

See also: http://www.red-sweater.com/blog/170/usable-keychain-scripting