How can I set the default browser depending on time of day?

I have a MacBook Pro from work. To keep my browsing history separate, I use 2 different browsers. (Chrome = Work, Safari = Fun)

I want to make my default browswer Chrome while at work (9 am to 6pm), and Safari my default browser in the evenings. This will prevent me from accidentially opening a link in the wrong browser.

How could I switch my default browser at predefined times throughout the day?

Update

There does not appear to be a 'set default browser' workflow in automator. Perhaps it would be possible to edit this file?

/Users/yourname/Library/Preferences/com.apple.LaunchServices.plist

https://discussions.apple.com/thread/2803775?tstart=0


Solution 1:

Not nearly as complicated, but perhaps of value, is the app Choosy. It allows you to choose your browser every time you open a link outside of a browser. While this might sound onerous, it's actually pretty transparent, and I find it very useful in the leadup to going live with a new web project and testing in multiple browsers over and over and over…

Might suit the bill if one added keystroke isn't a problem. Of course, once you're inside Safari, links clicked will open in Safari (although there's even a way to modify this behavior.)

FWIW.

Choosy

Solution 2:

Expanding on my comment above....


See the comment by user kerma at this related StackFlow article

His little command line app at https://github.com/kerma/defaultbrowser does what you need.

He provided the source code but it was designed for 10.9. A small change can make it to compile on 10.6 as well. I tested it on my system and it worked correctly.

All you then need to do is run it like this...

#defaultbrowser -set firefox

#defaultbrowser -set safari

The changes I made, to make it work with 10.6, were

a) Add the Cocoa framework

b) Make the following code change...

Replace

NSString *split = HTTPHandlers[i];

with

NSString *split = [HTTPHandlers objectAtIndex:i];

EDIT:

As was pointed out, the code does not change the default browser for HTTPS. To add that functionality, I added the following lines...

Find the first line below, and add the second one for https

CFStringRef urlschemeref = ( CFStringRef)@"http";
CFStringRef urlschemeref2 = ( CFStringRef)@"https";

and the same here

OSStatus s = LSSetDefaultHandlerForURLScheme(urlschemeref, newHandler);
OSStatus s2 = LSSetDefaultHandlerForURLScheme(urlschemeref2, newHandler);

Finally change

if (s == 0) {

to

if (s == 0 && s2 == 0) {

Regarding the scheduling, since they know what cron is, I assume they know how to make the necessary changes. If not, a google search would provide the necessary answers. So the cron lines would be...

0 9 0 0 1-5 /path/to/app/defaultbrowser -set chrome
0 18 0 0 1-5 /path/to/app/defaultbrowser -set safari

BUT, a much better way, because cron tasks will not run when system is powered down, is to have a means to detect location (like wifi connection) and change according to that. So if the cron job is set at 9am and you arrive late to work, you powerup your device at 9:15am nothing will happen...

So, as discussed here, I would recommend that they install ControlPlane or something like it, and schedule the change upon connection to the wifi/network instead.

Hope that helped.

Solution 3:

Based on Vic's answer, this is what I came up with.

Download defaultbrowser, create a cronjob to run at 9 am and 6pm. Because there is no gui, it will require taking advantage of the fact that mac is unix.

First checkout the source code of defaultbrowser, and copy it to /usr/local/bin

git clone https://github.com/kerma/defaultbrowser.git /tmp/defaultbrowser
sudo cp /tmp/defaultbrowser/build/defaultbrowser /usr/local/bin/
sudo chmod +x /usr/local/bin/defaultbrowser

Then to schedule the binary to be run at specific times, run the following:

EDITOR=NANO crontab -e

0 9 * * 1-5 /usr/local/bin/defaultbrowser -set chrome >> /Users/sowen/defaultbrowser.log 2>&1 && echo $(date) >> /Users/sowen/defaultbrowser.log 2>&1
0 18 * * 1-5 /usr/local/bin/defaultbrowser -set safari >> /Users/sowen/defaultbrowser.log 2>&1 && echo $(date) >> /Users/sowen/defaultbrowser.log 2>&1

Now my browser will change at 9 am and 6pm on weekdays.

Explanation:

EDITOR=NANO crontab -e edits the crontab file
0 9 * * 1-5 means on days 1-5 (monday - friday), run the following command at 9:00 /usr/local/bin/defaultbrowswer -set chrome sets the default browser
>> /Users/sowen/defaultbrowser.log appends to a file I call defaultbrowser.log in my home directory
2>&1 fancy syntax that sends both errors, and logs to the same place
&& combines two commands into 1
echo $(date) >> /Users/sowen/defaultbrowser.log 2>&1 Adds a timestamp to the log file

I had some problems creating the cronjob, I had to use nano based on this information.

Also, this could have been rewritten using launchd, however cron was easier.