Approach to Automate text in JavaScript

If you are a user who love automation to make life simple. You have knowledge in JavaScript or like to use your own custom snippet. I think this approach to use Javascript to automate task is interesting. It is not a scriptable application maybe it could be who knows (its open source).

I guess it would be possible to use GUI Scripting. Or use the JavaScript
in the bundle to feed AppleScript that use JavaScript.

The application is ‘Boop’ and you could find it at App Store.
The website is: Boop.
You could add your own JavaScript snippet or extend it the way you like it and share with community.

Input: plain text
Output: what ever you like

I hope it could be useful for any user of this forum and thats why I like to share the knowledge. And if the forum had a category of Automation I would use that. :slight_smile:

Here is simple GUI Script


on performOpenPickerWithString(theString)
	tell application "System Events" to tell application process "Boop"
		set frontmost to true
		key code 0 using {command down}
		delay 0.1
		key code 11 using {command down}
		delay 0.1
		keystroke theString
		keystroke return
	end tell
end performOpenPickerWithString

on stringToBoop(theString)
	tell application "System Events" to tell application process "Boop"
		-- set frontmost to true
		tell window 1 to tell scroll area 1
			set value of text area 1 to theString
		end tell
	end tell
end stringToBoop

on stringFromBoop()
	tell application "System Events" to tell application process "Boop"
		-- set frontmost to true
		tell window 1 to tell scroll area 1
			set theString to its value of text area 1
		end tell
	end tell
	return theString as text
end stringFromBoop

set theString to quoted form of "hello world!"
stringToBoop(theString)

performOpenPickerWithString("add slashes")

set theString to stringFromBoop()

tell application "System Events"
	activate
	display dialog theString
end tell

I’m confused. You menton “JavaScript” several times, but I don’t see any JavaScript in your example code.

Can you please clarify?

The application Boop use JavaScript to manipulate a string. If you download the application and check the bundle you will see a Script folder containing JavaScript snippet. The application use a keyboard shortcuts of command + b to search for a JavaScript snippet that the user could apply to the string.

Did you download the application Boop ??

The example code use GUI Scripting to use application Boop because its not scriptable application. The code show a AS String in application that manipulate the string in JavaScript (Boop) and later return the string to AS. The code example itself is maybe not so useful but I like the approach the application Boop does and I thought my GUI script show that with one example of manipulate a string.

Bear in mind that keyboard shortcuts are customisable in macOS, so assuming that cmd-b will do what you intend might end up making an ass out of you and Ming.

Triggering the menu item instead would be more reliable, particularly if referenced by index key rather than name, as the former will be the same across different systems, and cannot be altered by the user in any conventional way.

Rather than GUI scripting the interface for Boop, could you instead get AppleScript to read a particular snippet file from the Boop scripts folder, and then apply the JavaScript yourself to some input ? AppleScript can perform a run script command in "JavaScript", so it feels like it lends itself to this reasonably well, and even more naturally if you wrote the script in JXA instead of AppleScript. I think the two things to consider with this would be how to deal with the ScriptExecution object that Boop uses to interface with its main window and the contents; 2) importing relevant JS modules into the JXA environment. But I think they are straightforward considerations, but if not, then you can always do the same approach using AppleScriptObjC and the JavaScriptCore framework.

You are right its properly a better approach if my JavaScript knowledge was better. The first approach didn’t work so well for me. I try to import the contents of the bundle scripts to feed a input of a JavaScript function.

I’m sure there are plenty of knowledge in this forum to have ideas. The way I have done it before is to call a JavaScript function with inputs there the inputs was AS string. All examples in the scripts folder use main as function and has 1 input. But it doesn’t have any return so I have to figure out how that works in JavaScript.