Currently I am developing a small application that I wanna to remove the airport settings.
Firstly, I use shell command networksetup to delete the preferred networks, and then delete the recorded-password in keychain by AppleScript. But I found that the airports’ passwords recorded in both system.keychain and login.keychain. I write the AppleScript such as:
tell application "Keychain Scripting"
set keychainName to "Login.keychain" -- "system.keychain"
set gkeyCount to count generic key of keychain keychainName
repeat with keyIndex from 1 to gkeyCount
log keyIndex
if (account of generic key keyIndex of keychain keychainName is "ABCD") then
delete generic key keyIndex of keychain keychainName
log "find the key"
end if
end repeat
end tell
For keychain “login.keychain”, it’s no problem, but for keychain “System.keychain”, it failed with a popup show me “Keychain Scripting got an error: File not open with write permission.”
Any idea?
Edit: This doesn’t work. Ah well, it was just a guess anyway. I’m leaving the answer up for posterity.
My guess is that the problem is that the system keychain is locked. I can’t really test this, since I don’t want to delete things from my keychain, but I did confirm that my system keychain defaults to being locked, and this seems like the sort of thing which would cause the error you see. Thus, I’d do something like this:
The idea is to first unlocked the keychain if you need to, and then make sure you relock it when you’re done. Let me know if this does or doesn’t work—like I said, I had nothing I wanted to test this on.