Apple - Can I open a text file in Preview from the Terminal?
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.
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.
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