OSX/launchctl: How do I open a specific website in a new instance of chrome in the foreground every morning?

1

Requirements:

  1. Specific website
  2. Every morning despite laptop being closed overnight (launchctl can handle this)
  3. Foreground - must come to my attention, even if I have multiple spaces/desktops
  4. New instance of chrome (not necessary, but preferable)

When I try open http://superuser.com in a desktop/space that doesn't have Chrome already open, I see it fail the cases, 3. & 4.. What happens is that a tab opens within an existing Chrome instance in another space/desktop quietly in the background.

barrrista

Posted 2015-10-14T04:31:33.017

Reputation: 1 519

1Trying to get that fanatic badge eh? ;) – Insane – 2015-10-14T04:42:36.147

Other than bringing it up on the foreground, my request isn't too crazy. I just wanted to specify all my requirements ahead of time to be clear. – barrrista – 2015-10-14T04:44:27.080

Answers

0

Here's what I came up with. Was simpler than I thought.

Create shell script daily_goals.sh:

#!/bin/bash

# this is needed to launch Chrome as a new window. Since it's a new window, it will open in the foreground in current space/desktop. 
open -n -a Google\ Chrome 

# this sleeps for 1 second. It's necessary because otherwise the website, called below, will open in existing Chrome window, which may be located in another desktop. I think sleeping lets the next `open` call find the newly opened Chrome window, and replace the new tab with the provided website. 
sleep 1 

# the website I provided. 
open http://joesgoals.com 

Create /Library/LaunchDaemons/daily_goals.plist:

<?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>daily_goals</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/user/Work/daily_goals.sh</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>07</integer>
        <key>Minute</key>
        <integer>00</integer>
    </dict>
</dict>
</plist>

Add to launchctl:

launchctl load -w /Library/LaunchDaemons/daily_goals.plist

In summary, this launches joesgoals.com every morning at 7am in a newly opened Chrome instance. If the laptop is asleep at 7am, it should open JoesGoals when the laptop is resumed from sleep. I'll update later if I see any quirks resuming osx in different desktops/spaces (with or without Chrome). Hoping it won't be an issue.

barrrista

Posted 2015-10-14T04:31:33.017

Reputation: 1 519