0

How do I get an Automator script to restart an application and make it minimised after restart? The application in question has no support for AppleScript.

I have an application (Setapp version of MoneyWiz) with the very annoying feature that if it is not started at least once a week it will delete all its saved files in the cloud. Continuously running the application does not count, it has to be (re)started.

I created a calendar automator script that runs every other night that restarts the application, but that brings the application to the front, and I want it to be launched in the background. Due to the application's lack of AppleScript I cannot use the
tell application [...] set miniaturized
which is the common solution I find when I search the web for solutions.

1
  • 3
    Which app is it it might be better to see if we can suggest a fix. The issue sounds very odd
    – mmmmmm
    Commented Nov 24, 2020 at 9:26

1 Answer 1

2

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.

4
  • @user3439894 I believe that the -j flag implies the -g flag, although it's not documented as such, or at least It doesn't seem to bring a hidden app-launch into the foreground. But it's a good one to note in case it does steal the menubar in some situations. I'll add it to the answer. Thanks.
    – CJK
    Commented Nov 24, 2020 at 19:00
  • The Apple script parted worked for me. I got an error (can't recall exactly what right now) using the zsh scripts, but as the Apple script part worked I didn't spend any time troubleshooting. Commented Nov 25, 2020 at 22:55
  • Assuming you were usin¡g (or selected to have Automator use) zsh and not bash, then the errors would have been typographical, as &>/dev/null prevents reporting of any and all errors during execution, meaning it was generated pre-execution as a result from a parser error. The likely candidate will be the white space at the beginning of the line in bgreopen whose only other characters are OSA. I entered all lines that start with white space making their very first character
    – CJK
    Commented Nov 26, 2020 at 0:43
  • ...a tab character.
    – CJK
    Commented Nov 30, 2020 at 8:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .