Script: Convert record to bookmark, save record's text in bookmark's comment and inherit properties

This script

  • converts records to bookmarks,
  • saves the record’s text in the bookmark’s comment
  • inherits properties (e.g. comment, tags, label)

By storing the text in the comment we get a “searchable” bookmark, i.e. a search for the original record’s text yields the bookmark.

:warning: Note:

  • URLs can become unreachable. Do not rely on bookmarks for important things.

-- Convert record to bookmark, save record's text in bookmark's comment and inherit properties

-- Note: URLs can become unreachable. Do not rely on bookmarks for important things

-- This script lets the bookmark inherit properties of the selected record (e.g. comment, tags, label)
-- If you don't want to inherit a property prefix the line with #

property useRedirectedURL : true -- Use redirected URL instead of original record's URL
property markerForUnavailableURL : "--- URL NOT REACHABLE ---" -- Set some string that should be added to the bookmark's name in case it is unreachable. Use this to search for (temporarily) unreachable URLs

tell application id "DNtp"
	try
		set theRecords to selected records
		if theRecords = {} then error "Please select some records"
		show progress indicator "Creating bookmark..." steps (count theRecords) as string with cancel button
		
		repeat with thisRecord in theRecords
			set thisRecord_Name to name without extension of thisRecord
			step progress indicator thisRecord_Name
			set thisRecord_URL to URL of thisRecord
			if thisRecord_URL ≠ "" then
				try
					set thisRecord_URL_redirected to do shell script "/usr/bin/curl -Ls -o /dev/null -w %{url_effective} " & quoted form of thisRecord_URL
					if useRedirectedURL = true then set thisRecord_URL to thisRecord_URL_redirected
				on error
					set thisRecord_Name to thisRecord_Name & space & markerForUnavailableURL
				end try
				
				set thisBookmark to create record with {name:thisRecord_Name, type:bookmark, URL:thisRecord_URL} in (first parent of thisRecord whose location does not start with "/Tags/")
				
				set comment of thisBookmark to my tid({comment of thisRecord, plain text of thisRecord}, linefeed & linefeed & "---" & linefeed & linefeed)
				
				tell thisBookmark -- Inherit properties
					set aliases to aliases of thisRecord -- Wiki aliases
					set creation date to creation date of thisRecord -- The creation date
					try
						set custom meta data to custom meta data of thisRecord -- User-defined metadata of a record
					end try
					set exclude from search to exclude from search of thisRecord -- Exclude record from searching.
					set exclude from see also to exclude from see also of thisRecord -- Exclude record from see also.
					set exclude from Wiki linking to exclude from Wiki linking of thisRecord -- Exclude record from automatic Wiki linking.
					set label to label of thisRecord -- Index of label (0-7)
					set locking to locking of thisRecord -- The locking
					set rating to rating of thisRecord -- Rating (0-5)
					set state to state of thisRecord -- The state/flag
					set tags to tags of thisRecord -- The tags
					try
						set thumbnail to thumbnail of thisRecord -- The thumbnail
					end try
					set unread to unread of thisRecord -- The unread flag
				end tell
			end if
		end repeat
		
		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
		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

1 Like

@pete31 I’m just curious: what is a use-case for this? (Genuinely curious, not trying to imply anything.)

2 Likes

@mksBelper is coming from EagleFile.app with 10.000 webarchives (Thread: Updating webarchives).

He first wanted to update them, i.e. reload the content and save it if it’s fine. But now he’s considering whether using a mixed format strategy would be better. In 99% of cases he wants the current content of a URL so this script can be used to convert his webarchives with labels etc.

3 Likes

Thanks for clarifying, @pete31 :slight_smile: .

Actually, the entire db has over 10,000 records; about a quarter of those are webarchives.

Still a big task; so I only want to do it once!