Script doesn't run from menu, but runs on Script Editor

Title.

The script is a simple script that formats a markdown file which I modified from one of the scripts found in the forum that modifies the markdown file generated by “summarize highlights as markdown”.

It works without issue if it’s run from Script Editor, but nothing happens when I select the script from the script menu.

Any idea how to fix it?

Could you post the script?

For proper formatting wrap the script in three backticks, like this

```
script

```

Anything in DT’s log window?

Not sure how to post script here but here it goes.

tell application id "DNtp"
	repeat with thisRecord in (selection as list)
		-- Loop through selected documents
		
		if (type of thisRecord as string) = "markdown" then
			-- Only process Markdown docs
			
			set newText to {}
			-- Set up an empty list to push the paragraphs into)
			
			set currentText to (plain text of thisRecord)
			-- Get the current text of the document
			
			copy ("#" & " " & (name of thisRecord) & return) to end of newText
			
			repeat with thisLine in (paragraphs of currentText)
				-- Loop through the document, paragraph by paragraph
				set the clipboard to thisLine
				set myCount to (count words of (the clipboard))
				
				if myCount is less than 5 then
					-- 
					
					copy (thisLine & return) to end of newText
					
				else if thisLine begins with "Address" then
					
					copy (myCount & "###" & " " & (thisLine) & return & thisLine & return & "***" & return) to end of newText
					
				else
					
					set cachedText to thisLine
					-- Anything else, including the H1 line at the top, just push into the newText list
				end if
			end repeat
			
			set plain text of thisRecord to (newText as string)
			-- Set the text of the document to the new text
			
		end if
	end repeat
end tell

I’ve tried the three backticks, but received “An unknown token can’t go here”. Sorry I’m not very familiar with applescripts, I only know how to shift existing script around.

This script used to work for a couple of days before it stopped working.

There is nothing in the log.

Try this


tell application id "DNtp"
	try
		set theRecords to selected records
		if theRecords = {} then error "Please select some records"
		
		repeat with thisRecord in theRecords
			-- Only process Markdown docs
			set theType to (type of thisRecord as string)
			if theType is in {"markdown", "«constant ****mkdn»"} then
				
				set newText to {}
				-- Set up an empty list to push the paragraphs into)
				
				set currentText to (plain text of thisRecord)
				-- Get the current text of the document
				
				copy ("#" & " " & (name of thisRecord) & return) to end of newText
				
				repeat with thisLine in (paragraphs of currentText)
					-- Loop through the document, paragraph by paragraph
					set the clipboard to thisLine
					set myCount to (count words of (the clipboard))
					
					if myCount is less than 5 then
						-- 
						
						copy (thisLine & return) to end of newText
						
					else if thisLine begins with "Address" then
						
						copy (myCount & "###" & " " & (thisLine) & return & thisLine & return & "***" & return) to end of newText
						
					else
						
						set cachedText to thisLine
						-- Anything else, including the H1 line at the top, just push into the newText list
					end if
				end repeat
				
				set plain text of thisRecord to (newText as string)
				-- Set the text of the document to the new text
				
			end if
		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
		return
	end try
end tell

It works! Thank you.

I don’t quite understand what’s going on, if you don’t mind can you briefly explain what was the issue?

I understand you added the error reports, other than that the major change is “«constant ****mkdn»”. Is that what solved the problem?

1 Like

Yes. There’s a known issue where scripts that are run from DEVONthink’s script menu may fail to convert type of thisRecord as string to a string. When this fails we don’t get "markdown" but the raw AppleScript syntax "«constant ****mkdn»".

I didn’t understand what the script is supposed to do. Why are you setting set the clipboard to thisLine and afterwards set myCount to (count words of (the clipboard))?

The idea is to only extract the headers, without the body of the markdown text. Since the headers are usually fewer than five words I use word counts to eliminate the body. Initially, I tried to set myCount to count words of thisLine, but it always return the number of characters instead. I couldn’t figure out the problem so I worked around by setting the line to clipboard and count the words from clipboard instead. Not the best solution but the only way I know how to get it to work.

If you post at least two possible texts with different headers there’s surely a cleaner way to do this :slight_smile:

Thanks @BLUEFROG :wink:

No problem :blush:

1 Like