lsof does not show all files opened in Preview app

Solution 1:

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.