Script to find orphaned files

Users asking other users for AppleScripts that work with Bookends.
Post Reply
zvh
Posts: 32
Joined: Sun Aug 27, 2017 12:47 am

Script to find orphaned files

Post by zvh »

I've written a script that can get a list of files from your Bookends library folder and subfolders and will then compare that list against the attachments indexed to entries in your currently opened Bookends library. I thought this might be useful for others. The gist for the script is:

https://gist.github.com/zverhope/7193c7 ... 0bd802db5e

You'd need to customize a few things to make this work for you:

1) enter the path to your main Bookends library folder (relative to $HOME) in the first line of the script (mine is in my Google Drive);
2) If there are any kinds of attachments that will be in your library folder that aren't indexed by Bookends, then you can include their extensions in the following line (near the bottom of the gist):

Code: Select all

if n does not end with "skim" then
I use Skim to make annotations, which produces a skim notes file with the same name as the pdf. I don't index these with Bookends. If there's anything specific you exclude, then you can add those here. Alternatively, if you only want to include pdfs, you could change the line to:

Code: Select all

if n ends with "pdf" then
Or you could simply delete this contingency altogether (and be sure, then, to also delete the corresponding "end if").
3) Decide what you want to do with the output. I have mine going to a Keyboard Maestro variable, but you could also display the output or write it to a text file to go through.
Last edited by zvh on Sun Aug 26, 2018 6:05 pm, edited 1 time in total.
Jon
Site Admin
Posts: 10048
Joined: Tue Jul 13, 2004 6:27 pm
Location: Bethesda, MD
Contact:

Re: Script to find orphaned files

Post by Jon »

Thanks for uploading this. Note that we already have a built-in group that only shows references with attachments: Attachments.

Jon
Sonny Software
zvh
Posts: 32
Joined: Sun Aug 27, 2017 12:47 am

Re: Script to find orphaned files

Post by zvh »

Thanks Jon, I had entirely forgotten about that when writing the script. I've updated the instructions above (and the gist itself) to reflect and also included instructions on how to exclude a specific file type (skim files for me) or specify only a certain kind of file (e.g. pdfs).
Nestorito
Posts: 17
Joined: Mon Oct 09, 2017 4:36 am

Re: Script to find orphaned files

Post by Nestorito »

Hi zvh,
if I understand correctly your script should output a list of files that are not associated to references in an open BE database, am I right? So basically it could be used to "clean" the BE attachments folder from old or unassociated files, right?
In my BE attachments folder I have also powerpoint files and other kind of files, how can I tell your script to consider also this files as attachments?
After this how can I tell the script to output the list in a text file to later compare it with the attachments folder files?
Thanks a lot
Nestor
zvh
Posts: 32
Joined: Sun Aug 27, 2017 12:47 am

Re: Script to find orphaned files

Post by zvh »

Nestorito wrote: Fri Sep 07, 2018 5:25 am Hi zvh,
if I understand correctly your script should output a list of files that are not associated to references in an open BE database, am I right? So basically it could be used to "clean" the BE attachments folder from old or unassociated files, right?
In my BE attachments folder I have also powerpoint files and other kind of files, how can I tell your script to consider also this files as attachments?
After this how can I tell the script to output the list in a text file to later compare it with the attachments folder files?
Thanks a lot
Nestor
Hi Nestor,

Yes, this script takes a list of the names of the files within a certain folder (and its subfolders) and compares it against the list of the names of the attachments indexed in your open Bookends library. It then creates a new list of anything in the folder that isn't indexed by Bookends (whether because it has no record in Bookends or because the name of the file is different and therefore incorrectly indexed, etc.).

As per (2) in my initial post, if you run the script above then it should automatically include all file types except for skimnote files, which I've excluded near the bottom of the script. Altering that line would allow you to include (or exclude) files with the powerpoint extension or any other kind of extension. If you simply leave it as is, however, it should include powerpoints alongside pdfs, epubs, and so on.

There are a couple of options in terms of getting the output of the script. Changing the line

Code: Select all

return textList
to

Code: Select all

set the clipboard to textList
would set your clipboard to the list. To write to a file, you could instead change the line to

Code: Select all

do shell script "echo  " & quoted form of textList & " > /path/to/your/file"
Note that, if you have spaces in your path, you need to escape them like so

Code: Select all

/Users/Nobody/My\ Library/This\ List.txt
There is also a way to write to a file without invoking the shell, but it involves opening the file with write permission, so I think this way might be a bit easier to implement.

Hope this helps!
Dellu
Posts: 268
Joined: Sun Mar 27, 2016 5:30 am

Re: Script to find orphaned files

Post by Dellu »

Thank you. This is a very useful script

Is it possible to move (tag, and then use Hazel to move) these files to a specific folder instead of listing their name?
zvh
Posts: 32
Joined: Sun Aug 27, 2017 12:47 am

Re: Script to find orphaned files

Post by zvh »

Dellu wrote: Sun Sep 16, 2018 3:03 pm Thank you. This is a very useful script

Is it possible to move (tag, and then use Hazel to move) these files to a specific folder instead of listing their name?
Hi there,

So glad you find the script useful.

What you're looking for could certainly be done. You could tag the files or simply move them directly using Finder's AppleScript dictionary. It would be relatively simple if you only have one directory of attachments with no subfolders. What the posted script does is separate the file name from the file's path to compare it against a list of all attachments that Bookends has indexed. So what you'd need to do is restore the path to the orphaned file's name and then repeat Finder's "move" command with each item on the list to a specified directory. This, then, becomes more difficult if you have subfolders, as the list doesn't remember where each item came from, if that makes sense. What you might do in that instance is use a "try" command (or if path exists) for each item with each possible directory, so that the script attempts to move each file at each possible path but doesn't fail when the path doesn't exist. This would work, I think, but is messier (and would also cause problems if there is more than one file with the exact same name, which isn't the case with any of my files, but could happen).

Hopefully this helps!

Zach
Dellu
Posts: 268
Joined: Sun Mar 27, 2016 5:30 am

Re: Script to find orphaned files

Post by Dellu »

You could tag the files or simply move them directly using Finder's AppleScript dictionary. It would be relatively simple if you only have one directory of attachments with no subfolders. What the posted script does is separate the file name from the file's path to compare it against a list of all attachments that Bookends has indexed. So what you'd need to do is restore the path to the orphaned file's name and then repeat Finder's "move" command with each item on the list to a specified directory.
Zach

That makes sense.
Thank you so much. I cannot tell enough how this script saved me from a lot of hard work. I used to move my attachment folder to different folders once in a while just to get the orphaned files. Now, I don't need to. Thank you!
zvh
Posts: 32
Joined: Sun Aug 27, 2017 12:47 am

Re: Script to find orphaned files

Post by zvh »

Just an update on this to say that the linked script will now move any orphaned files to a pre-made folder called "Orphans". The script now compares paths rather than file names, so this should work with hierarchical folder structures. This version also simplifies the script a bit.

I had also switched over to the new AppleScript library, but the old events-based system actually seems to complete this task at least twice as quickly, so I've left the old way in the script for now.

As this script now moves files, please be certain to do a backup before using it for the first time. I have only tested this on my files (app. 3000 in hierarchical structure), so there could be problems that I didn't encounter. That said, the script is now basic enough that I'm optimistic about its general usability for those interested.
Post Reply