Turn script which already works from the command line into a text filter

52 views
Skip to first unread message

jh

unread,
Apr 6, 2020, 6:59:29 PM4/6/20
to BBEdit Talk
I have an installation of the JSBeautify (basically Tidy for JavaScript) code on my system and I can run it like this on any file:

/usr/local/bin/js-beautify -rd <the filename>

so how do I turn it into a BBEdit script, text filter or text factory, such that I can run it and the full path to the frontmost document is sent as that <the filename> argument?

I can't immediately see a way to have 'full path to the frontmost document' sent to the script.

I realise it would only work if the document is saved to disk of course. js-beautify has the option to read from STDIN as well, so can I do that, and have the output piped back in to the current document?

Charlie Garrison

unread,
Apr 6, 2020, 7:49:39 PM4/6/20
to BBEdit Talk

On 7 Apr 2020, at 8:59, jh wrote:

I realise it would only work if the document is saved to disk of course. js-beautify has the option to read from STDIN as well, so can I do that, and have the output piped back in to the current document?

Yep, do it with STDIN/STDOUT. Here is a text filter I've got installed:

#!/bin/sh

## https://github.com/einars/js-beautify
## https://www.npmjs.org/package/js-beautify

# js-beautify -f - --good-stuff
/usr/local/bin/js-beautify --e4x   -f -

Using STDIN/STDOUT means you also process the selected text, rather than processing the whole file.

-cng

--

Charlie Garrison                   <cha...@garrison.com.au>
Garrison Computer Services      <http://www.garrison.com.au>
PO Box 380
Tumbarumba NSW 2653  Australia

jh

unread,
Apr 6, 2020, 8:45:45 PM4/6/20
to BBEdit Talk
Thanks so much!

It's working perfectly. In my frantic googling I also found this:


but it requires node. Yours is way more elegant.

Charlie Garrison

unread,
Apr 6, 2020, 8:58:46 PM4/6/20
to BBEdit Talk

On 7 Apr 2020, at 10:45, jh wrote:

It's working perfectly.

That's great. I've stopped using that one in favour of a custom prettifier that looks at document language and fires off different tool (JSON/Javascript/Perl/SQL/etc), but the concept is still the same. I was getting tired of different key shortcuts depending on language; now all prettifiers are cmd-ctrl-P. Easy. :-)

In my frantic googling I also found this:
https://gist.github.com/brentsimmons/7819109
but it requires node. Yours is way more elegant.

Using a node module/wrapper is perfectly good solution too, but certainly not as elegant and requires more setup/installation.

Reply all
Reply to author
Forward
0 new messages