简体   繁体   English

在OSX 10.7中从AppleScript调用Objective-c类

[英]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? 是否可以在OSX 10.7中从AppleScript调用Objective-c类?

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. AppleScript-Obj-C似乎是一种构造使用AppleScript的GUI的方法,但是我想调用从脚本编写的类。

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. 我可以编写一个名为AppleScript的Objective-c程序,但这似乎很简单,但似乎有点过分。

PS I have thousands of photos, which I had entered JPEG COMments of Windows, unfortunately iPhoto does not understand these. PS我有成千上万张照片,我已经输入Windows的JPEG COMments,不幸的是,iPhoto无法理解这些照片。

You can make an AppleScriptObjC script using AppleScript Editor. 可以使用AppleScript编辑器制作AppleScriptObjC脚本。 Go to File > New from Template > Cocoa-AppleScript Applet. 转到“文件”>“来自模板的新建”>“ 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. 这将像标准脚本一样运行,没有任何GUI,但是您也可以像在Xcode中使用AppleScriptObjC一样访问Cocoa功能。 You should also be able to use Objective-C classes like you would do in Xcode. 您还应该能够像在Xcode中一样使用Objective-C类。

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. 当我想做像您这样的简单任务,并且需要Objective-C的帮助时,我发现最简单的解决方案是将Objective-C的部分变成Xcode中的命令行工具。 Then I call the tool from applesript using "do shell script". 然后,我使用“ do shell script”从applesript调用该工具。 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. 我的网站上有许多这些工具的示例,其中1)Unix工具的代码,2)如何从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. “代码共享”菜单下的任何项目都将帮助您采用这种方法。

No, you can't call Objective-C from a regular, freestanding AppleScript run from AppleScript Editor or a script menu. 不可以,您不能通过从AppleScript编辑器或脚本菜单运行的常规,独立的AppleScript调用Objective-C。 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 . 您需要将脚本构建到使用Xcode创建的AppleScriptObjC应用程序中,或在诸如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. 对于只想调用几个Objective-C方法的情况,我可能会使用do shell script来调用Python并使用PyObjC桥。 That way your script remains compatible with regular AppleScript, and you don't need any auxiliary executable files. 这样,您的脚本将与常规AppleScript保持兼容,并且您不需要任何辅助可执行文件。 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM