15

I know that I can open a file with an external program from the Terminal with open, and that works for most applications. However, when I try to use a command like open -a Preview info.txt, it launches Preview, but the file is not opened.

I am on Mavericks 10.10.2.

Is there a way to use the open command (or any other built-in command) to actually open a file in Preview from the Terminal?

1
  • 1
    Preview cannot open text files, but the command open -a Preview photo.jpg works for me, it opens the image in Preview.
    – Lyes
    Apr 28, 2015 at 2:55

4 Answers 4

14

Preview only works with PDF files and some image files (png, jpg, gif, tiff, bmp) so you won't be able to open a text file on Preview.

To open your info.txt file from terminal you need to choose an application that can open text files, like TextEdit or any other text editor.

You also could use open -e file, to open any file using TextEdit.

3
  • 1
    If you don't want to risk editing the file, try open -a Safari info.txt.
    – lhf
    Apr 28, 2015 at 10:57
  • 1
    I don't think the question was about opening a text file. There is still the question how to open a pdf file in Preview from the terminal?. You failed to answer that question.
    – Walter
    Jul 20, 2015 at 13:24
  • 1
    There is no question on 'how to open a pdf file in preview', as the user stated on his example, he already knew how to open files in Preview from the terminal but didn't understood why it didn't worked when trying to open a txt file. I just answered explaining what kind of files you can open with Preview and how to open any file from the terminal using the default text editor. Also, other users answered the same, using another example with a image file. But you've got a point, I could have had answered in a better way.
    – leandrojmp
    Jul 21, 2015 at 2:48
7

NB - If there are any further questions about this answer please comment.

UPDATE

There's nothing wrong with the other answers, this one leverages a system command to convert the text file to a compatible filetype for Preview (i.e. PDF).

Given a sample file you can run:

cupsfilter info.txt > info.pdf

(to hide the debug output use cupsfilter info.txt > info.pdf 2> /dev/null)

After which one may apply the original answer to open the new info.pdf file in Preview. You can learn more by running man cupsfilter. I believe this just exposes the basic Save As PDF functionality that exists in the CUPS print system.

(Source)

Additionally

As noted in the comments one can simply pipe the command to open a file directly into Preview. This worked for me:

cupsfilter info.txt 2> /dev/null | open -f -a Preview

(Original Answer)

To open a supported Preview file from Terminal, such as pdf, png, jpg, gif, tiff, bmp:

open -a Preview <nameOfSupportedFileType>

So for example:

open -a Preview [email protected]

Opens the png from the current folder in Preview.

1
  • 1
    Nice! I tried cupsfilter but it didn't occur to me to redirect stderr (doh!). With the redirect you can pipe stdout directly to open ( cupsfilter info.txt 2> /dev/null | open -f -a /Applications/Preview.app )
    – John N
    Dec 7, 2016 at 20:32
2

There is, but you need to convert the file to postscript or PDF first. For example, I have a function pman which works exactly like regular man, but opens the man page in Preview.app:

pman () 
{ 
    man -t $* | open -f -a /Applications/Preview.app
}

The -t option tells man that the output needs to be formatted (using groff) as postscript.

You want to open a text file in Preview.app. For the "convert to PDF" stage I use paps, which I installed using brew:

brew install paps

After that, it's easy!

paps info.txt | open -f -a /Applications/Preview.app

That'll open info.txt in Preview. If you do this a lot, you'll probably want to create a function (in your ~/.bash_profile or similar):

preview ()
{
    if [ -z "$*" ]; then
        echo "Usage: preview [FILE]"
    else
        paps $1 | open -f -a /Applications/Preview.app
    fi
}
complete -f -X '!*.txt' preview
1

QuickLook scans file contents before you open those files. Usually this just lets you view a file quickly. But you can also use this same technology from the command line to bring about a change to the Finder without actually opening a file. To access QuickLook from the command line, use qlmanage.

qlmanage -p ~/Desktop/MyTowel42.pdf

— Charles Edge

You can add following function to ~/.bash_aliases:

preview () { qlmanage -p "$*" >& /dev/null; }

Found here: https://clburlison.com/preview-files-from-terminal/

1
  • This is the current best answer. What a useful alias!
    – Gillfish
    Aug 19, 2022 at 17:36

You must log in to answer this question.

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