3

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.

  • 2
    Do we expect every single window open in Preview to have exactly one open file as seen by lsof? While I'm not an experienced Mac developer, it would seem a natural thing for Preview.app to be able to read an entire image file from disk into memory and then issue the close system call. Especially if Preview were showing dozens of files. – D A Vincent May 13 '17 at 13:00
  • 1
    What is Application Monitor? Did you mean Activity Monitor? – D A Vincent May 13 '17 at 13:03
  • 1
    @DAVincent I have one window open for every file, though it only shows some of them. You mean that the Preview is closing the files and keeping the image on memory? So, another approach would be to make a watcher of the files opened by Preview app and write it to some log. – Werner May 14 '17 at 19:52
  • 1
    @DAVincent, yes, I meant the Activity Monitor. – Werner May 14 '17 at 19:52
  • 1
    Yes @Werner, you understood what I was getting at. We could create a script to watch Preview.app. It might be tricky to avoid a race condition where Preview ran too quickly for the watcher to notice. But extrapolating from my past experience wiht othe4r computers, maybe we could have the script use system debug functionality to capture every system call that Preview made. – D A Vincent May 15 '17 at 7:39
3
+50

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.

  • 1
    Thanks a lot, it worked! Would it be possible to save this file as PreviewProfile-%date.txt? – Werner May 15 '17 at 2:39
  • Sure you can. Check the updated answer. – I0_ol May 15 '17 at 3:19
  • 1
    I voted for this answer. It looks better than trying to create something using lsof or process level debug facilities. – D A Vincent May 15 '17 at 7:47
  • Maybe it would be useful to add to your answer how to open the files with Preview later on: cat ~/PreviewEnvironments/PreviewProfile_05-15-17.txt | while read; do echo "$REPLY"; open $REPLY; done. This way it preserves the spaces and other special characters. Another edit that I would make is to change "/Users/YourName" by "~", which would make it easier for newbies to use the script. – Werner May 16 '17 at 3:47
  • 1
    @Werner See my update for opening files in the list – I0_ol May 16 '17 at 12:27

You must log in to answer this question.

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