Skip to content

Instantly share code, notes, and snippets.

@tevino
Created June 12, 2019 09:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tevino/160d823ca7c0cf31e138b84d94fe4b3d to your computer and use it in GitHub Desktop.
Save tevino/160d823ca7c0cf31e138b84d94fe4b3d to your computer and use it in GitHub Desktop.
-- Find Similar Contents.
-- Created by Christian Grunenberg on Thu Feb 10 2005.
-- Copyright (c) 2005-2014. All rights reserved.
-- Limit for more or less identical contents (0.0: no similarity, 1.0: almost identical)
property pLimit : 0.9
-- Max. number of similar results to display
property pMaxResults : 10
tell application id "DNtp"
try
set this_selection to the selection
if this_selection is {} then error "Please select some contents."
show progress indicator "Finding Similar Contents..." steps (my countContents(this_selection))
my compareContents(this_selection)
hide progress indicator
on error error_message number error_number
hide progress indicator
if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
end try
end tell
on countContents(theseRecords)
local this_record, this_count
tell application id "DNtp"
set this_count to count of theseRecords
repeat with this_record in theseRecords
if type of this_record is group then set this_count to this_count + (my countContents(children of this_record))
end repeat
end tell
return this_count
end countContents
on filename_of_record(the_record)
set the_filename to false
tell application id "DNtp"
set the_filename to ((location of the_record) & (name of the_record))
end tell
return the_filename
end filename_of_record
on records_to_filenames(the_records)
set the_filenames to {}
tell application id "DNtp"
repeat with this_record in the_records
copy my filename_of_record(this_record) to the end of the the_filenames
end repeat
end tell
return the_filenames
end records_to_filenames
on find_record_by_filename(the_filename, the_records)
set found_record to false
tell application id "DNtp"
repeat with this_record in the_records
set this_filename to my filename_of_record(this_record)
considering case
if this_filename = (the_filename as string) then
set found_record to this_record
exit repeat
end if
end considering
end repeat
end tell
return found_record
end find_record_by_filename
on compareContents(theseRecords)
local i, this_record, this_button, this_result, this_message, this_url, this_title
tell application id "DNtp"
repeat with this_record in theseRecords
step progress indicator (name of this_record as string)
if type of this_record is not group then
try
set theComparison to compare record this_record
if (count of theComparison) > 1 then
set i to 0
set similar_records to {}
set this_message to ""
repeat with this_result in theComparison
if id of this_result is not id of this_record then
if (score of this_result) < pLimit then exit repeat
if i ≤ pMaxResults then
set this_message to this_message & my filename_of_record(this_result)
copy this_result to the end of similar_records
set this_url to URL of this_result
if this_url is not "" and this_url begins with "http" then
set this_message to this_message & " (" & this_url & ")" & return
else
set this_message to this_message & return
end if
end if
set i to i + 1
end if
end repeat
if i > 0 then
set this_message to "Found " & (i as string) & " similar result(s):" & return & return & this_message
if i > pMaxResults then set this_message to this_message & return & "..."
set this_title to my filename_of_record(this_record)
set this_url to URL of this_record
if this_url is not "" and this_url begins with "http" then set this_title to this_title & " (" & this_url & ")" & return
display alert this_title message this_message as warning buttons {"Cancel", "Skip", "Choose files to delete"} default button 3
set this_button to the button returned of the result
if this_button is equal to "Choose files to delete" then
copy this_record to the end of similar_records
set similar_files to my records_to_filenames(similar_records)
choose from list similar_files with prompt "Choose files to delete:" with multiple selections allowed
if the result is not false then
repeat with the_filename in result
set found_record to my find_record_by_filename(the_filename, similar_records)
if found_record is not false then
delete record found_record
else
display alert "record not found: " & return & the_filename
end if
end repeat
end if
else if this_button is equal to "Cancel" then
return false
end if
end if
end if
on error msg number num
if num is -128 then return false
end try
else if not my compareContents(children of this_record) then
return false
end if
end repeat
end tell
return true
end compareContents
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment