Gather related items by replicating

There is a sibling thread here, but I am posting this script in its own thread because it is a generalized answer to the question “how do I gather related documents into a single location” rather than the special case posted at the linked thread.

Select a document and run the script. The script will ask if you want to look into Names, URLs (as set in “Show Info…”) or Spotlight Comments of the documents in a database. The script then asks for the text you want to search from. Based on the scope (Names, URLs, or Comments) and the search text, the database is searched. If the search turns up anything, you are prompted to select an existing location, or create a new one if you wish. Finally, any document that is found in the database that satisfies the request is replicated to the designated location.

-- Gather replicants of related records
-- Written 2013 by korm; edited by Eric Böhnisch-Volkmann

-- change this property to 'true' if we want to include Group names, comments, or URLs in the search

property searchInGroups : false

tell application id "com.devon-technologies.thinkpro2"
	
	try
		set theSelection to (the selection as list)
		if ((count of theSelection) = 0) then error "Please select a document."
		set theSelection to the first item of theSelection
		if (not searchInGroups and (the kind of theSelection is "Group")) then error "Please do not select a group."
		
		-- dialog
		
		-- (1) where are we looking?
		
		set searchIn to (choose from list {"Name", "Comments", "URL"} with title "Gather documents" with prompt "Gather documents by searching in:" default items {"Name"}) as string
		
		-- (2) get the search string
		
		set defaultAnswer to ""
		if searchIn is in {"Name"} then -- suggest that this Name is the search string
			set defaultAnswer to the name of theSelection
		else if searchIn is "Comments" then -- suggest that this Spotlight Comment is the search string
			set defaultAnswer to comment of theSelection
		else if searchIn is "URL" then -- suggest that this URL is the search string
			set defaultAnswer to the URL of theSelection
		end if
		
		if defaultAnswer is not "" then
			
			-- We cannot have a blank search string so keep asking until we have the string
			
			set searchString to ""
			repeat until searchString is not ""
				set searchString to text returned of (display dialog "Enter what all items to be gathered should have in common (use wildcards * and ? if needed, e.g. enter 'devon*' to find all items starting with 'devon'):" default answer defaultAnswer with title "Enter search string")
				if searchString is "" then display dialog "Search string was empty.  Please try again."
			end repeat
			
			
			--search
			
			set searchResults to {}
			if searchIn is "Name" then
				set searchResults to search searchString within titles
			else if searchIn is "Comments" then
				set searchResults to search searchString within comments
			else if searchIn is "URL" then
				set searchResults to search searchString within URLs
			end if
			
			if searchResults is {} then error "Sorry, nothing found."
			
			-- (3) where do we put the result
			
			set theLocation to display group selector "Where do you want to gather the replicants of the found items?"
			
			--replicate
			
			repeat with thisItem in searchResults
				replicate record thisItem to theLocation
			end repeat
			
		end if
		
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "We have a problem…" message error_message as warning
		
	end try
	
end tell

1 Like

This falls fair and squarely in the “I didn’t know I needed this until you posted it, now I shall be using it a lot” category! Thanks Korm. :smiley: