Script to add a CSS header to markdown -- sanity check...

I’ve been following the discussion over at this thread:

about adding a CSS header to markdown files. I’ve managed to get it working for new files and now I’m looking to add it retrospectively to old files – that will be enough for me, although it wouldn’t meet the needs of the OP in that thread.

I’ve cobbled together the following short basic script to do this: it tests the file to see it already has a CSS header and if not adds a DTP link to the CSS document I keep in the database.

It seems to work OK when I’ve tested it, but before I apply it to four thousand files or so, I would be grateful if somebody who knows what they’re doing could have a quick look and tell me if there is anything in it which is bad practice or could cause problems.

tell application id "DNtp"
	set theSelection to the selection
	if theSelection is not {} then
		set theCSS to "<link rel=\"stylesheet\" href=\"x-devonthink-item://C8B8E159-9653-4E7F-AE0F-1CD6F04246BE\">"
		repeat with theRecord in theSelection
			set theText to plain text of theRecord
			if theText does not start with "<link rel=" then
				set theText to theCSS & return & return & theText
				set theText to theText
			end if
			set plain text of theRecord to theText
		end repeat
	end if
end tell

Secondly, the idea is to use a smart group to pick up all the Markdown files across my databases, using Kind is Plain Text – which of course picks up .txt files as well as .md.

I could just sort and select that way, but is there any way in a Smart Group or Script of differentiating between Plain Text and Markdown files in the database?

Many thanks for your help.

Hi Korm, thanks for the reply. I’ll amend it for the search term – helpful, thanks.

Why am I doing it? Partly because I saw the problem in the other thread and wondered how I could get round it in AppleScript (which now and then I attempt fitfully to get a grip on), partly because on DTTG the font size of the alternative display is a little too small for comfort (hence Marked isn’t an option there). I have time machine and other regular backups, so I’m not too worried about that aspect.

So, no, it’s not essential!

BTW, I think it’s unlikely it would mangle non-markdown plain text because although you don’t seem to be able to search on Kind=markdown, you can sort on it in the results, so it’s a matter of sorting and selecting the right files.

Thanks again.