Index BBEdit shell worksheets content (or don't show protocol)

Would be great if DEVONthink would index content of BBEdit shell worksheets.

Or an (hidden) option to don’t show the protocol each time one is added.

kMDItemContentTypeTree             = (
    "public.item",
    "public.data",
    "com.barebones.bbedit.shell-worksheet"
)

Per BBEdit

Shell worksheets are stored in a private document format which is not text-based. This format allows BBEdit to store auxiliary data in the worksheet file’s data fork, thus ensuring that worksheets can safely be stored in version control systems that are not resource fork aware.

What do you want DEVONthink to do about that?

I’ve no idea :slight_smile: Perhaps it would be possible to have a custom whitelist of extensions which don’t pop up the protocol. Would be nice to index a folder of worksheets to have them in DEVONthink search results.

Since the content is not text-based, that’s not going to happen.

In BBEdit you can export a .worksheet as .txt – so if you wrote a new routine in BBEdit to export all .worksheets as text in the same folder(s) where the .worksheets are stored, then you would end up with a .txt sidecar file next to your .worksheet files, and DEVONthink would be able to index and search those.

1 Like

Funny, I did something similar this week as I started using git and wrote a textconv filter to get nice plain text diffs of Nisus Writer zrtf files. I’ll try your suggestion

BBEdit has a wonderful scripting dictionary, and “export” is one of the commands – should be able to do a simple conversion script that Hazel operates.

Hazel is a good idea as it’s searchable in Finder and other apps too.

I’ve got this now (but PlistBuddy seems to have a limit what it can extract).

EDIT: deleted script, see working smart rule below

I’ll try Hazel now, but Spotlight comments are also limited in lenght …

Ok tried a script for Hazel but Finder does really strange things with the comments …
All files have comments (I can see them in the info windows) but most don’t show in a Finder window - and some only show a part?!

2020-03-27_17-05-02

Makes no difference if Hazel set the comments or if the smart rule did it with an indexed folder. Restarting Finder didn’t help.

So I think I’ll stick with the smart rule …

Finder doesn’t like returns in comments …

Here’s an external Hazel script


-- External Hazel script: Set BBEdit Worksheet content as comment

on hazelProcessFile(theFile)
	tell application "Finder"
		try
			set thisFile to file theFile
			
			if (name extension of thisFile) = "worksheet" then
				set thePath to POSIX path of (thisFile as text)
				
				set theWorksheetContents to do shell script "/usr/libexec/PlistBuddy -c 'Print WorksheetContents'  " & quoted form of thePath
				
				if theWorksheetContents = "" then
					set theWorksheetContents to "[content too long]" -- seems PlistBuddy has a limit
				else if (count of characters in theWorksheetContents) > 256 then
					set theWorksheetContents to (characters 1 thru 256 in theWorksheetContents) as string
				end if
				
				set cleanWorksheetContents to my replace_String(theWorksheetContents, return, space)
				set comment of thisFile to cleanWorksheetContents
				return true
			else
				return false
			end if
			
		on error error_message number error_number
			if the error_number is not -128 then display alert "Hazel" message error_message as warning
			return
		end try
	end tell
end hazelProcessFile

on replace_String(theText, oldString, newString)
	local ASTID, theText, oldString, newString, lst
	set ASTID to AppleScript's text item delimiters
	try
		considering case
			set AppleScript's text item delimiters to oldString
			set lst to every text item of theText
			set AppleScript's text item delimiters to newString
			set theText to lst as string
		end considering
		set AppleScript's text item delimiters to ASTID
		return theText
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't replaceString: " & eMsg number eNum
	end try
end replace_String

This smart rule works and Finder now shows comments correctly

-- Set visible BBEdit Worksheet content as comment

on performSmartRule(theRecords)
	tell application id "DNtp"
		try
			repeat with theRecord in theRecords
				set thePath to path of theRecord
				set theWorksheetContents to do shell script "/usr/libexec/PlistBuddy -c 'Print WorksheetContents'  " & quoted form of thePath
				if theWorksheetContents = "" then set theWorksheetContents to "[content too long]" -- PlistBuddy seems to have a limit what it can extract
				set cleanWorksheetContents to my replace_String(theWorksheetContents, return, space) -- Finder doesn't like returns in comments
				set comment of theRecord to cleanWorksheetContents
			end repeat
			
		on error error_message number error_number
			if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		end try
	end tell
end performSmartRule

on replace_String(theText, oldString, newString)
	local ASTID, theText, oldString, newString, lst
	set ASTID to AppleScript's text item delimiters
	try
		considering case
			set AppleScript's text item delimiters to oldString
			set lst to every text item of theText
			set AppleScript's text item delimiters to newString
			set theText to lst as string
		end considering
		set AppleScript's text item delimiters to ASTID
		return theText
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't replaceString: " & eMsg number eNum
	end try
end replace_String