Rename with Regular Expression - JavaScript version

Hi,
There’s a “Rename Using RegEx” script in DT that kinda works. “kinda”, because it throws errors in many cases and it uses a fairly limited regular expression syntax (because it relies on Mac OS’ sed command line utility). It was gnawing at me, so that I finally wrote my own version of it in JavaScript. It relies on JS regular expression syntax (so you can use \d instead of [0-9] etc.). The whole thing is not very robust, e.g. it doesn’t open a dialog if there’s no current selection. But at least it does what I want it to do. And the dialogs are in German, but that can easily be fixed.

app = Application('DEVONthink 3');
app.includeStandardAdditions = true;

var sel = app.selection();

if (sel.length > 0) {
		var searchFor = app.displayDialog('Suchen nach RE', { defaultAnswer: "" });
		if (searchFor.textReturned === '') exit;
		var replaceWith = app.displayDialog(searchFor.textReturned + 
		    '\nErsetzen durch RE', { defaultAnswer: ""});
		var re = new RegExp(searchFor.textReturned);

		sel.forEach( el => {
            var n = el.name();
			el.name = n.replace(re,replaceWith.textReturned);
		});
}

Can you clarify - is it possible to use Javascript in DT anywhere that Applescript is used? Benefits of one vs another? What version(s) of Javascript work? How is Javascript different from Javascript for Automation?

Yes, you can. JavaScript is officialy equivalent to AppleScript since Mac OS 10.10 (or so). I have no idea if and how JS is different from JS for Automation. I suppose that JXA is a beefed up version of JS, e.g. with the Application object that’s not part of JS. What I know, however: JS scripting in Mac OS is documented even worse then AppleScript. So it’s even less fun scripting Mac Apps in JS than it is in AppleScript.

Firstly: Whatever you can achieve with JavaScript, you can probably also achieve with AppleScript. If you’re comfortable with the latter, by all means: stick with it. I was never comfortable with it (in fact, I think it’s an overly verbose pain in the ass), but I am quite happy with JavaScript.

Secondly, AppleScript is a bit less sophisticated in terms of built-in functions (no regexes, for example, no forEach or map or similar array functions). Even the existing functions are somewhat limited, e.g. offset works only from the beginning of the string. Which sums up to “JavaScript scripts are a lot less to type than AppleScript scripts”.

In my opinion (!), AppleScript is on its way out. It is probably fine for english speaking people, because it resembles written English (that’s why it is so verbose and requires an inordinate amount of typing). But not everybody is a native English speaker… AppleScript is supposedly easy to learn, but I’m not so sure how much of this conviction comes from native English speakers. JavaScript is more modern and evolving, whereas AppleScript seems to be very much cast in stone. And JS is obviously more widely used since it’s (one of the) language(s) of the Web.

But hey, if you’re comfortable with AppleScript, use it. I seem to remember that there’s even some kind of regular expression library available for it…

Interesting - thank you

I am proficient only to a limited degree in either Applescript or Javascript - I plan to self-study to master one or the other over the next few months. Applescript seems to be overwhelmingly popular on the Forum and among Devontech staff so that seems to make the most sense, but I am curious if there is some advantage to consider Javascript instead.

To put it bluntly: AppleScript can be used only with Mac Apps. JavaScript is used client- and server-side on the whole Web – and you can script Mac Apps with it. However, if that’s all the programming you’re ever going to do, than Apple Script might be just the thing for you.