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
3 Likes

That’s very cool! Thanks for sharing.

On this topic, I just wanted to let you know that PopClip now supports loading .js, .ts and .yaml files directly, either from the Open With menu or by dragging and dropping the file onto the PopClip menu bar icon. This is new in v2026.8.

PopClip does not install itself as the default handler, however, so it won’t hijack those files on double-click.

This gives the benefit of being able to install JS or YAML snippets directly from a text file without the problem of the .popcliptxt suffix being opaque for editors.