5

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)

5

A non-specific approach is:

 osascript -e 'open location "file:///Users/me/index.html#my-anchor"'
  • That is what we need! – ocodo Jul 24 '15 at 13:57
  • 1
    Does not work for Chrome unfortunately. – Timmmm Jun 27 '18 at 15:30
7

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"'
  • To be very clear the question is to open the file url via terminal / scripting. The title is updated. – ocodo Jul 7 '15 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 Jul 7 '15 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 Jul 7 '15 at 16:54
  • √ Done. What do you need this for? – loco.loop Jul 7 '15 at 17:02
  • Opening downloaded docs from Vim and Emacs and jumping to the relevant item. Noticed open fails to pass the hash. – ocodo Jul 7 '15 at 17:04
2

Taking a stab at narrowing this down: I was getting cryptic errors [1] from this command, and only inconsistently:

osascript -e 'open location "file:///Users/me/index.html#my-anchor"'

Weirdly, that command breaks on file paths containing spaces only if the file path with spaces additionally contains an anchor.

I had no luck with backslash-escaping and quoting the problematic path (more like "'\"Application\\ Sabotage\"'", am I right?), but percent-encoding the spaces to %20 avoided the issue.


[1]: at first, I was only seeing:

execution error: File some object wasn’t found. (-43)

but some attempts at escaping eventually gave me the slightly more helpful:

syntax error: Expected “"” but found unknown token. (-2741)

  • percent-encoding the spaces to %20 - the whole path should be percent encoded to handle legal path chars that break urls. – ocodo Mar 18 '18 at 0:33

You must log in to answer this question.

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