Question

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?

Was it helpful?

Solution 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 ♦♦*

OTHER TIPS

Try using pkill instead:

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

Some examples here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top