macOS Preview – lsof Does Not Show All Files Opened in Preview App

filemacospreview

I have about 43 files opened in the preview, however, when I run lsof -c Preview, I only get a fraction of those (i.e. 8 files) which are currently opened.

This is the same output I get when using the Activity Monitor, but I would like to save the complete list of opened files so that I could reopen this list of files later on.

I have found some tips on how to retrieve the list of current files opened in this link.

Does anyone have a hint of what may be happening? Another solution that would be useful is to retrieve the list of files which were opened in the last Preview section.

EDIT:

As commented by D. A. Vincent, it is possible that the files are closed after loaded to memory. If it is possible to make a watcher of the files opened by preview, and log them to a file, this would be an appropriate solution as well. Especially if this watcher thread could be launched when user login or OS starts.

Best Answer

Here's an applescript solution. This should give you a complete list of open files (including the full path) in the preview app.

Also, if any AppleScript gurus come across this answer, I'd be grateful for any constructive criticism you have to offer. :)

set text item delimiters to "\n"
set myList to {}
tell application "Preview"
    set theDocs to get documents
    repeat with eachDoc in theDocs
        set thePath to path of eachDoc
        copy thePath to end of myList
    end repeat
end tell
set the_list to myList as text
tell application "Finder"
    set myFile to "/Users/YourName/YourFolder/FileName.txt"
    do shell script "date >> " & myFile
    do shell script "echo " & quoted form of the_list & " >> " & myFile
end tell

This will print the current date followed by a list of every document open in Preview. If you'd rather omit the date just remove the line:

do shell script "date >> " & myFile

Be sure to fill in the correct information on the line:

set myFile to "/Users/YourName/YourFolder/FileName.txt"

If you want to add a date stamp to your filenames, just put the following lines under the tell application "Finder" block

set time_stamp to (do shell script "date \"+%m-%d-%y\"")
set myFile to "/Users/YourName/YourFolder/PreviewProfile_" & time_stamp & ".txt"

The terminal date command has many different formats available. To read about the different options available, open your terminal and type man strftime.

After playing around a little more I realize this script can simplified even further. This is a more streamlined version that avoids some unnecessary steps from the original. But the result is the same either way.

set text item delimiters to "\n"
tell application "Preview"
    set theDocs to get path of every document as text
    tell application "Finder"
        set time_stamp to (do shell script "date \"+%m-%d-%y\"")
        set myFile to "/Users/YourName/YourFolder/PreviewProfile_" & time_stamp & ".txt"
        do shell script "echo " & quoted form of theDocs & " >> " & myFile
    end tell
end tell

For ease of use, you can save this script as an Automator service to use while working in any application. To do that just open Automator - then from the File menu select New or ⌘N from the keyboard. Then select Service from the choices shown. When the document opens, select Utilities in the far left column. Then Select Run AppleScript in the column to the right of that. Paste this script into the box that appears. In the drop down choices near the top of the page, select Service receives no input and in any application. Then just pick and name, save the file and you should have a service available to use anytime in the services menu.

Update

Here is a way to read and open the list of files we just created. This will allow you to pick and choose which files you want to open without having to open every file in the list if you don't want to.

tell application "Finder"
    set file_list to {}
    set my_files to paragraphs of (read "/Users/YourName/path/to/YourFile")
    repeat with nextLine in my_files
        if length of nextLine is greater than 0 then
            copy nextLine to the end of file_list
        end if
    end repeat
    choose from list file_list with multiple selections allowed
    set chosen_Files to the result
    repeat with next_file in chosen_Files
        do shell script "open " & next_file
    end repeat
end tell

Hope it helps. Let me know if you have any problems.