Ashish Pancholi
Ashish Pancholi

Reputation: 11

How to Duplicate an AppleScript function in Objective-C?

This AppleScript code gives the name of files that have been dropped onto the script. How do I do the same in an Objective-C app? Would an application written in Objective-C be able to boot the JAR file using the file name as an argument to 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

We need to replace this AppleScript with a compiled app that can run a JAR that has been dropped onto the compiled app.

Upvotes: 1

Views: 419

Answers (2)

Peter Hosey
Peter Hosey

Reputation: 96393

As for actually running the jar:

do shell script "cd /Desktop/RunJar/; java -jar " & fileName

Use NSTask. The difference is that NSTask does not run a shell script; it runs the program (in this case, java) directly. You will need to set the task's working directory before running it.

Upvotes: 0

NSResponder
NSResponder

Reputation: 16861

To get the path to the JAR file, your app must first implement Drag-and-Drop. See Drag and Drop Programing Topics for Cocoa (or as PDF)

Upvotes: 2

Related Questions