Use BBEdit Scripting to change syntax dynamically for snippets

Hello everybody

I use BBEdit’s action (scripting) to change syntax of document with extension .popcliptxt between yaml (default), javascript and Unix Shell.

By default in my BBEDitt configuration, files with .popcliptxt extension have assigned syntax for YAML. But when I you use reverse notation (YAML embedded into comment of JavaScript or Shell Script) this is ugly.

So I’ve put in BBEdit’s Attachment Scripts folder the script like below. It must be named Document.documentDidOpen.scpt or be part of valid other BBEdit ActionScript file.

I hope that it will be useable for someone.

using terms from application "BBEdit"
	
	on documentDidOpen(theDoc)
		
		tell theDoc
			set theExt to get name
			# Actions for PopClip
			if theExt ends with ".popcliptxt" then
				tell theDoc
					set theLine to get first line as text
					if theLine contains "popclip" then
						if theLine starts with "//" then
							set source language to "javascript"
						end if
						if theLine starts with "#" then
							set source language to "Unix Shell Script"
						end if
					end if
				end tell
			end if
		end tell
		
		return true
	end documentDidOpen
	
end using terms from
1 Like

That’s very cool! Thanks for sharing.