如何在Objective-C中复制AppleScript函数?

时间:2010-02-04 07:03:15

标签: objective-c cocoa macos applescript

此AppleScript代码提供已删除到脚本上的文件的名称。如何在Objective-C应用程序中执行相同操作?用Objective-C编写的应用程序是否能够使用文件名作为JAR的参数来引导JAR文件?

on open of theFiles -- Executed when files are dropped on the script

    set fileCount to (get count of items in theFiles)

    repeat with thisFile from 1 to fileCount
        set theFile to item thisFile of theFiles
        set theFileAlias to theFile as alias

        tell application "Finder"
                set fileInfo to info for theFileAlias
                set fileName to name of fileInfo

                -- something to this effect, but now that you have the file name,
                -- do what you will...
                do shell script "cd /Desktop/RunJar/; java -jar " & fileName

        end tell

    end repeat

end open

我们需要将这个AppleScript替换为可以运行已被删除到已编译应用程序的JAR的已编译应用程序。

2 个答案:

答案 0 :(得分:2)

要获取JAR文件的路径,您的应用必须首先实现拖放。 See Drag and Drop Programing Topics for Cocoa(或PDF

答案 1 :(得分:0)

至于实际运行jar:

  

做shell脚本“cd / Desktop / RunJar /; java -jar”&文件名

使用NSTask。区别在于NSTask不运行shell脚本;它直接运行程序(在本例中为java)。在运行之前,您需要设置任务的工作目录。