开发者

On MAC O.S., how to get the name of files that has been dropped onto apple script to boot JAR with the name of file as a argument to JAR

开发者 https://www.devze.com 2022-12-18 15:41 出处:网络
In Script editor i have written a command to boot the JAR. do shell script \"cd /Desktop/RunJar/; java -jar RunMyJar.jar\"

In Script editor i have written a command to boot the JAR.

do shell script "cd /Desktop/RunJar/; java -jar RunMyJar.jar"

and Saved as script file as a Application. When i click on script file jar get run.

My requirement

I would like get the name of file that has been dropped onto the script file and would like to pass the name of that dropped file as an argument to my jar.

I have implemented this in Windows but could not working similar on MAC O.S.

On Windows

I have placed a BAT file to Boot JAR along with the absolute file name, that has been dropped on the bat file on windows. @echo %* will give me the list of files that has been dropped onto Batch file.

@echo %* @pause java开发者_高级运维 -jar RunMyJar.jar %*

Similar i would like to implement in MAC O.S.

Thanks.


I found the following example at: Ben's AppleScript Snippets:

on open of finderObjects -- "open" handler triggered by drag'n'drop launches
  repeat with i in (finderObjects) -- in case multiple objects dropped on applet
    displayName(i) -- show file/folder's info
    if folder of (info for i) is true then -- process folder's contents too
      tell application "Finder" to set temp to (entire contents of i)
      repeat with j in (temp)
        display dialog j as string -- example of doing something with each item
      end repeat
    end if
  end repeat
end open


You can also easily modify my answer to a similar question:

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


To add to Paul's answer

on open of finderObjects

repeat with currFile in finderObjects
   -- ok, we have our current item. But it's an "alias": a Mac OS path
   -- not a POSIX path
   set unixPath to POSIX path of currFile

   set base to do shell script "dirname " & unixPat
   set fname to do shell script "basename " & unixPath
   -- you could ask the Finder to give this to you too -- I'll use this way because
   -- I'm guessing it's more familiar to you

   do shell script "cd '" & base & "';" & " java -jar " & fname
end repeat  

end open

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号