1

Suppose I have a file ~/Desktop/foo:bar.webloc.

(a) If I double-click it, it opens a particular web page. Great!

(b) If in the Terminal I run open ~/Desktop/foo:bar.webloc, it opens the webpage. Great!

(c) If in the Terminal I run cd Desktop and then open foo:bar.webloc, I am greeted with

% open foo:bar.webloc 
foo:bar.webloc?
[0] cancel
[1] Open the file foo:bar.webloc
[2] Open the URL  foo:bar.webloc

Which did you mean?

I'm guessing that open thinks that the filename might be a URL. Is there some way to force open to open the file by default if the file exists, even if the file name matches a pattern that could conceivably be a URL? Kind of the opposite of open -u? Do I just need to avoid colons in filenames?

This arose because Firefox created a filename containing a colon when I dragged from the URL bar to the desktop, and then I tried to open it from the Terminal. Maybe another question is: is there a way to automatically get colon-free filenames when creating webloc files? (If you want to test, you can use the URL https://github.com/sagemath/trac-to-github/blob/master/docs/Migration-Trac-to-Github.md. Safari and Chrome produce colon-free filenames, but not Firefox.)

Edit: based on case (b), a partial workaround is to use open ./foo:bar.webloc.

0

2 Answers 2

3

I'm guessing that open thinks that the filename might be a URL.

You would be correct in that guess.

Unix/BSD/Linux/etc. (macOS is a certified Unix) don’t use characters for filenames. Instead, it’s an array of bytes. With the exception of a few characters like the Null character (0x00) and forward slash (0x2f - /) pretty much everything else is fair game including colons. In short, you can have colons in your filename and it be valid.

See this excellent post on Unix & Linux entitled Understanding Unix File Name Encoding for additional details.

Is there some way to force open to open the file by default if the file exists,

Unfortunately, no. Your operating system (macOS in this case) cannot tell the difference between a filename with a colon or a URL with a colon.

Why?

The problem here is that per the URL Specification (RFC-1738), §2.1, The main parts of URLs define what a URL is supposed to look like…

A URL contains the name of the scheme being used () followed by a colon and then a string (the ) whose interpretation depends on the scheme.

Emphasis Mine

if the file name matches a pattern that could conceivably be a URL?

A file is a file; that’s easy. A URL on the other hand, can be many things, including a file. See RFC-1738 §3.10 Files:

The file URL scheme is used to designate files accessible on a particular host computer. This scheme, unlike most other URL schemes, does not designate a resource that is universally accessible over the Internet.

A file URL can take the form of (VAX/VMS):

DISK$USER:[SHARE]FOO.BAR
         ꜛ
          Note the colon!

That’s a perfectly valid file and in this case, would be remote. However, due to how Unix allows every single one of the those characters to exist in a filename, the question (for the OS) is “how to handle it?” Open as a URL or open as a file? This is why you get the prompt.

TL;DR

You can’t force open to open a target as a file when the argument can be one of many things. In this case, a file with a colon could be either URL or it could be a file; thus the prompt you are seeing.

This is due to the “permissible characters” (byte arrays, actually) that make up a filename on Unix as well as the confusion created behind what a URL is defined as.

As for a “reverse” of open -u, well, that just doesn’t exist as an argument for the open command.

2
  • It seems odd that there is open -u which forces open to view the argument as a URL, even if the argument also matches a filename, but there is no complementary flag to force it to choose the filename. May 3, 2023 at 18:44
  • Note that on OS X, apparently in contrast with VAX/VMS, foo$bar:baz.webloc is viewed as a filename rather than a URL, I think because $ is not an allowed character in a scheme name. May 3, 2023 at 23:02
5

The current directory can be referenced using the directory name .. So use

open ./foo:bar.webloc

This is not a valid URL, so it's treated as a filename.

This is also a common way to deal with filenames that begin with -, which might otherwise be interpreted as command options.

4
  • Right, I added that as an edit to my post a few hours ago. May 4, 2023 at 0:12
  • Sorry, didn't notice that until after I posted my answer. So I added the additional part about filenames with -.
    – Barmar
    May 4, 2023 at 0:13
  • You could of course have posted that as an answer yourself. Solutions are supposed to be in answers, not the question itself.
    – Barmar
    May 4, 2023 at 0:13
  • I'm sorry about that. It's not actually a useful answer for me, just a workaround, and I didn't think to post it as an answer: I really want to be able to use this in Emacs, in its dired mode: basically you choose a file, hit a key, and run a command on it. Typically open works, but typing in open followed by the full filename is not practical. I could do some emacs-lisp hacking to take care of it, but to be honest, it's a rare situation and probably easiest to just rename the file to avoid colons. May 4, 2023 at 2:43

You must log in to answer this question.

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