0

I want to be able to get all the pdf files currently opened by my Preview app into Terminal. The idea is, I want to be able to feed the output to a text file for future reference.

My use case may be satisfied without involving any direct confrontation with the Preview app itself. I might be able to get the data from console or doing a ps command from terminal.

As of now all such attempts of mine haven't gotten me anywhere. Please let me know how to achieve this task.

2
  • 1
    What do you mean by 'output'? Do you mean the PDF data stream, or the text of the PDF? What's your ultimate goal? What do you want to do with the data? When you say 'ps command', do you mean THE ps command?
    – benwiggy
    Jun 25 at 20:52
  • By output, I mean the name and the possibly the location of the pdf file opened. My ultimate goal, is to be able to keep a record of those pdfs in a file for later purposes. I am an academic, I at most times through a particular day, download a lot of "papers"/pdfs from the web that I may or might not read at that exact moment. If there was a way to get these informations on the terminal by some command, then I would place the data in a file or future use purposes. And, yes its, The ps command, it technically should give me all the process that are running right? (including Preview)
    – Ramit
    Jun 26 at 9:42

2 Answers 2

1

The following AppleScript will get the filepaths for all open documents in Preview:

tell application "Preview"
    set pathList to path of documents
    return pathList
end tell

You can use this in the shell like this:

osascript -e 'tell application "Preview"' -e 'set pathList to path of documents' -e 'return pathList' -e 'end tell'

Remove "path of" in the second line of the script to get just the filenames with no path.

6
  • Wow thanks! This indeed does the job! I had no idea that we could actually communicate with Preview in the above way! Any pointers on where I can learn more on osx programming of the above sort?
    – Ramit
    Jun 28 at 13:00
  • 1
    @Ramit There are loads of Apple documentation for AppleScript, as well as third-party support pages and examples. Personally, I find AppleScript's "Englishyness" an obstacle -- you think it's going to be more flexible than it really is, and actually you have to be as precise as other langs.
    – benwiggy
    Jun 28 at 15:33
  • Thanks a ton! I will look into them. This worked for the most part, save the circumstances when say a jpg file is open via Preview. Is there a way to make the script list out explicitly only the pdf files?
    – Ramit
    Jun 29 at 11:23
  • 1
    You'd have to create a loop and go through each document, and then use an if condition to check for pdf before adding the filepath to the list. Only a few more lines.
    – benwiggy
    Jun 29 at 11:46
  • Got it thanks! :)
    – Ramit
    Jun 29 at 12:30
0

So I finally found a workflow haven't gotten around to bash scripting it yet. But the following lines of code gives me the desired solution.

lsof | ack Preview | ack pdf > tmp Then I'd edit tmp to get the format of data I wanted. Bottomline is lsof came to my rescue for this operation.

This is not quite correct, yet as we also get the pdfs that were open at an earlier time. What I ideally want is the pdfs currently open in Preview.

0

You must log in to answer this question.

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