2 scripts which don't fire - why (compiled ok)

Hi and good day,

On this forum 2 scripts were presented for texts, to replace simple line breaks where there should be 2 - a common feature when you copy and paste some text.

(for /n do /n/n )

Those 2 scripts are from @chrillek and @houthakker, so I saved the one as AppleScript and the other as JavaScript and I put them into the DT scripts folder.

But they don’t fire - irrespective if I select a file or not.

This is the 1st script:
SCRIPT 1


/** Replace /n with /n/n

by chrillek, 2020-05-06

**/

(() => {

app = Application(‘DEVONthink 3’);

app.includeStandardAdditions = true;

var sel = app.selection();

sel.forEach(doc => {

text = doc.plainText();

doc.plainText = text.replace(/\n/g,"\n\n");

});

})


and this is the AS:
SCRIPT 2


— Replace /n with /n/n

— by houthakker, 2020-05-08

How to run a simple Regex on the copied text?

— no line breaks

use AppleScript version “2.4”

use scripting additions

on moreSpace(x)

x & linefeed

end moreSpace

on run

set the clipboard to unlines(map(moreSpace, paragraphs of ( the clipboard )))

end run

-------------------- GENERIC FUNCTIONS --------------------

https://github.com/RobTrew/prelude-applescript

– mReturn :: First-class m => (a -> b) -> m (a -> b)

on mReturn(f)

– 2nd class handler function lifted into 1st class script wrapper.

if script is class of f then

f

else

script

property |λ| : f

end script

end if

end mReturn

– map :: (a -> b) -> [a] -> [b]

on map(f, xs)

– The list obtained by applying f

– to each element of xs.

tell mReturn(f)

set lng to length of xs

set lst to {}

repeat with i from 1 to lng

set end of lst to |λ|( item i of xs, i, xs)

end repeat

return lst

end tell

end map

– unlines :: [String] -> String

on unlines(xs)

– A single string formed by the intercalation

– of a list of strings with the newline character.

set {dlm, my text item delimiters} to { my text item delimiters, linefeed}

set s to xs as text

set my text item delimiters to dlm

s

end unlines


/
Any assistance would be greatly appreciated!

With best regards,
Omar KN

How about this:

on performSmartRule(theRecords)
	tell application id "DNtp"
		repeat with theRecord in theRecords
			set theText to plain text of theRecord as string

			set theText to my findAndReplaceInText(theText, return, return & return)
			-- set theText to my findAndReplaceInText(theText, linefeed, return & return)
			-- set theText to my findAndReplaceInText(theText, return & linefeed, return & return)
			
			set plain text of theRecord to theText
		end repeat
	end tell
end performSmartRule

on findAndReplaceInText(theText, theSearchString, theReplacementString)
	set AppleScript's text item delimiters to theSearchString
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to theReplacementString
	set theText to theTextItems as string
	set AppleScript's text item delimiters to ""
	return theText
end findAndReplaceInText

Please note that this is a script for a Smart Rule. If you want a script for the menu or for a Reminder the enclosing lines have to be different. You can look them up in the Scripts Folder.

The two lines commented out with -- are examples for cases where you don’t face just returns but linefeeds too.

Yes for the Menu / or folder in Menu ( Menu > Scripts).

Those scripts don’t call the application like so:

tell application id "DNtp"

(Maybe that’s missing?)

How & which enclosing lines would you use for the DT3 Menu?

/

with best regards, Omar KN, Stockholm, Sweden

You have to remove the two lines

on performSmartRule(theRecords)
end performSmartRule

But that’s not enough as I notice just now. The repeat with theRecord line must be replaced too. I suggest you open one of the menu scripts. Take one from the Edit folder because editing is what you want do. Take the enclosing lines from that script and insert the set theText to line plus the associated handler at the end. That should do it.

hi, all I could do was scrabble around:

-- suavito, 2020-05-16

– Purpose: to replace /n with /n/n in DT3 texts

2 scripts which don't fire - why (compiled ok)

– took from Count Characters

– Name of script: Replace -n with -n-n-AS2.scpt


– removed: on performSmartRule(theRecords)

tell application id “DNtp”

– added new next line: try

try

– removed: repeat with theRecord in theRecords

set theText to plain text of theRecord as string

set theText to my findAndReplaceInText(theText, return, return & return)

– set theText to my findAndReplaceInText(theText, linefeed, return & return)

– set theText to my findAndReplaceInText(theText, return & linefeed, return & return)

set plain text of theRecord to theText

– removed: end repeat

– added new next line: end try

end try

end tell

– removed: end performSmartRule

on findAndReplaceInText(theText, theSearchString, theReplacementString)

set AppleScript’s text item delimiters to theSearchString

set theTextItems to every text item of theText

set AppleScript’s text item delimiters to theReplacementString

set theText to theTextItems as string

set AppleScript’s text item delimiters to “”

return theText

end findAndReplaceInText


– The Count Characters script

---- Count Characters.

---- Created by Christian Grunenberg on Mon Jun 07 2004.

---- Copyright © 2004-2019. All rights reserved.

–tell application id “DNtp”

– try

– set this_selection to the selection

– if this_selection is {} then error “Please select some contents.”

– show progress indicator “Counting characters” steps -1

– set this_characters to my countCharacters(this_selection)

– hide progress indicator

– display alert “DEVONthink” message (this_characters as string) & " characters."

– on error error_message number error_number

– hide progress indicator

– if the error_number is not -128 then display alert “DEVONthink” message error_message as warning

– end try

–end tell

–on countCharacters(these_childs)

– local this_child, this_count

– tell application id “DNtp”

– set this_count to 0

– repeat with this_child in these_childs

– set this_count to this_count + (character count of this_child)

– set this_count to this_count + (my countCharacters(children of this_child))

– end repeat

– end tell

– return this_count

–end countCharacters

It doesn’t work though.

I have to apologize, you are right. I underestimated the trouble and most of all I overestimated my Apple Script abilities. I only use parts from working scripts like Lego bricks. Which works to some extend. And because I’m lazy I focus on Smart Rule scripts that work automatically once I have set them up. They work a bit differently to menu scripts, only sometimes they differ by just their wrappers. So that little I have learned about Apple Script does not always apply. And it was late yesterday evening. My bad.

But nonetheless I want to throw another script into the ring. It is meant for selected items in DEVONthink and does not handle clipboard content at all.

Note that it does only work with text and Markdown notes as it flattens RTF, Formatted Notes, and HTML. And oddly it behaves pettishly every now and then with notes other than Markdown.

Try it on test notes until some of the experts turn up with a probably way better solution.

-- Add empty lines

tell application id "DNtp"
	
	try
		
		set thisSelection to the selection
		if thisSelection is {} then error "Please select some contents."
		
		set astidBefore to AppleScript's text item delimiters
		
		repeat with thisItem in thisSelection
			
			set theType to type of thisItem as string
			
			if ((theType is not "text") and (type of thisItem is not markdown)) then error "This script does not work with " & theType & "!"
			
			set theText to the plain text of thisItem
			
			set AppleScript's text item delimiters to {return} -- & linefeed, return, linefeed, character id 8233, character id 8232}
			set theNewText to every text item of theText
			set AppleScript's text item delimiters to {return & return}
			set theNewText to theNewText as text
			
			set the plain text of thisItem to theNewText
			
		end repeat

set AppleScript's text item delimiters to astidBefore
		
	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

PS to the experts: type of thisItem is not txt does, unlike type of thisItem is not markdown, not work. Why? The property reference says:

type (bookmark/‌feed/‌formatted note/‌group/‌html/‌markdown/‌PDF document/‌picture/‌plist/‌quicktime/‌rtf/‌rtfd/‌script/‌sheet/‌smart group/‌txt/‌unknown/‌webarchive/‌xml, r/o) : The type of a record.

As an exercise by someone who wished to know AppleScript (that’s me!) I ventured ahead using your script and a few other lines like so:

-- suavito, 2020-05-16
-- Purpose: to replace  /n with /n/n in DT3 texts
-- https://discourse.devontechnologies.com/t/2-scripts-which-dont-fire-why-compiled-ok/55714
-- suavito's 2nd script
-- Name of script: Replace -n with -n-n-AS3.scpt


tell application id "DNtp"
	
	try
		
		set thisSelection to the selection
		if thisSelection is {} then error "Please select some contents."
		
		set astidBefore to AppleScript's text item delimiters
		
		repeat with thisItem in thisSelection
			
			set theType to type of thisItem as string
			
			if ((theType is not "text") and (type of thisItem is not markdown)) then error "This script does not work with " & theType & "!"
			
			set theText to the plain text of thisItem
			
		end repeat
		
	end try
	
end tell
   

on findAndReplaceInText(theText, theSearchString, theReplacementString)
	set AppleScript's text item delimiters to {return} -- & linefeed, return, linefeed, character id 8233, character id 8232}
	set theNewText to every text item of theText
	set AppleScript's text item delimiters to {return & return}
	set theNewText to theNewText as text
end findAndReplaceInText

It doesn’t work of course.

Yes if someone from DevonThink could have a go - would be phantastic!

/
Any assistance would be greatly appreciated!

With best regards,
Omar KN

As with suavito’s script also mine were cut off on this form - don’t know why.

So here comes the rest of mine:

		set theText to the plain text of thisItem
			
		end repeat
		
	end try
	
end tell


on findAndReplaceInText(theText, theSearchString, theReplacementString)
	set AppleScript's text item delimiters to {return} -- & linefeed, return, linefeed, character id 8233, character id 8232}
	set theNewText to every text item of theText
	set AppleScript's text item delimiters to {return & return}
	set theNewText to theNewText as text
end findAndReplaceInText

My last script isn’t cut off, it does not include the handler on findAndReplaceInText anymore. It is not visible in full length but you can scroll to completely get it. Likewise with your first script.

And why did you add lines, did my script not work for you? Try it with Markdown, that should work. You might have to look at the not rendered Markdown as the rendered does not display tripple, quadruple or even a higher number of returns.

If you need a reliably working script as soon as possible you still can execute my first script in a Smart Rule. Set the trigger to “On Demand” and apply it manually. That would be a bit circuitous in comparison to a menu driven script but for the time being it will work. Until the adults come and rescue us.

Just sent an email to the DT adults…

I’ll check the Smart Rule script ASAP, thank you.

/okn

Regarding the Smart Rule method,

this is the warning I get:


/

with best regards, Omar KN, Stockholm, Sweden

Your script doesn’t do anything, it just cancels. The Smart Rule must execute my first script. The script must be saved in the Smart Rules script folder under a name of your choice.

The conditions would be if kind is either Markdown or Plain Text.

Double Returns

If the script is saved and a Smart Rule set up that executes it apply it by right-click on a file you want to add the extra returns to and chose “Apply Smart Rules” and the script.

And please be careful and only test with test data or duplicates. You seem not to be very familiar with Smart Rules either, that might cause damage to your data.

Hi and good day suavito,

This gives me a good opportunity to look into ”Smart Rules” set-up, thank you.

The good news is the adults (came and) helped me with the script:

Actually it was a pre-existent one, already in the treasure box of devontechnologies :slight_smile:

Name
Add Returns in RTF.scrpt

tell application id "DNtp"
	try
		tell text of think window 1
			set n to (count of paragraphs)
			repeat with i from 1 to n
				make new paragraph with data (return) at (after paragraph (2 * i - 1))
			end repeat
		end tell
	end try
end tell

Now with a keyboard shortcut set up in macOS System Preferences, when there is a text, which lost its paragraph returns, one shortcut will do, mine is ALT CMD P.

So this is solved = SOLUTION!