Generating worksheets programmatically

30 views
Skip to first unread message

Bruce Van Allen

unread,
Apr 5, 2021, 1:42:52 PM4/5/21
to BBEdit-Talk
Hi Friends,

Wondering whether anyone else has dealt with this: I want to generate a
BBEdit worksheet programmatically AND populate it with some initial info
for its use.

BBEdit has its CLI tool `bbedit`, which can create a worksheet from the
command line:

`bbedit —-worksheet my_worksheet`

This will create and open a new (blank) BBEdit worksheet named
‘my_worksheet’.

The BBEdit docs say that the bbedit command has an option `—-append`,
and that it may be used with `—-worksheet` but I don’t get how to
use it.

Going further, I’m generating the worksheets programmatically using
Perl:

## Perl script ##
#!perl

my $project = ‘abc’;
my $base_dir = ‘/Users/me/tests’;
my $worksheet_filename = ‘ws_test_01’;

# How do I get this into the new worksheet?
my $initial_content = <<CON;
A=‘project=$project’
B=‘base_dir=$base_dir’

perl my_project_analyzer.pl “$A” “$G”
#-#-#

CON

chdir $base_dir;

system( ‘bbedit’, ‘—worksheet’, ‘—wait’,
‘—resume’, $worksheet_filename);

## end Perl script ##

This opens a blank worksheet, and returns me to the Terminal after I
close the worksheet.

My goal is to have it open the worksheet with the following at the top:

A=‘project=abc’
B=‘base_dir=/Users/me/tests’

perl my_project_analyzer.pl “$A” “$G”
#-#-#

I have a command that selects everything down to the ‘#-#-#’ line,
so when I press Enter, it executes the perl script with the arguments
provided. The output appears below the ‘#-#-#’ line. The project
name, worksheet filename, base_dir, and some other potential args
dynamically come out of the executing Perl script that create the
worksheet. So my problem isn’t solved with stationery.

How do I apply the `-—append` option to put the initial content into
the worksheet??

I’ve tried variations on pipes etc, and this might be a limitation of
Perl’s `system` function.

If I run the above and then (in my Perl app) open the worksheet and
print the above, the file is no longer an executable worksheet. This
makes sense to me because under the hood, BBEdit worksheets are XML
docs, so arbitrarily opening and printing to them messes up the XML
structure.

The best I’ve been able to muster is to have the Perl script output
that initial content to the Terminal, which I can then copy-paste into
the new blank worksheet. LTA.

Anyone know how to deal with this?


Thanks,

- Bruce

_bruce__van_allen__santa_cruz__ca

jj

unread,
Apr 5, 2021, 4:09:03 PM4/5/21
to BBEdit Talk
Hi Bruce,

Here is an intend to do this with a shell script (if I understood correctly your problem).
The --worksheet flag has to be used with a -t flag naming the worksheet, otherwise BBEdit appears to create a second document with the passed name.

```shell
#! /bin/sh

WORKSHEET="ws_test_01" ;

CONTENT=$(cat <<'EOF'
A=‘project=abc’ 
B=‘base_dir=/Users/me/tests’ 

perl my_project_analyzer.pl “$A” “$G” 
EOF
)

echo "$CONTENT" | bbedit --worksheet -t "$WORKSHEET";
```
HTH

Jean Jourdain

Charlie Garrison

unread,
Apr 5, 2021, 7:41:12 PM4/5/21
to BBEdit-Talk

On 6 Apr 2021, at 3:42, Bruce Van Allen wrote:

I’ve tried variations on pipes etc, and this might be a limitation of Perl’s `system` function.

Correct, don't use system. Try this instead of the system line:

open(my $bb_fh, '|-', "bbedit —-worksheet —-append $worksheet_filename")
    or die "Couldn't open a pipe to bbedit: $!";
print $bb_fh $initial_content;

Or at least something close to that. See here for more info:

-cng

--

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

Bruce Van Allen

unread,
Apr 5, 2021, 8:48:39 PM4/5/21
to BBEdit Talk
Hi Jean,

Thanks! You led me to a solution.

I couldn’t use yours directly, because the worksheet filename, and the
values of the arguments in the template are dynamic, created within the
surrounding Perl code. I couldn’t figure out how to feed those to the
shell script as args to Perl’s `system` command from entirely within
the Perl code. But I found a workaround by putting the shell script in a
separate file. (My larger program can create that earlier in the setup
for a project, so no prob.)

So, simplified, this is what works:

In a separate executable shell script file, named here make_wksht.sh:

#!/bin/sh
FILE=$1;
TMPL=$2;
echo "$TMPL" | bbedit --worksheet -t "$FILE";

In the main Perl script:
#!/perl

# . . .

# faked here for demo:
my $project = 'abc';
my $dir = '/Users/me/test';
my $wksht_file_name = ‘ws_test_01’;

my $wksht_tmpl = <<TMPL;
A='project=$project'
B='base_dir=$dir'

perl my_project_analyzer.pl "$A" "$B"

#-#-#

TMPL

my $sh_script = './make_wksht.sh';
my $wksht_file_name = 'test_01.worksheet';

system "$script", "$wksht_file_name", “$wksht_tmpl”;

__END__


I expect there’s a way to accomplish this without the external shell
script - maybe just a matter of reconciling quoting between the shell
and Perl.

Meanwhile, I can move on with my larger project, and this helps a lot
for testing as I go!

Thanks!
> --
> This is the BBEdit Talk public discussion group. If you have a feature
> request or need technical support, please email
> "sup...@barebones.com" rather than posting here. Follow @bbedit on
> Twitter: <https://twitter.com/bbedit>
> ---
> You received this message because you are subscribed to the Google
> Groups "BBEdit Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to bbedit+un...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/bbedit/e6e3f22c-5bc8-4af7-b18a-03b99e9fc2b9n%40googlegroups.com.

Thanks,

- Bruce

_bruce__van_allen__santa_cruz__ca

Bruce Van Allen

unread,
Apr 5, 2021, 8:53:43 PM4/5/21
to BBEdit-Talk
Hey Charlie,

I just posted my other solution before I saw yours.

I hadn’t thought of using Perl’s `open` - too focused on the bbedit
commandline tool! I’ll see how —-append works when used this way…

Thanks!

On 5 Apr 2021, at 16:40, Charlie Garrison wrote:

> On 6 Apr 2021, at 3:42, Bruce Van Allen wrote:
>
>> I’ve tried variations on pipes etc, and this might be a limitation
>> of Perl’s `system` function.
>
> Correct, don't use `system`. Try this instead of the `system` line:
>
> ```
> open(my $bb_fh, '|-', "bbedit —-worksheet —-append
> $worksheet_filename")
> or die "Couldn't open a pipe to bbedit: $!";
> print $bb_fh $initial_content;
> ```
>
> Or at least something close to that. See here for more info:
>
> - https://perldoc.perl.org/perlopentut#Opening-a-pipe-for-writing
>
> -cng
>
> --
>
> Charlie Garrison <cha...@garrison.com.au>
> Garrison Computer Services <http://www.garrison.com.au>
> PO Box 380
> Tumbarumba NSW 2653 Australia
>
> --
> This is the BBEdit Talk public discussion group. If you have a feature
> request or need technical support, please email
> "sup...@barebones.com" rather than posting here. Follow @bbedit on
> Twitter: <https://twitter.com/bbedit>
> --- You received this message because you are subscribed to the Google
> Groups "BBEdit Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to bbedit+un...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/bbedit/4744F087-9E92-4891-9F99-E9299154836A%40garrison.com.au.

Thanks,

- Bruce

_bruce__van_allen__santa_cruz__ca

jj

unread,
Apr 6, 2021, 6:24:11 AM4/6/21
to BBEdit Talk
Bruce,

With Charlie's tip you can go full perl.

```perl
#!/usr/bin/perl
use strict;
use warnings;

my $project = 'abc'; 
my $dir = '/Users/me/test'; 
my $wksht_file_name = 'ws_test_01'; 

my $wksht_tmpl = <<TMPL ;
A='project=$project' 
B='base_dir=$dir' 

perl my_project_analyzer.pl \"\$A\" \"\$B\" 
TMPL

open(my $bb_fh, '|-', 'bbedit', '--worksheet', '-t', "$wksht_file_name");
print $bb_fh "$wksht_tmpl";
```
Best regards

Jean Jourdain
Reply all
Reply to author
Forward
0 new messages