Skip to content

Instantly share code, notes, and snippets.

@zverhope
Last active December 16, 2022 08:24
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zverhope/7193c79fc963c63673b3b80bd802db5e to your computer and use it in GitHub Desktop.
Save zverhope/7193c79fc963c63673b3b80bd802db5e to your computer and use it in GitHub Desktop.
Find orphaned files in Bookends, whether with hierarchical or flat folder structure, and then move to Orphans folder
set theFolder to (path to home folder as text) & "Library:Mobile Documents:iCloud~com~sonnysoftware~bot:Documents"
set BEList to {}
set orphanList to {}
set FinderList to {}
set pathList to {}
set od to AppleScript's text item delimiters
tell application "Finder"
-- create a list of the name of every file in the folder you've set
set FinderList to every file of folder theFolder as alias list
-- get the name of every file in the subfolders of that folder
repeat with thisSubfolder in (get folders of alias theFolder)
set subFolder to thisSubfolder as string
set subFolderList to every file of folder subFolder as alias list
-- add names of items from each subfolder to end of main list
copy subFolderList to end of FinderList
end repeat
repeat with i in FinderList
set thePath to POSIX path of i
copy thePath to end of pathList
end repeat
end tell
tell application "Bookends"
-- depending on the size of the library, Bookends can take a bit of time to work through all of its items, so make sure that the script doesn't timeout
with timeout of 600 seconds
set theIDs to «event ToySRUID» "Attachments"
set theLines to paragraphs of theIDs
repeat with theID in theLines
-- get the unencoded path of each item with attachments
set BEpath to «event ToySGETA» theID
-- deal with any item that has multiple paths
set AppleScript's text item delimiters to linefeed
set thePaths to text items of BEpath
repeat with thePath in thePaths
copy thePath as string to end of BEList
end repeat
end repeat
end timeout
end tell
repeat with x from 1 to count of items of pathList
set n to item x of pathList
-- exclude any kind of attachment that Bookends does not index
if n does not end with "skim" then
if n is in pathList and n is not in BEList then set end of orphanList to n
end if
end repeat
tell application "Finder"
repeat with orphan in orphanList
move orphan as POSIX file to theFolder & ":Orphans" with replacing
end repeat
end tell
set od to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeed
set orphanList to text items of orphanList as string
set theCount to count of text items of orphanList
set textList to ("Total Number of Orphaned Files: " & theCount & linefeed & linefeed & orphanList)
set the clipboard to textList
set AppleScript's text item delimiters to od
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment