Trying to call a Python object method inside an AppleScript Studio app

I’ve got an application written entirely in AppleScript Studio and dealing with the keychain via Keychain Scripting was starting to feel kludgy so I’m trying to use keychain.framework (http://sourceforge.net/projects/keychain). Having looked at http://tommorris.org/blog/2007/10/02, I was much more readily able to make use of keychain.framework in Python than in Objective-C, but I can’t seem to figure out how to call a Python object method from the main AppleScript.

here’s my little stub of Python, which works fine independently (as in, I can instantiate the object, call the getKeysByLabel method, and see results):

[code]#

PyKeychain.py

import objc, new

from Foundation import *

class PyKeychain(NSObject):
def init(self):
self.Keychain = new.module(‘Keychain’)
objc.loadBundle(‘Keychain’, self.Keychain.dict, bundle_path=‘/Library/Frameworks/Keychain.framework’)
def getKeysByLabel(self, theLabel):
search = self.Keychain.KeychainSearch.new()
search.setLabel_(theLabel)
return search.internetSearchResults()[/code]
I dragged an NSObject into my nib file in Interface Builder, pointed it at my Python class, and set it to be available in my AppleScript file with the name “myPyKeychainInstance”, then tried this:

set myKeys to (call method "getKeysByLabel" of object myPyKeychainInstance with parameters {"the_label"})

The result seems to be that myKeys remains unset.

Any ideas or suggestions? Thanks in advance for any help.

How did you do that?

What does getKeysByLabel return? A list/NSArray?

From the command line, I ran an interactive Python session and entered the Python I posted, but omitting “from Foundation import *” and the NSObject inheritance (because pyobjc didn’t seem to want to let me instantiate the object at the command line when it was an NSObject subclass). I then instantiate the object and call the method:

[code]>>> import objc, new

class PyKeychain:
… def init(self):
… self.Keychain = new.module(‘Keychain’)
… objc.loadBundle(‘Keychain’, self.Keychain.dict, bundle_path=‘/Library/Frameworks/Keychain.framework’)
… def getKeysByLabel(self, theLabel):
… search = self.Keychain.KeychainSearch.new()
… search.setLabel_(theLabel)
… return search.internetSearchResults()

myInstance = PyKeychain()
myInstance.getKeysByLabel(‘Yahoo!.ilg’)
(
Yahoo!.ilg: ilg @ Adium://Yahoo!.ilg/ ()
)
[/code]

from the keychain.framework,

- (NSArray*)internetSearchResults;

so I’m assuming getKeysByLabel returns an NSArray. An earlier version of the Python object reprocessed the return value from internetSearchResults() into a list of strings and returned that, but yielded the same unset variable in AppleScript.

Upon further fiddling, I found this error in my console log: “Unknown class PyKeychain' in nib file, using NSObject’ instead.”

So, my best guess is that the Python class definition file was never getting properly loaded so the object couldn’t be instantiated properly and I’m not at all sure how to do that. The net result is that as beautifully simply as the Python code was, I gave up and replaced it with Objective-C, which works.

As an important aside for anyone using “call method”, there seems to be no error reporting whatsoever when “call method” fails. It just fails silently.