3

I have a folder of 6000+ PDF files (chapters, articles, etc.). I'm trying to weed out/sort those that I've just downloaded but never annotated. Is there a way to do this? Those PDFs that I've never annotated usually have the same "created" and "modified" dates, so I was thinking those criteria could be used (i.e., look for files whose modified date is later than/not the same as the created date), but I have no idea how to do that.

In other words, I need to be able to find any PDF on my computer that has been modified.

Thank you for any help!

  • What is it you want to do with the found PDF files that meet the criteria? I ask because using Automator, it found, in 2.5 seconds, the 108 PDF files out of the 1,181 in my Documents folder that were modified after being created. I can tell you the workflow in an answer but knowing how you want the info and or what to do with the files found, I could expand the workflow before posting it. – user3439894 Jan 9 '18 at 20:21
  • @user3439894 Ideally, I could have a complete workflow that a) finds any modified PDFs (i.e., with Skim annotations), b) exports them with embedded notes (which'll make the annotations readable in Preview, PDFpen, etc.), c) deletes the original PDF, and d) that keeps the newly-converted PDF in the same folder location as the one it replaced. Or if Finder just shows me the modified PDFs, I could take it from there, albeit in a less efficient way. I do already have a droplet that does the exporting with embedded notes...I just need a way to identify the modified files. Hope this makes sense. – william Jan 9 '18 at 20:47
  • Okay, I'm headed out for the evening so I don't have the time to do all that was presented in you comment, so I'll post the info I have and you can see if you can build on it, as I won't have any time till tomorrow (depending on how late I get back tonight). In Automator, create a new workflow and add a Find Finder Items action with settings, e.g., Search(Documents) > (All) > (Kind)(is)(PDF). Add a Run AppleScript action, replacing the default code with the following example AppleScript code, which you must first run this as a command in Terminal to parse it into individual lines of code: – user3439894 Jan 9 '18 at 21:12
  • printf '\non run {input, parameters}\nset modifiedFilesList to {}\nrepeat with i from 1 to count input\nset fileInfo to info for item i of input\nset cDate to creation date in fileInfo\nset mDate to modification date in fileInfo\nif mDate > cDate then\nset end of modifiedFilesList to item i of input\nend if\nend repeat\nreturn modifiedFilesList\nend run\n\n' Then copy & paste from Terminal to Run AppleScript action. Note that as coded, it passes the lists of files modified since creation to the next action in the workflow, or you can expand the example AppleScript code to do something else. – user3439894 Jan 9 '18 at 21:12
  • Thank you for the help. First, that's amazing. However, there are many files I never annotated, but nonetheless it appears the date modified is a couple seconds later than the date created (I just now found out how to view seconds in Finder). Is there a way to modify the script to list only those files whose modified vs created date differs by more than 30 seconds or so? Thanks so much for the help. – william Jan 9 '18 at 22:55
3

Per info in the OP and comments, this will do as you asked.

In Automator:

  • Create a new Workflow.
  • Add a Find Finder Items action.
    • With settings, e.g., Search (Documents)
    • (All) of the following are true
    • (Kind) (is) (PDF)
  • Add a Run AppleScript action.

    • Replace the default code with the following example AppleScript code show further below:

    • Note: If Skim is not in the /Applications folder, then modify the value of the skimpdfPathFilename variable accordingly. You should not need to modify anything else unless you want to set the value of the offsetInSeconds variable, e.g. set offsetInSeconds to 60, to a different value. This variable is used to help find the files that really have been modified since they were created. The granularity differential between the creation date and modification date when a file is first created can be from 0 seconds to a higher value, which is not a consistent value depending on how the file was created. Make adjustments as you see fit for your use case.

What the Workflow and example AppleScript code does:

  • Finds all PDF files in the target folder, including all subfolders.
    • This is done with the Find Finder Items action and its output is passed to the
      Run AppleScript action.
  • Creates a list of all PDF files that have been modified after the creation date, per the value of the offsetInSeconds variable.
    • This is done in the first repeat loop. Files meeting the criteria are stored in modifiedFilesList to be used in the next repeat loop.
  • Creates a list of all files that have annotations made in Skim.
    • This is done using xattr to get the extended attributes of the target files. If a file has the target extended attributes a flag is set to true and if not, set to false. The files flagged as true go into annotatedSkimFilesList to be used in the next repeat loop.
  • Embeds in place the annotations made to the files in Skim.
    • Using the skimpdf utility within Skim on the files in annotatedSkimFilesList, annotations are embedded in place. Thus no need to export to a second file, then delete the original and replace it.

NOTE: While I have tested this and it works without issue for me, nonetheless do not run this until you are sure you have a proper backup! You should also test the workflow on a small sampling of copied files placed outside of the actual search folder the workflow will be run on after testing is done.


Example AppleScript code:

on run {input, parameters}

    set skimpdfPathFilename to "'/Applications/Skim.app/Contents/SharedSupport/skimpdf'"

    set offsetInSeconds to 60       
    set modifiedFilesList to {}
    set annotatedSkimFilesList to {}

    repeat with i from 1 to count input
        set fileInfo to info for item i of input
        set cDate to creation date in fileInfo
        set mDate to modification date in fileInfo
        if mDate > (cDate + offsetInSeconds) then
            set end of modifiedFilesList to POSIX path of item i of input
        end if
    end repeat

    repeat with i from 1 to count modifiedFilesList
        set withNotes to (do shell script "xattr " & quoted form of item i in modifiedFilesList ¬
            & " | [ $(grep -c \".*_notes$\") -ge 1 ] && printf 'true' || printf 'false'") as boolean
        if withNotes then
            set end of annotatedSkimFilesList to item i in modifiedFilesList
        end if
    end repeat

    repeat with i from 1 to count annotatedSkimFilesList
        do shell script skimpdfPathFilename & space & "embed" & space & ¬
            quoted form of item i in annotatedSkimFilesList
    end repeat

end run

Understanding the do shell script command in the second repeat loop:

When a PDF is annotated in Skim and saved, extended attributes are set on the file, e.g.:

$ xattr Filename.pdf 
com.apple.FinderInfo
net_sourceforge_skim-app_notes
net_sourceforge_skim-app_rtf_notes
net_sourceforge_skim-app_text_notes
$ 

The output is piped | to:

[ $(grep -c \".*_notes$\") -ge 1 ] && printf 'true' || printf 'false'

Which tests the output of grep counting the occurrences of the pattern and if grep finds one or more occurrences of the pattern, then the value of the withNotes variable is set to true, while being set to false otherwise.

Note that Skim does have a built-in command line utility, e.g. /Applications/Skim.app/Contents/SharedSupport/skimnotes that can be used to test if a PDF has annotations made in Skim, however because of its output this utility is better used in an shell script run in Terminal then a do shell script command, and why I used xattr and grep instead.


Note: The example AppleScript code above is just that, and does not include any error handling as may be appropriate/needed/wanted, the onus is upon the user to add any appropriate error handling for any example code presented and or code written by the oneself.

  • Whilst I don't use Skim, I do have modified PDFs. I found that 2 seconds difference is not enough. But you soon find what is best by experiment. I am keeping your solution (without Skim) in my saved Workflows - thanks. – Gilby Jan 10 '18 at 7:47
  • @Gilby, It was late when I originally posted my answer and I used + 2 just because some value had to be added and I made note of it, however after a reread this morning, I've modified the code to use your 60 seconds suggested in the comment to the OP. I did set it as a separate variable so as not to have to go deeper into the code the change it. Thanks for you comment. – user3439894 Jan 10 '18 at 17:40
  • This is FANTASTIC. I did get an error alert that may be related to certain PDF file types. E.g., one PDF in particular is read-only or otherwise locked down (i.e., I can annotate, but I can't print it). When I ran the script specifically on the folder containing this PDF, I got the error again. Nonetheless, when I do a spot check of a couple dozen other (regular) PDFs, they've all been saved with embedded notes. Brilliant stuff. Thank you again! – william Jan 10 '18 at 18:22
1

Introduction
Based on your questions and your follow-up comment below it, I think the solution can be as simple as what I am proposing. In addition to @user3439894's well-written comment, I believe you a couple of great choices to accomplish your task.

The Setup

  1. Open the location in Finder and navigate to the view options at the top. It looks like this: enter image description here

  2. Now, navigate to the arrangement options/bar at the top, click on them and you should see the following. Be sure to check both Date Modified and Date Created along with any other options you wish to sort by.enter image description here

  3. Next, sort your list by Date Modified, in my case I created the files one after another in consecutive naming order, don't let this fool you. I have changed the file 17.pdf and saved it. As you can see, it jumped to the top of the list. When viewed in deciding order. enter image description here

Implementation
As all files are now grouped by their Date Modified, you can drag them in chunks of individually into your droplet (assuming it does in fact function fully as you say it does).
This would cover the second half of your follow-up comment, while @user3439894 has given you essentially what you are looking for in the first half.
I would be interested in following up with you on how things went, whichever option you choose is up to you, they are both alternatives to manually screening the data one by one.

  • I appreciate your suggestion to use a visual method, which would be more helpful if I weren't dealing with so many PDFs in multiple folders. Still, Finder only allows me to sort on one criteria at a time. Thus when I sort by 'date modified' and I scroll down through the files, they're still jumbled together (i.e., not all the modified files are at the top/bottom). Thank you. – william Jan 9 '18 at 22:19
0

Probably the easiest thing to do would be to create a smart folder in Finder. This smart folder contains all of the files defined by your search criteria. From there, it's very easy to sort the files in any which way you like.

In Finder, go to menu item File/New Smart Folder.

enter image description here

If you want to search your entire computer for the PDF files, select “Macintosh HD" in the sidebar. In the search field, simply input… .pdf

enter image description here

On the right side of that image you'll see a + button beneath the Save button. Clicking that button will allow you to enter more search criteria. There are tons of options you can choose. I chose “file extension” and added .pdf for the value

enter image description here

In Finder, go to menu item View/Show View Options

enter image description here

Make sure "Date Modified" Is set in both available fields

enter image description here

Hit the "Save" Button underneath the tab that says new Smart Folder

enter image description here

Now you will have a new smart folder with all of your PDFs in your Finder sidebar.

enter image description here

  • 1
    Whilst that is a nice demo of how to create a useful saved search, I don't believe it can answer @william's question. There is no way (as far I know) to do a comparison of dates in spotlight. – Gilby Jan 10 '18 at 7:42

You must log in to answer this question.

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