Automator script for restarting an application minimised without AppleScript
As you stated "I created a calendar automator script that runs every other night that restarts the application, but that brings the application to the front", I assume you have the parts of the workflow in place that closes your application on a timed-basis.
It sounds like the part you need solving is how to open the application after it's closed, so that it does not appear in the foreground.
Using a Run AppleScript action (deleting any and all sample code that may be present):
ignoring application responses
tell the application named "..."
if it is not running then run
end tell
end ignoring
You should make sure that the part of your workflow that shuts down the application has completed before moving onto attempt a relaunch. If the above script executes while the application is in the process of shutting down, it will not do anything.
For completeness, here's an example script that would handle the closing and relaunching of an application in the background. It would be run using a Run Shell Script action, and assumes you have zsh
installed:
#!/usr/bin/env zsh
bgreopen () {
pkill -xi "$*" &&
osascript <<-OSA
ignoring application responses
tell application "$*" to ¬
if it is not running ¬
then run it
end ignoring
OSA
} &>/dev/null
bgreopen Microsoft Teams
Replace "Microsoft Teams"
with the name of your application. It uses osascript
to execute the AppleScript from the earlier part of this answer, partly to demonstrate its incorporation into a larger solution, and partly because it's very reliable at launching apps silently.
The shell command open
has an option flag -j
used to launch an app hidden. It has been slightly less reliable at abiding the request to remain hidden in the past, but this was in an earlier version of the OS. If you want to give this a try instead, it would actually be a more ideal method as it keeps everything executing in the same language rather than calling in and out to other places. It's also a much more elegant solution:
#!/usr/bin/env zsh
(){ pkill -ix "$*" &&
open -ja "$*"
} &>/dev/null Microsoft Teams
Additionally, open
has a -g
option flag, which specifically stops apps being brought to the foreground upon launch. Used independently of -j
, it would open the app behind whatever you're working on, but not necessarily be hidden. The -j
flag ought to imply -g
as well, but perhaps it doesn't and explains my varying mileage with it in the past. So, you can experiment to see what works for you, and you can combine their use together with: open -jga "$*"
(just make sure a
is the last of the option flags immediately before the "$*"
argument that represents the name of the application).
If you wanted to optimise efficiency of this process, you can google launchd
. It's the macOS launch demon that regulates execution of programs on a schedule, and would allow you to remove Automator and calendar alarms from the equation. It would only require a .plist
file that defines how often you want to run a script, and the script body or path to a script file, which would simply be the shell script above.