Make the program Messages relaunch once closed

jyung
New Contributor III

JAMF Nation

I know there is probably a easy terminal command or script to make a program relaunch once close. My company uses Messages for a IM with bonjour configured. End users keep on closing the program so they aren't available. Is there a way to script or managed preference to keep messages open?

1 ACCEPTED SOLUTION

mm2270
Legendary Contributor III

Hmm, I wasn't aware of the -g flag for the open command. Thanks for pointing that out @JRM][/url][/url :)

FWIW, I recently discovered that Applescript has a similar function. Instead of using 'activate' use 'run' to make an application launch in the background.

tell application "Messages" to run

or

osascript -e 'tell application "Messages" to run'

Incidentally this actually works somewhat better, as it doesn't cause the applications windows to appear, even in Mission Control. The app icon shows in the Command-Tab switcher and in the Dock, but the app window shows up nowhere until its clicked on or switched to.

Another note - you can avoid worrying about the grep picking itself up in the results if you use ps axc. That outputs a neat list of just the application names, based on the Finder display name and not the full path. It also for whatever reason avoids needing to use grep -v grep or other tricks to avoid grep picking up its own results.
So combining these 2 together, you could also try this-

if [ `ps axc | grep -c Messages` == 0 ]; then osascript -e 'tell application "Messages" to run'; fi

Though it didn't occur to me that users may simply be able to set themselves to unavailable or offline if they choose not to be bothered by IMs. Ultimately this seems more like a company policy issue that might need attention from someone in HR or whatnot, rather than a technical problem.

View solution in original post

4 REPLIES 4

mm2270
Legendary Contributor III

No, to my knowledge there's no easy Terminal command to do this, or easy way to make this happen… other than perhaps a user level LaunchAgent that runs every 10-15 seconds, checks the process list and if Message.app isn't running, launches it? That might work, but seems kludgy to me.

Maybe someone else will have a better idea than that.

JRM
Contributor

Though unless there is some policy that they have to run this, users may be closing it on purpose. They may not like using/receiving IM's. You might find them offline due to just setting themselves offline while cursing the computer for keeping the application open.

If you are going to force the program to be always open, I would agree that the use of a user level LaunchAgent that runs periodically would be the best choice. It would be my personal choice to extend the time to more like 30-60 seconds.

I would use the -g switch so as to not foreground the application on them. It will reopen their contact list(s) window in the background still. In my testing I had to put the -g switch before the -a switch or it was looking to open a file instead of the application. In my example I am using grep to count how many matches it finds from the output of ps -e. Since it will always find itself running when checking against output from ps -e you should have 2 matches if it is already running and 1 match if it is not running.

if [ `ps -e | grep -c /Applications/Messages` == 1 ]; then open -ga messages; fi

mm2270
Legendary Contributor III

Hmm, I wasn't aware of the -g flag for the open command. Thanks for pointing that out @JRM][/url][/url :)

FWIW, I recently discovered that Applescript has a similar function. Instead of using 'activate' use 'run' to make an application launch in the background.

tell application "Messages" to run

or

osascript -e 'tell application "Messages" to run'

Incidentally this actually works somewhat better, as it doesn't cause the applications windows to appear, even in Mission Control. The app icon shows in the Command-Tab switcher and in the Dock, but the app window shows up nowhere until its clicked on or switched to.

Another note - you can avoid worrying about the grep picking itself up in the results if you use ps axc. That outputs a neat list of just the application names, based on the Finder display name and not the full path. It also for whatever reason avoids needing to use grep -v grep or other tricks to avoid grep picking up its own results.
So combining these 2 together, you could also try this-

if [ `ps axc | grep -c Messages` == 0 ]; then osascript -e 'tell application "Messages" to run'; fi

Though it didn't occur to me that users may simply be able to set themselves to unavailable or offline if they choose not to be bothered by IMs. Ultimately this seems more like a company policy issue that might need attention from someone in HR or whatnot, rather than a technical problem.

Lotusshaney
Contributor II

LaunchD item with keep alive set ?

https://github.com/tjluoma/launchd-keepalive