Paste a variable number of characters?

Hi,
I’m performing a calculation on the contents of the system clipboard and I would like to paste a result that includes a number of characters base on the calculation, and then the contents of the system clipboard. I can’t figure out how to repeatedly type a number of characters based on the value of the calculation. Is it possible?

It's a bit tough to give specific help without seeing your macro, but in general, you just need a Repeat loop:

In this example, your calculation results are stored in the local_theCalculation variable outside the loop. And again, I'm not sure exactly what you're trying to do, but this example would type "A" until it reached the value stored in the local_theCalculation variable.

-rob.

As well as simulating keystrokes you can build a string then "Insert by Typing" (or by pasting, if you delete the current clipboard afterwards). If it's a one-off calculation you can even do that in the "Repeat" action:

image

But I'm wondering -- do you want to repeat the same character or are you looking to use n characters of some other piece of text?


What I"m trying to do is create a balanced divider in BBEdit. I want to select the text I'd like centered, do some math and based on the result, paste n(octothorp) before and after my text.

So, after this, I'd have 48(#) a space, %SystemClipboard%, a space, and 48(#). Thank you griffman and Nige_S.

Are you assuming 100 columns, as in the image?

Yes, I am. That is based on my standard window width.

While I'm sure the BBEdit community already has a (better, native) way to do what you want, this'll get you started. It uses CEIL and FLOOR so that when there's an odd number of characters in your text the extra # goes in the prefix.

Pad to Center Text.kmmacros (3.7 KB)

Image

If you are triple-clicking to select a line you'll also get the \n, so either account for that in the macro or never select a full line :wink:

I use Keyboard Maestro for a bunch of things and I want to use it to keep my text manipulation workflows as similar as possible. I'll experiment with this. I've gotten as far as pasting the first batch of (#)s but then I get "%NamedClipboard%Buffer%" pasted, not the actual value. Thank you. I'll write back when I succeed.

Erm... Doesn't the macro above work for you? Copy some text, run the macro, paste the now-modified Clipboard into BBEdit.

It doesn't. I'll have time to look into why after my workday.

Still working perfectly here. Have you got any other clipboard managers installed that might mess with things?

Anyway, all those repeats feel a bit clunky. Here's another method, where we build the string with the maximum number of hashes front and back and then take 100 characters from the middle:

Pad to Center Text (substring).kmmacros (2.9 KB)

Image

1 Like