Script: Add reference URL to aliases and create Smart Group

This script prepares records so that they can be found via their reference URL.

What

  • adds each selected record’s reference URL to the record’s aliases
    → We can then use the reference URL in a search to find the record

  • creates a Smart Group in the global inbox that matches “aliases contains [reference URL]”
    → We get a Smart Group that matches exactly the selected records

Why

There are several ways this could be useful, e.g.

  • simply use resulting Smart Group
    i.e. collect selected records in a Smart Group without the need to use Custom Meta Data

  • drag Smart Group onto the global Smart Groups section of the sidebar
    i.e. create a global Smart Group that matches exactly the selected records

  • drag Smart Group onto the Smart Rules section of the sidebar
    i.e. create a Smart Rule that matches exactly the selected records

Scope

As the Smart Group is created in the global inbox it may not show all selected records. To get all results you need to move it to the selected records’ database or make it a global Smart Group.

  • In case of Smart Rules and global Smart Groups the scope must be changed manually.

  • If you move the Smart Group to another database its scope updates automatically.

-- Add reference URL to aliases and create Smart Group that matches "aliases contains [reference URL]"

-- This script can be used to comfortably create a Smart Rule or global Smart Group by dragging the resulting Smart Group into the navigation sidebar.

tell application id "DNtp"
	try
		set theRecords to selected records
		if theRecords = {} then error "Please select some records."
		
		set theReferenceURLs to {}
		
		repeat with thisRecord in theRecords
			set thisRecord_ReferenceURL to reference URL of thisRecord
			set thisRecord_Aliases to aliases of thisRecord
			
			if thisRecord_Aliases does not contain thisRecord_ReferenceURL then
				set thisRecord_Aliases_list to my tid(thisRecord_Aliases, {", ", "; ", ",", ";"})
				set end of thisRecord_Aliases_list to thisRecord_ReferenceURL
				set thisRecord_Aliases_string to my tid(thisRecord_Aliases_list, ", ")
				set aliases of thisRecord to thisRecord_Aliases_string
			end if
			
			set end of theReferenceURLs to thisRecord_ReferenceURL
		end repeat
		
		set theSmartGroup_Query to "{any: aliases:~" & my tid(theReferenceURLs, " aliases:~") & "}"
		
		set theOutputGroup to root of inbox
		set theSmartGroup to create record with {type:smart group, search predicates:theSmartGroup_Query, search group:theOutputGroup, name:theSmartGroup_Query, exclude from search:true} in theOutputGroup
		
		set theWindow to open window for record theOutputGroup with force
		set selection of theWindow to {theSmartGroup}
		activate
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		return
	end try
end tell

on tid(theInput, theDelimiter)
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	if class of theInput = text then
		set theOutput to text items of theInput
	else if class of theInput = list then
		set theOutput to theInput as text
	end if
	set AppleScript's text item delimiters to d
	return theOutput
end tid

3 Likes

Thanks for sharing this script!

1 Like
  • adds each selected record’s reference URL to the record’s aliases
    We can then use the reference URL in a search to find the record

I’m curious as to when this is being done.

  • creates a Smart Group in the global inbox that matches “aliases contains [reference URL]”
    → We get a Smart Group that matches exactly the selected records

Wouldn’t this always be a smart group of one item?

@rufus123 asked whether it’s possible to find a record via its reference URL , see Crazy suggestion: accept links in search boxes. To do this I wrote this script.

And @AW2307 asked for a way to include the records’ name in a markdown group’s transclusion record, see this post. Instead of setting up the Smart Rules manually we came up with the solution to set the Smart Groups search results by using the “reference URL in aliases” approach. This way there’s only one Smart Group needed.

I think the script will be useful in a Smart Rule scenario where we don’t want a dynamic search but know beforehand that we want to match a static set of groups (and don’t want to use Custom Meta Data). Instead of creating several Smart Rules with different scopes we can use a single Smart Rule and let its script act on this static set of groups (or records).

Nope, the script uses any:

(I’ve moved the resulting Smart Group from the global inbox to the selected records’ database)

If I would drag this Smart Group onto the sidebar’s Smart Rules section I then could set up a script that acts on the previously selected records. So it’s a way to set a Smart Group/Rule’s search results without the need to use extra Custom Meta Data.