0

I am trying to open a file on an smb share in preview from terminal. The following command opens the containing folder in finder instead of the file in preview.

open -a /Applications/Preview.app "smb://myshare._smb._tcp.local/path/to/file/my_file.pdf"

Update 1: I tried with and without the -a switch. The same thing happens in both cases: finder is opened with the last folder (/path/to/file). Preview is already associated with PDFs, so it should open automatically.

3
  • what does happen when you just try to run this command: /Applications/Preview.app ? does it open the Preview App ?
    – harlock59
    Commented Nov 5, 2014 at 9:26
  • Is the SMB share mounted in /Volumes/myshare? As a workaround you could try open -a /Applications/Preview.app /Volumes/myshare/path/to/file/my_file.pdf instead.
    – jaume
    Commented Nov 5, 2014 at 9:32
  • @harlock59 You cannot run applications like that - it gives error that Applications/Preview.app: is a directory. @jaume - I want it to work when the share is not mounted - that is why I am passing the server path.
    – Anand
    Commented Nov 5, 2014 at 13:42

4 Answers 4

1

Just use /Applications/Preview.app/Contents/MacOS/Preview /path/to/file.pdf instead of /Applications/Preview.app /path/to/file.pdf and it should work.

1
  • That doesn't work either - it does seem to launch the app if not open, but it doesn't open the file in the app and opens just the folder instead.
    – Anand
    Commented Nov 5, 2014 at 13:44
1

1.) Your situation is not related to the open command. Before one can pass a remote share filepath as part of an argument, the share must first be established at a mount point. Here's an illustration to show what happens--or doesn't happen--using the simplest of commands:

ls smb://myshare._smb._tcp.local/path/to/file/my_file.pdf

Indeed, referencing a file in the manner you described--invoking its path via a mount request--causes that path to become a mount point. The Finder window you mentioned seeing:

... finder is opened with the last folder (/path/to/file) ...

represents the last known directory the system could use to create a mountable share. Run the command as you did, and look in /Volumes to see what I mean. It's likely not the share name you're used to seeing on that machine.

2.) When using open as a command to launch an applications which is present in /Applications (or in ~/Applications), it's not necessary to provide its full path or its extension. Although the man pages would seem to indicate otherwise from their presentation, the command open -a Preview is equivalent to open -a /Applications/Preview.app

3.) For that matter, if launch services associates a particular filetype with a default application--as with .pdfs and other image files with Preview, for example--it is not necessary to include the application name in the argument. The command open /path/to/my_file.pdf is equivalent to open -a Preview /path/to/my_file.pdf

4.) I'm not all that clever when it comes to UNIX, so my following attempt at a solution will no doubt soon be improved by someone who has the skills I lack.

I'll start with the simplest way I know to mount a remote filesystem (it doesn't require creating a /dev node as with mount or creating a mount point directory as with mount_smbfs).

open smb://myshare._smb._tcp.local/share_name/ (quotes aren't necessary)

The share will be mounted in /Volumes, and you can open the file in Preview with this command:

open /Volumes/share_name/path_to_file/my_file.pdf

The two commands can be combined into a one-liner with &&:

open smb://myshare._smb._tcp.local/share_name/ && open /Volumes/share_name/path_to_file/my_file.pdf

I tested all of the above [OS X 10.9.5], and unless I made a typo somewhere it should work. Good luck.

0

Assuming that you have USERNAME and PASSWORD as credentials to connect to the myshare._smb._tcp.local, you can use curl in a shell to read contents of the file and pass it through a pipe to the Preview application:

$ curl smb://USERNAME:PASSWORD@myshare._smb._tcp.local/path/to/file/my_file.pdf | open -f -a Preview

If you have a POSIX shell (e.g. bash, zsh, ... [NOT fish]) you can use redirect process substitution output to Preview's input:

$ open -f -a Preview < <(curl smb://USERNAME:PASSWORD@myshare._smb._tcp.local/path/to/file/my_file.pdf)

And If you want to use a named pipe:

$ mkfifo /tmp/my_pipe
$ curl smb://USERNAME:PASSWORD@myshare._smb._tcp.local/path/to/file/my_file.pdf > /tmp/my_pipe & open -f -a Preview < /tmp/my_pipe
$ rm /tmp/my_pipe
-1

didn't you just try "open -a Preview /path-to-your-file.pdf"

3

You must log in to answer this question.

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