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?

  • 1
    You are calling the open command. To see what arguments it can accept, type man open. – NReilingh Apr 7 '15 at 22:35
  • Thanks for the comment, but could you pls elaborate a bit more? I thought man open gives the args for the command open, which all go before the file name, e.g.-a to specify the application. But I thought I need to call `--args' to pass some sort of a page parameter to the PDF reader itself in this case. I don't know what args I could pass? If you could give an example that'd be much appreciated.. – tomato Apr 7 '15 at 22:41

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 Preview.app in this case) have an executable file somewhere in their application bundle. You can actually execute this (/Applications/Preview.app/Contents/MacOS/Preview) 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: https://discussions.apple.com/thread/3215851

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 Editor.app).
  3. Learn enough about the command line to devise a statement that will execute both steps in one go.
| improve this answer | |
  • This is still a solution that uses AppleScript. But it confirms that you can't do a simple command line thing.. thanks! – tomato Apr 18 '15 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.