Monday 6 May 2013

Apple Script: Delete Keychain (OS X Mountain Lion 10.8)

This Apple script allows your user to delete their Keychains easily especially for those companies that requires their employees to change their password after 90 days. In order for this script to work, you only require to change the XXXXXXXXXX to the Name of the specific Keychain.


Note:
How to create an AppleScript
http://www.macinstruct.com/node/68




set t to ""
tell application "Keychain Access" to activate
set label to "XXXXXXXXXX"
set KeyKind to " "
set AcctName to " "
set ServiceName to " "
set Acctpassword to " "
set AcctComments to " "

set t to GetAcctnameServicenameAndPwrdFromPwrdItem(label, KeyKind)
set t2 to "Label: " & label & "
" & "Account name: " & text item 1 of t & "
" & "Password: " & text item 2 of t & "
" & "Service name: " & text item 3 of t & "
"
set t to DeletePwrdItemFromKeychain(label, KeyKind)
return t

on GetAcctnameServicenameAndPwrdFromPwrdItem(label, KeyKind)
set retarray to {"", "", ""}
set Qlabel to quoted form of label
set oldelim to text item delimiters
try
if KeyKind is equal to "Internet password" then
set t to do shell script "security 2>&1 find-internet-password -gl " & Qlabel & "-gA"
else
set t to do shell script "security 2>&1 find-generic-password -gl " & Qlabel & "-gA"
end if
set text item delimiters to "acct"
set tlst to every text item of t
set acct to item 2 of tlst
set text item delimiters to "\""
set tlst to every text item of acct
set acct to item 3 of tlst
if KeyKind is equal to "Internet password" then
set text item delimiters to "srvr"
else
set text item delimiters to "svce"
end if
set tlst to every text item of t
set svcnam to item 2 of tlst
set text item delimiters to "\""
set tlst to every text item of svcnam
set svcnam to item 3 of tlst
set text item delimiters to "\""
set tlst to every text item of t
set pw to item 2 of tlst
set retarray to {acct, pw, svcnam}
set retarray to {"", "", ""}
end try
set text item delimiters to oldelim
return retarray
end GetAcctnameServicenameAndPwrdFromPwrdItem

on DeletePwrdItemFromKeychain(label, KeyKind)
set Qlabel to quoted form of label
set retval to true
try
if KeyKind is equal to "Internet password" then
set t to do shell script "security delete-internet-password -l" & Qlabel
else
set t to do shell script "security delete-generic-password -l " & Qlabel
end if
on error
set retval to false
end try
return retval
end DeletePwrdItemFromKeychain


No comments:

Post a Comment