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.
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)
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.
精彩评论