3

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

  • Do You have Xcode to recompile some sources? – Mateusz Szlosek Sep 8 '14 at 22:02
  • You can just switch it in Safari preferences. – Ruskes Sep 9 '14 at 2:09
  • 1
    I'm looking for an automated solution that does not require clicking buttons. – spuder Sep 9 '14 at 5:10
  • Check the comments for this answer. A user build his own app to do just that. You could launch it with a bit of script code. It appears to be for 10.9, will not run on my 10.6 :( HTH – Vic Sep 9 '14 at 9:26
6

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.

  • How do you set it to specific times? – Ruskes Sep 9 '14 at 19:56
  • You don't, see edit. I would use a event based scheduler instead. – Vic Sep 11 '14 at 11:57
  • +1, but I'd change the logic in if(s == ... statement. I know this should not happen to be able to change only one of http/https, but You never know ;) – Mateusz Szlosek Sep 11 '14 at 12:02
4

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

1

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.

  • 1
    I'd compile "defaultbrowser" from sources with addition to handle https prefix as well. Right now it handles only http. That's why I've asked, if You can compile things in Xcode... – Mateusz Szlosek Sep 10 '14 at 8:06
  • Yes you're right. I updated my answer with the changes needed and it seems to work ok for both cases now. – Vic Sep 11 '14 at 11:55
  • I was surprised to see EDITOR=NANO actually works, but of course, the macOS default filesystem ignores file name case. You might prefer a different editor than nano even if you are a beginner. – tripleee Mar 21 '17 at 4:22
  • See the link in the answer. There is a bug/quirk when using vim to edit cron on OSX. Nano is not affected. – spuder Mar 21 '17 at 5:42
  • 1
    Apple introduced another dialog recently where the user has to confirm the actual switching. Here is an Apple Script that can be run with osascript. Oh and maybe look into ControlPlane as a replacement for this cron stuff, and it comes with its own "Changing Default Browser" functionality. – mike wyatt Jan 25 '18 at 18:20

You must log in to answer this question.

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