AppleScript to force quit Java (JAR) programs?

泪湿孤枕 提交于 2019-12-08 13:00:03

问题


Hello,

I run a Java (jar) application on MAC OS. I am using an AppleScript to run the Java program and it works fine. Now, I like to use an AppleScript to close the Java program. I need to Force Quit the Java program. I used the following AppleScript,

set app_name to "NPC"
set the_pid to (do shell script "ps ax | grep " & (quoted form of app_name) & " | grep -v grep | awk '{print $1}'")
if the_pid is not "" then do shell script ("kill -9 " & the_pid)

The AppleScript that runs the Java program is called "NPC.app" When I run "NPC.app" it shows as NPC and NPC.npc on the Activity Monitor application. The above code which is set to remove the NPC application does not remove either NPC (this is the "NPC.app") or NPC.npc (this is the Java program). I get the following error,

error "sh: line 0: kill: 1180 1182: arguments must be process or job IDs" number 1

1180 being the PID for NPC and 1182 being the PID for NPC.npc in the Activity Monitor.

What is the correct AppleScript to force quit the Java program?


回答1:


Try using pkill instead:

do shell script ("pkill -9 NPC*")

Some examples here.




回答2:


Here is the AppleScript that will close Java programs,

tell application "Terminal"
    do shell script "ps ax | grep \"java.*$1\" | grep -v grep | awk '{ print \"kill \" $1 }' | sh"
    quit
end tell

*Reference: MAC OS - Is there a way to close a Java program?

Thanks to: wojciechka ♦♦*



来源:https://stackoverflow.com/questions/22900013/applescript-to-force-quit-java-jar-programs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!