Variable from Objective-C into AppleScript ?

I’m writing a software that will get the name of a person in Address Book via the ABFramework (Objective-C), and then send a message to that person in iChat. Since I can only do the iChat part via AppleScript, I was wondering if there’s a way to have my appleScript get the value from the Obj-C code. Something like:

[code] NSString *friendsName = @“FeLuks”; // → on the Objective C code

tell application “iChat” to message user with name friendsName // → on the AppleScript file[/code]
I’d be loading the script on Obj-C with this: [script executeAndReturnError:nil]; , script being a NSActionScript pointer that holds the data for the script that will talk to iChat.
Any help is much appreciated!
I’ve been searching on the forum for this for a while and couldn’t find it, sorry if It’s been answered before.

I’m a bit confused as to what you’re trying to accomplish exactly. For the most part, applescript studio provides pretty good ways to interact with obj-c without doing anything really complex. Your code is obviously not anything like what you’ll be using in your actual obj-c method, so your example doesn’t give me anything to base my advice on. If all you wanted to do is set a string value, that of course can be accomplished in AS only.

Generally, to execute some obj-c code from an applescript in ASS you’d do two things:

  1. Create a subclass, typically of NSObject, in IB (see my post about menu item separators for a bit of discussion about this.) For this example we’ll name it “FLObject”. Create both the interface and implementation files (.h & .m). In your .m file, you should create a method that executes your obj-c code, and then returns the string value you’re looking for. For example, something like this…

+(NSString *)getABName:(id)sender { NSString* theUserName = [some code here to generate an nsstring object]; return theUserName; }
2) In your applescript, use the ‘call method’ command to execute your obj-c. By writing an obj-c method that returns a string value, when the method is executed the result is passed back to your script. For example, you could use something like the following to get the value into your script…

set theUserName to call method "getABName:" of class "FLObject"

The above examples are merely a guide to help understand how interacting with obj-c works in ASS. There are other ways that may be dictated by how your UI elements and design are set up. It is unclear what the actual methods you wish to implement are, so I have no way to actually provide a ‘solution’ unless you are more forthcoming with what you already have… both in terms of your desired obj-c methods and also in how you expect to implement them. Are you using a control to trigger the execution of the code, is it happening programmatically… how and when are you trying to get this obj-c code to execute? I don’t really have any experience using the ABFramework, so I can’t give you specific examples. Are you certain that address book doesn’t have applescript hooks for getting the information you’re after? Once you select a person in the list, is it necessary to call an obj-c method, or can you pass the information you get back to address book in an applescript call instead? Don’t know, just a thought that might save you some trouble.

Also, I’m not sure where you are going with using the obj-c method “[script executeAndReturnError:nil];”. This is used to create an applescript IN obj-c and then execute it from obj-c. If you are writing your app in ASS, I can think of no scenario where you would want to write an obj-c method that would in turn create an applescript object to execute. This is redundant and unnecessary… simply execute your applescript from your script, and then interface with obj-c only as necessary.


Hi Jobu, thanks for your reply.
This is mainly an Obj-C software, but for a few thing I’m going to have to use AppleScript, thanks to Apple and its private InstantMessage framework. I’ll have the Applescripts in the same bundle, and then run them inside the Obj-C code, as you said. I’m not doing this on AS Studio, so do you think there’s any other way?

OK, that clarifies things. :smiley:

No, then, I don’t see any other way to interface between your obj-c app and any other apps. You’re doing it the right way, and this is exactly what applescript is for. As an example, you might use the following to reveal a file in the finder…

NSString *asString = [[NSString alloc] initWithFormat:@"tell application \"Finder\"\nactivate\nreveal POSIX file (\"%@\")\nend tell", thePath]; NSAppleScript *asScript = [[NSAppleScript alloc] initWithSource]; [asScript executeAndReturnError:nil]; [asString release]; [asScript release];
Good luck,

Thank you very much! :D:D:D