Macos – Mac Command Line Open PDF to Specific Page


The goal is to use command line to open a PDF file to a specific page. The more general question is how do I find out what args are available for any given app? I am able to open a pdf file in Mac OS X command line with the open command, and knows that you can pass args, but couldn't figure out the exact args to put to open the PDF file to page 5.

Tried the following blindly, and didn't work…

open file.pdf --args -page=5
open file.pdf --args -p 5
open file.pdf --args -page 5

Have seen this question, but the solution requires installing evince or write Apple Script. Is there no easy solution in just passing –args in Mac just like in Windows command line?

Best Answer

I had no idea that all Windows applications understood command line arguments.

Whether or not that's the case, it is not the case on Mac OS. When you are using the Terminal application, you are interfacing with a BSD subsystem that follows all the patterns you would expect from a UNIX or Linux operating system.

There are plentiful resources out there to get familiar with the bash shell and *NIX command line. To get you started, the man command will get you the manpage for whatever command you pass in. Generally all space-separated "words" you type after a command are treated as arguments for that command, at least until you hit some kind of output redirection or control character like |, <, >, &.

So in your example, you were calling open and passing in file.pdf, --args, and -page=5 as arguments. As you've observed, open doesn't know what to do with most of these.

Anyway, let's dig deeper. Most applications (like in this case) have an executable file somewhere in their application bundle. You can actually execute this (/Applications/ to start Preview directly without going through open, but only certain purpose-built executables will understand command-line arguments. Preview is not one of them.

The man command only works for commands that have had manpages installed, and these will typically only be commands in your shell's $PATH variable. For other executables, (and usually for standard ones as well), you can try calling the executable with a --help argument to get usage information whether or not it is in your path. This won't work for everything, but it's a common pattern.

So there may be a PDF reader out there whose executable understands command-line arguments, but if we're still working with Preview, we need to shift into a different paradigm--AppleScript. You can read all about it on Wikipedia, but the basics of it is that it's a natural language scripting language that allows you to programmatically send Apple Events to applications. Some applications implement extensive AppleScript libraries to allow for greater automation.

The good news here is that you can process AppleScript on the command line with osascript. Even more good news is that people have already solved your AppleScript problem of getting Preview to flip to a page in a PDF here:

So what your approach should be here to solve your problem is:

  1. Devise a terminal command to open your PDF in Preview open file.pdf should work fine.
  2. Test an AppleScript to change the page of that PDF to the one you specify (use Script
  3. Learn enough about the command line to devise a statement that will execute both steps in one go.