Search code examples
macosapplescriptmacos-catalinaautomator

Applescript or Automator behaves differently at night?


I have a simple Automator app to launch an app at midnight, and then am automator worksflow to quit at 6am. Both are run through the calendar. Launching works fine. I'm having problems quitting, but only at night.

If I run the quitting script now, it works fine. If I run the script via setting the calendar to a few minutes from now, it works fine. But when set for 6am, I check in the morning, its not worked.

My system is a Mac Mini macOS Catalina. The app is Bittorent Web. Bittorrent Web launches a small app that then launches Safari. So for my 'quit' workflow, I first quit Safari using an Automator function. Next I use a script to quit Bittorrent because Bittorrent asks if you're sure you want to quit. The script brings bittorent to the front, runs command-q, then hits return. This part doesn't work at night.

The machine is set to not sleep. The automator and the automator app has full permissions to run scripts - as I say if I were to run it now it would work. So i'm lost. The script is;

on run {input, parameters}
  tell application "System Events"
    set frontmost of process "BitTorrent Web" to true
     keystroke "q" using command down
     keystroke return
  end tell
end run

Hope this makes sense. As I said, this works now, but in the mornings I find that Safari is closed, but BitTorrent is still running - no errors, and the "do you want really want to quit" dialog box is not their either. Thanks


Solution

  • A better and more reliable approach is

    on run {input, parameters}
      if application "BitTorrent Web" is running then quit application "BitTorrent Web"
    end run
    

    Alternatively address the process explicitly (the process name might be different)

    on run {input, parameters}
      activate application “BitTorrent Web”
      tell application "System Events"
         tell process “BitTorrent Web”
             keystroke "q" using command down
             keystroke return
         end tell
      end tell
    end run