I'm trying to set up a function which will allow me to open offline Google docs. The issue is that they only work through Finder if Chrome is the default browser as the Google Drive app will open the file and this will then pass the desired URL to the default browser. As the offline docs can't open in Safari I need to either change my default browser (no thanks!) or copy and past the URL given to Safari into Chrome which can correctly open the file.

When running a cat of an offline Google doc/sheet an output such as the following is returned,

{"url": "https://docs.google.com/open?id=31234123fdasdfsg412341", "doc_id":"31234123fdasdfsg412341", "email": "xxxxxxxxxxx@googlemail.com", "resource_id": "document:31234123fdasdfsg412341"}

I have managed to use sed to extract the https part of this,

sed 's/.*\(https[^"]*\)".*/\1/' myfile.gsheet

An example of this is,

echo '{"url": "https://docs.google.com/open?id=31234123fdasdfsg412341", "doc_id": "31234123fdasdfsg412341", "email": "xxxxxxxxxxx@googlemail.com", "resource_id": "document:31234123fdasdfsg412341"}' | sed 's/.*\(https[^"]*\)".*/\1/'

However to open this in Chrome and actually view the file offline I now need to pass this to,

open -a 'Google Chrome' <sed_output>

Finally, the trickier part (!), I need to make this entire process a function so I can set an alias and type,

chromeopen myfile.gsheet

and have it extract the url from that file and pass it to open.



Using mmusante's suggestion I created an alias function to perform this action,

chromeopenfunc() {
    open -a 'Google Chrome' "$(sed 's/.*\(https[^"]*\)".*/\1/' $1)"
alias chrome=chromeopenfunc

You can put the sed output directly on the command line using the $() operator. e.g. open -a 'Google Chrome' "$(sed 'blah blah blah')"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.