0

I followed the docs and wrote:

require 'rubygems'
require 'appscript'

loginKeychain = Appscript::app('Keychain Scripting').keychains['login.keychain']
userName = loginKeychain.keys[its.name.eq(name)].password.get

UPDATE: if I run the script via Terminal, it succeeds the first time, and then returns the error below until I quit and restart Terminal; and repeat...

Error returned intermittently:

/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()

UPDATE 2: Due to the mysterious error, and that Keychain Scripting seems buggy in general, I decided to use MacRuby. I found the MacRuby Keychain Wrapper library, which calls Keychain Services directly, rather than going through Scripting:

require 'keychain-wrapper/keychain'
MRKeychain::GenericItem.item_for_service(service_name).password
  • Easy
  • More efficient
  • Works flawlessly

Done.

Sean DeNigris
  • 6,306
  • 1
  • 31
  • 37

2 Answers2

0

I didn't use Ruby but I used Python and I have no trouble. I'd try doing regular Applescript and seeing if it can access Keychain Scripting. If not then try rebooting your Mac. But this sounds like more a problem of your computer than Appscript.

Clark
  • 833
  • 4
  • 6
  • You successfully scripted Keychain using Python? Applescript worked fine from the AppleScript Editor - I prototyped it there before translating to Ruby. – Sean DeNigris Apr 23 '11 at 00:10
  • Yeah - I ran a small test with Python & Appscript. Try using ASTranslate and click the check to send the actual events to the application. Sorry I can't help directly with Ruby. I'm a Python man and just don't know Ruby. – Clark Apr 23 '11 at 18:03
  • How did you run your Python script? In Terminal, the same thing happened when I executed:
        $python
        >>>from appscript import *
        >>>app('Keychain Scripting').keychains['login.keychain'].keys[its.name == 'name'].password.get()
    
    – Sean DeNigris Apr 24 '11 at 16:24
  • I ran it from within Textmate. But that shouldn't matter. Sounds like your appscript might be borked. Try reinstalling. – Clark Apr 25 '11 at 16:53
0

I'm getting that same error, but just with things that require authentification:


kc = app("Keychain Scripting").keychains["login.keychain"]
p kc.keys[its.name.eq("loginwindow")].name.get
# ["loginwindow"]
p kc.keys[its.name.eq("loginwindow")].password.get
# ... Application isn't running ...

tell app "Keychain Scripting" to password of keys of keychain "login.keychain" where name is "loginwindow" works just fine.

Lri
  • 26,768
  • 8
  • 84
  • 82