我按照文档并写道:
require 'rubygems'
require 'appscript'
loginKeychain = Appscript::app('Keychain Scripting').keychains['login.keychain']
userName = loginKeychain.keys[its.name.eq(name)].password.get
更新:如果我通过终端运行脚本,它第一次成功,然后返回下面的错误,直到我退出并重新启动终端;并重复...
间歇性返回错误:
/Library/Ruby/Gems/1.8/gems/rb-appscript-0.5.3/lib/appscript.rb:539:in `_send_command': CommandError (Appscript::CommandError)
OSERROR: -600
MESSAGE: Application isn't running.
COMMAND: app("/System/Library/ScriptingAdditions/Keychain Scripting.app").keychains["login.keychain"].keys[its.name.eq("a name")].password.get()
更新 2:由于神秘的错误,而且 Keychain Scripting 通常看起来有问题,我决定使用MacRuby。我找到了MacRuby Keychain Wrapper库,它直接调用Keychain Services,而不是通过 Scripting:
require 'keychain-wrapper/keychain'
MRKeychain::GenericItem.item_for_service(service_name).password
- 简单的
- 更高效
- 完美运行
完毕。