Milliways
Milliways

Reputation: 1275

Call an Objective-c class from AppleScript in OSX 10.7

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.

Upvotes: 0

Views: 1965

Answers (3)

Ben
Ben

Reputation: 315

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.

Upvotes: 1

Michael Tsai
Michael Tsai

Reputation: 2030

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()'"

Upvotes: 1

regulus6633
regulus6633

Reputation: 19030

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.

Upvotes: 1

Related Questions