0

Is it possible to call an Objective-c class from AppleScript in OSX 10.7?

I have seen some suggestions for Snow Leopard or earlier, but none seem to work.

AppleScript-Obj-C seems to be a way of constructing a GUI which uses AppleScript, but I want to call a class I have written from a script.

The following (which does not work) is what I would like to do:-

on getJpegComment(photoFile)
    set aJpeg to call method "initWithPath" of class "JpegCom" with parameter photoFile (does not work)
    return call method "comment" of aJpeg
end getJpegComment

tell application "iPhoto"
    tell album "Sale"
        set thePhotos to get every photo
    end tell

    repeat with aPhoto in thePhotos
        tell application "iPhoto"
            set aPhotoFile to image path of aPhoto
            set aComment to my getJpegComment(aPhotoFile)
            set comment of aPhoto to aComment

        end tell
    end repeat
end tell

I started down this path because of links which seemed to indicate this was possible.

I could write an Objective-c program, which called AppleScript, but this seems overkill for seemed to be a simple task.

PS I have thousands of photos, which I had entered JPEG COMments of Windows, unfortunately iPhoto does not understand these.

3
  • So are you using applescript-objc? Commented Mar 26, 2012 at 1:24
  • "call method" is from AppleScript Studio, which was deprecated in Snow Leopard. AppleScriptObjC can call Objective-C classes and vice-versa, in addition to being able to call Cocoa class methods directly - what is the class "JpegCom"? Commented Mar 26, 2012 at 1:35
  • Thanks to all who commented. I was beginning to suspect you couldn't call Objective-C from AppleScript. I did try an AppleScriptObjC application created with Xcode, but I didn't want a GUI - just a simple script. All the examples I could find were GUI (and it is much easier to make an Objective-C application). If I could have used my main script, and just provided a handler in AppleScriptObjC this would be ideal. Commented Mar 27, 2012 at 0:42

3 Answers 3

1

When I want to do a simple task like you, and I need something from objective-c, I find the easiest solution is to turn the objective-c part into a command line tool in Xcode. Then I call the tool from applesript using "do shell script". I have many examples of these tools on my website of 1) the code for the unix tool, and 2) how to use the tool from applescript. So basically you make a tool which accepts one parameter (eg. the path to the image) and it returns the comment. Look here for my tool examples. Any of the items under the "Code Sharing" menu will help you with this approach.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It is easy to forget the traditional UNIX piping approach. Your scripts were very helpful. I already had a console program (C++) and it was only a few minutes work to convert it into a suitable tool
1

No, you can't call Objective-C from a regular, freestanding AppleScript run from AppleScript Editor or a script menu. You would need to build your script into an AppleScriptObjC application created with Xcode or execute it in a special environment such as ASObjC Runner.

For cases where I just want to call a couple Objective-C methods, I would probably use do shell script to invoke Python and use the PyObjC bridge. That way your script remains compatible with regular AppleScript, and you don't need any auxiliary executable files. For example:

do shell script "python -c 'from Foundation import NSDate; print NSDate.date()'"

3 Comments

That's pretty cool Michael. I'll remember that. For this question where he wants to call his own class and init it with a path, I'm not sure your solution would work.
@regulus6633 My approach is definitely easiest for system classes. It wasn't clear to me where his class came from. If it's already compiled as a framework, he could import that framework from Python. Otherwise, he needs to use Xcode to compile the source, and he could either make a hybrid AppleScriptObjC app, or a framework, or a command-line tool as you suggest. The tool would probably be simplest.
As of Yosemite, this is now possible: mjtsai.com/blog/2014/10/29/applescript-and-yosemite
1

You can make an AppleScriptObjC script using AppleScript Editor. Go to File > New from Template > Cocoa-AppleScript Applet.

This will run like a standard script, without any GUI, but you can also access Cocoa functionality like you can when using AppleScriptObjC in Xcode. You should also be able to use Objective-C classes like you would do in Xcode.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.