Tuesday, 31 December 2019

macos - Global keyboard shortcut to open a tab in Chrome on Mac OS X


What is the best way to configure a global keyboard shortcut to open a tab in Chrome on Mac OS X?


Caveats



  • It should be as simple as possible, so I don't want to + every time.

  • This must be global, so if I press my shortcut (which happens to be ) while I'm in Mail.app, it needs to bring Chrome to the front and open a new tab

  • It needs to open the new tab page, i.e. chrome://newtab, not a website like http://www.google.com

  • The highest priority is that it opens fast fast fast, the next priority is ease of configuration


Current Solution


Right now I've got Quicksilver.app configured to execute the following AppleScript (or osascript to be precise) whenever I press :


#!/usr/bin/osascript

tell application "Google Chrome Canary"
activate
end tell

tell application "System Events"
tell process "Google Chrome Canary"
tell menu bar 1
tell menu bar item "File"
tell menu "File"
click menu item "New Tab"
end tell
end tell
end tell
end tell
end tell

The problem with this is that it can take up to 5 seconds if it has been a while since I ran it last. I had complied this into a *.app, but that was slower than making it an executable osascript. I'm not afraid to try developing a compiled version of the above script, but I'm a UNIX/web developer, not OS X, and I'm not familiar with the system.



Answer



You can run the following Terminal command (e.g. in a bash script):


open -a "Google Chrome" chrome://newtab

This will cause the application Google Chrome to open the URL chrome://newtab, thereby opening a new tab.


Unfortunately, Google Chrome doesn't register the chrome:// URL type with Launch Services, and it will think that's a file path.


To fix this, right-click Google Chrome's application bundle, select Show Package Contents, open the Contents directory and edit Info.plist in a text editor.


Search for CFBundleURLTypes. Edit the following few lines to add the lines indicated by a +:


CFBundleURLTypes

+
+ CFBundleURLName
+ Chrome Internal URLs
+ CFBundleURLSchemes
+
+ chrome
+

+


CFBundleURLName
Web site URL

Save and close. Move Google Chrome's application bundle to a different directory, and back again, to make Launch Services pick up the change (if it doesn't work, log out and back in).


Then, run open like described at the beginning.




Once this works, your best option is to run a shell script that performs the open command, which in turn is invoked e.g. by your launcher. Since I believe the delay is caused by osascript loading, pretty much any solution you choose here should be fast enough.




To semi-automate the editing of the Info.plist file (you have to repeat this for all Chrome updates), you can use PlistBuddy in Terminal. First, create a file e.g. named chrome-url.plist with the following content:







CFBundleURLName
Chrome Internal URLs
CFBundleURLSchemes

chrome





Then, you can use the following to patch Chrome's Info.plist:



/usr/libexec/PlistBuddy -c "Merge '/path/to/chrome-url.plist' :CFBundleURLTypes" /Applications/Google\ Chrome.app/Contents/Info.plist



No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...