Applescript – Quit or Launch Application if another is or is not running

A simple use of Applescript to get PID (unix id of processes ) and kill one Application if another is not running.

 

 

But heres a better example..

If I have one App running and I want a second app always running while the first app is running.  Or when I quit the first App the second app will also quit.

You may have noticed the   #!/usr/bin/osascript    This is an the Applescript Shebang . Which basically allows you to write a applescript in its native syntax ,  save it as a applescrpt text file. And run it  like a shell command.

Like any shell executable you need to give it the correct permissions to execute.

So after saving the script down as a Applescript Text file. (.Applescript). Open Terminal.app and type:

chmod  755 /path/To/foo.applescript

This makes the file executable.

One of the ways I would use this now executable script. Is as a user’s Launch Agent.  Launch Agents run jobs on your Mac in the background. Google  or read about  Launch Agents  on Apples website for a better explanation.

All I want my  Launch Agent to do is run the Script every 30 seconds.

I first create a plain text document in textEdit  document. You can use your own flavour of plain text editing tool. And put the following code in it:

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” “http://www.apple.com/DTDs/PropertyList-1.0.dtd”>
<plist version=”1.0″>
<dict>
<key>Label</key>
<string>com.example.dualApps</string>
<key>ProgramArguments</key>
<array>
<string>/path/To/foo.applescript</string>
</array>
<key>StartInterval</key>
<integer>30</integer>
</dict>
</plist>

  •   Label   name of the Launch Agent process. It would be a good idea to name the plist file the same.  Here it is called  com.example.dualApps
  • ProgramArguments  are what it says on the tin. In this case it is just the path to the .appscript to run.
  • StartInterval   the time interval I want the agent to run.

More info can be found at  Launch Agents .

Save the  com.example.dualApps.plist  file in ~/Libary/LaunchAgnets as a .plist  file rather than a .txt.

In Terminal.app you can then start the job.

launchctl load ~/Libary/LaunchAgnets/myfooAgent.plist

Now ever 30 seconds The script will run and either quit or launch Snippets depending if Script Editor is running or not.

To unload the Agent.
launchctl unload ~/Libary/LaunchAgnets/myfooAgent.plist

Read the Manual for more commands on launchctl

Hi, Please Leave a Reply