7

I've been trying to open a file:/// url e.g.

open "file:///Users/me/index.html#my-anchor"

Or

open -a Safari "file:///Users/me/index.html#my-anchor"

Or

open -a Google\ Chrome "file:///Users/me/index.html#my-anchor"

However, the browser location after opening is:

file:///Users/me/index.html

The hash/anchor has been clobbered. Is there some way to do this that I'm missing?

(Note: Obviously this isn't what happens with URL's on web servers)

2 Answers 2

5

A non-specific approach is:

 osascript -e 'open location "file:///Users/me/index.html#my-anchor"'
3
  • That is what we need!
    – ocodo
    Commented Jul 24, 2015 at 13:57
  • 1
    Does not work for Chrome unfortunately.
    – Timmmm
    Commented Jun 27, 2018 at 15:30
  • Doesn't work for me with Safari as default browser either (on macOS 10.14.6). The version where a specific browser is specified does work, however.
    – Max Horn
    Commented Aug 5, 2020 at 12:13
8

Don't use the open command.

Google Chrome:

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome "file:///Users/me/index.html#my-anchor"

Safari:

osascript -e 'tell application "Safari" to open location "file:///Users/me/index.html#my-anchor2"'
7
  • To be very clear the question is to open the file url via terminal / scripting. The title is updated.
    – ocodo
    Commented Jul 7, 2015 at 15:38
  • I just changes my answer to what you need. Try the above commands in the terminal, I just did and they worked as expected.
    – loco.loop
    Commented Jul 7, 2015 at 16:31
  • Not sure who else down voted you. Please remove the initial answer from your post, that was never the intended question. I will mark as correct when the noise is removed.
    – ocodo
    Commented Jul 7, 2015 at 16:54
  • √ Done. What do you need this for?
    – loco.loop
    Commented Jul 7, 2015 at 17:02
  • Opening downloaded docs from Vim and Emacs and jumping to the relevant item. Noticed open fails to pass the hash.
    – ocodo
    Commented Jul 7, 2015 at 17:04

You must log in to answer this question.

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