Search code examples
applescriptkeychain

Is it possible to delete an application password from Keychain using AppleScript or a Shell Script?


I've written a AppleScript/Shell Script app in Automator to kill all relevant processes, reset environment testing variables, and to delete all relevant plists, device files, and logs. However, I still need to manually remove application oAuth tokens before I can start testing again. I need a solution to automatically find all passwords that match a property and delete them.

From another question I found the following snippet:

property theKey : "abcd"

tell application "Keychain Scripting"
    set keyList to every generic key of current keychain
    repeat with x from 1 to (length of keyList)
        if the name of item x of keyList is theKey then
            delete generic key x of current keychain
            exit repeat
        end if
    end repeat
end tell

When I try to verify it, though, it gets hung up on "generic".

Thoughts?


Solution

  • I ended up finding a solution that worked perfectly.

    security delete-generic-password -l "com.oauth_token_example.auth"
        ~/Library/Keychains/login.keychain || set t 0
    

    This effectively deletes any keys in the Keychain that match the string. If no keys are found that match the string, it returns an error, so I double-piped in an else statement (set t 0) that does nothing. I'm sure there's an easier way, but if it works, who cares?