オープンソースのSAMKeychainを用いてキーチェーンにアクセスし、指定項目のパスワードの取得、新規項目の作成、キーチェーン項目の検索、アカウントの取得、項目の削除などを行うAppleScriptです。
# Apitoreは2019年5月31日をもってサービスを終了
オープンソースのSAMKeychainをCocoa Framework化したkeychainLib.frameworkをインストールを呼び出しています。
かつて、Mac OS X上にはKeychain ScriptingというAppleScriptから呼び出す専用の補助プログラムがOSに標準装備されていましたが、OS X 10.7で廃止になりました。以後、OS X 10.9までshell commandのsecurityコマンドを呼び出すことが必要となっていましたが、OS X 10.10でAppleScriptがCocoa呼び出しに対応したため、このような使い方ができるようになった次第です。
AppleScript名:keychainLib |
— Created 2016-12-02 by Takaaki Naganoya — 2016 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" use framework "SSKeychain" –https://github.com/soffes/sskeychain property kSSKeychainServiceName : "SSToolkitTestService" property kSSKeychainAccountName : "SSToolkitTestAccount" property kSSKeychainPassword : "SSToolkitTestPassword" property kSSKeychainLabel : "SSToolkitLabel" set kRes to getPasswordForAccount("apitore API Key", "maro@piyocast.com") of me –> "xxXXXxxX-XxXx-XXXX-xXXX-XXxXXxxXxxXx" –Get Password on getPasswordForAccount(aServiceName as string, anAccount as string) set bQuery to current application’s SSKeychainQuery’s alloc()’s init() bQuery’s setService:aServiceName bQuery’s setAccount:anAccount bQuery’s setPassword:(missing value) set bRes to (bQuery’s fetch:(missing value)) as boolean return (bQuery’s |password|()) as string end getPasswordForAccount — New item on makeNewItem(aPassword as string, aServiceName as string, anAccount as string, aLabel as string) set aQuery to current application’s SSKeychainQuery’s alloc()’s init() aQuery’s setPassword:aPassword aQuery’s setService:aServiceName aQuery’s setAccount:anAccount aQuery’s setLabel:aLabel return (aQuery’s |save|:(missing value)) as boolean end makeNewItem — Look up on lookUpPassWord(aServiceName, anAccount) set bQuery to current application’s SSKeychainQuery’s alloc()’s init() bQuery’s setService:aServiceName bQuery’s setAccount:anAccount bQuery’s setPassword:(missing value) set bRes to (bQuery’s fetch:(missing value)) as boolean return (bQuery’s |password|() ≠ missing value) end lookUpPassWord — Search for all accounts on getAllAccounts() set cQuery to current application’s SSKeychainQuery’s alloc()’s init() return (cQuery’s fetchAll:(missing value)) as list –> (NSArray) {{mdat:(NSDate) 2010-02-23 08:36:19 +0000, cdat:(NSDate) 2010-02-23 08:36:19 +0000, acct:"GoogleJapaneseInput", class:"genp", svce:"GoogleJapaneseInput", labl:"GoogleJapaneseInput", crtr:9999999999}, …… end getAllAccounts — Check accounts for service on getAccount(aServiceName) set cQuery to current application’s SSKeychainQuery’s alloc()’s init() cQuery’s setService:aServiceName return (cQuery’s fetchAll:(missing value)) as list –> (NSArray) {{mdat:(NSDate) 2016-03-02 13:54:54 +0000, cdat:(NSDate) 2016-03-02 13:44:19 +0000, svce:"SSToolkitTestService", acct:"SSToolkitTestAccount", class:"genp", labl:"SSToolkitLabel"}} end getAccount — Delete on deleteKeychain(aServiceName, anAccountName) set dQuery to current application’s SSKeychainQuery’s alloc()’s init() dQuery’s setService:aServiceName dQuery’s setAccount:anAccountName return (dQuery’s deleteItem:(missing value)) as boolean end deleteKeychain –Test Password Object on testPasswordObject() set eQuery to current application’s SSKeychainQuery’s alloc()’s init() eQuery’s setService:kSSKeychainServiceName eQuery’s setAccount:kSSKeychainAccountName set eDic1 to current application’s NSDictionary’s dictionaryWithObjectsAndKeys_(42, "number", "Hello World", "string", missing value) eQuery’s setPasswordObject:eDic1 set fRes to (eQuery’s |save|:(missing value)) as boolean set fQuery to current application’s SSKeychainQuery’s alloc()’s init() fQuery’s setService:kSSKeychainServiceName fQuery’s setAccount:kSSKeychainAccountName fQuery’s setPassword:(missing value) fQuery’s fetch:(missing value) return {fQuery’s passwordObject(), eDic1} –> {(NSDictionary) {number:42, string:"Hello World"}, (NSDictionary) {number:42, string:"Hello World"}} end testPasswordObject |