one other thing I find myself doing quite often, is that I write one, let's say, variable name with a trailing number, copy it down and then, increment the number. So, after duplicating one line (with a nice little script) I would have a bunch of duplicate lines like:
variable1
variable1
variable1
variable1
What I really want is this:
variable1
variable2
variable3
variable4
I tried to come up with a Applescript solution to accomplish it. What I have now is this: I select the '1's in the first block with a rectangular selection. Then, I invoke a script, that increments that numbers by one. Here it is:
tell application "BBEdit"
set allTargets to selection
set counter to 0
repeat with oneTarget in allTargets
select oneTarget
try
set yeah_tmp to (get (selection as text) + counter)
set selection to yeah_tmp as text
set counter to counter + 1
end try
end repeat
end tell
This works quite well whilst the numbers do not cross a decimal border (9 -> 10, 99 -> 100, etc. - the character count is no longer valid if a former loop instance adds to the amount of existing characters). While this solution is good enough for most of my use cases I thought I might share this with the group and invite more fluent Applescript speakers to improve it.
Maybe this is even easier with Perl, Python & co. but I have no idea how rectangular selections are behaving when you throw them at one of these from BBEdit ...
looking forward to your ideas,
Roland
>Speaking of rectangular selections ...
Why? How much time and bother does it take simply to make such a selection?
>This works quite well whilst the numbers do not cross a decimal
>border (9 -> 10, 99 -> 100, etc. - the character count is no longer
>valid...
All the more reason to start out from better place.
>Maybe this is even easier with Perl, Python & co. but I have no idea
>how rectangular selections are behaving when you throw them at one
>of these from BBEdit ...
There are plenty of ways to achieve the result you want without using
rectangular selections. For example, start with a dialog asking what
common name you want for your variables, then a dialog asking how
many of the beasts you want and then have the script write what you
want at the insertion point.
With Perl you might simply select "variable 1 26" and run a script to
produce lines containing "variable_01" down to "variable_26" or
whatever you want. In any case rectangular selections don't come
into the picture.
JD
I wrote an Applescript to do this until I discovered the spreadsheet.
>I tried to come up with a Applescript solution to accomplish it.
>What I have now is this: I select the '1's in the first block with a
>rectangular selection. Then, I invoke a script, that increments that
>numbers by one. Here it is:
>
>tell application "BBEdit"
> set allTargets to selection
> set counter to 0
> repeat with oneTarget in allTargets
> select oneTarget
> try
> set yeah_tmp to (get (selection as text) + counter)
> set selection to yeah_tmp as text
> set counter to counter + 1
> end try
> end repeat
>end tell
>
>This works quite well whilst the numbers do not cross a decimal
>border (9 -> 10, 99 -> 100, etc. - the character count is no longer
>valid if a former loop instance adds to the amount of existing
>characters). While this solution is good enough for most of my use
>cases I thought I might share this with the group and invite more
>fluent Applescript speakers to improve it.
Assuming that you are have an IF command then just select the text
and have the suffixing as + tens + counter. (if you want to go over
99 the have a hundreds also). You set tens and hundreds to "". After
the set counter to counter + 1, test for counter=10. If it is, then
set counter to 0 and test for tens="", and use the result to set tens
to 1 or tens+1. Then if you want to go past 99, do the same test for
tens=10 and set hundreds to either 1 or hundreds+1 along with tens=0.
______________________________________________________________________
Hey Roland,
Script one will operate on the selected variable name OR let you enter a variable name IF no text is selected.
The format for the dialog is:
variableName<space>startNum<space>endNum
Given that it will produce an enumerated list and replace the selection or input at the cursor position.
Script two allows you to write the params in BBEdit like so:
someVariableName 1 10
Select the text, and run the script.
If you use Command-L to select the line the script will accommodate to the carriage return.
--
Best Regards,
Chris
------------------------------------------------------------------------------------------------
set varEnumList to {}
tell application "BBEdit"
try
tell front text window
set selectedText to contents of selection
if length of (words of result) = 1 then
set dialogDefaultAnswer to selectedText & " "
else
set dialogDefaultAnswer to ""
end if
tell me to set varListParams to text returned of ¬
(display dialog "'Variable_Name,Start_Num,End_Num'" default answer dialogDefaultAnswer)
set AppleScript's text item delimiters to " "
set varListParams to text items of varListParams
if length of varListParams ≠ 3 then
error "Wrong Input!"
else
set {varName, startNum, endNum} to varListParams
repeat with i from startNum to endNum
set end of varEnumList to varName & i
end repeat
set AppleScript's text item delimiters to return
set varEnumList to varEnumList as text
set selection to varEnumList
end if
end tell
on error errMsg number errNum
if errNum ≠ -128 then
set sep to "=============================="
set e to sep & return & "Error: " & errMsg & return & sep & return ¬
& "Error Number: " & errNum & return & sep
beep
display dialog e
end if
end try
end tell
------------------------------------------------------------------------------------------------
set varEnumList to {}
set returnSuffix to false
tell application "BBEdit"
try
tell front text window
set selectedText to contents of selection
if selectedText ends with return then
set selectedText to text 1 thru -2 of selectedText
set returnSuffix to true
end if
if length of (words of selectedText) = 3 then
set AppleScript's text item delimiters to " "
set varListParams to text items of selectedText
set {varName, startNum, endNum} to varListParams
repeat with i from startNum to endNum
set end of varEnumList to varName & i
end repeat
set AppleScript's text item delimiters to return
set varEnumList to varEnumList as text
if returnSuffix = true then
set varEnumList to varEnumList & return
end if
set selection to varEnumList
else
error "Wrong Input!"
end if
end tell
on error errMsg number errNum
set sep to "=============================="
set e to sep & return & "Error: " & errMsg & return & sep & return ¬
& "Error Number: " & errNum & return & sep
beep
display dialog e
end try
end tell
------------------------------------------------------------------------------------------------
Hi, Chris,
thank you so much for the two scripts that go even beyond the built in "Add/Remove Line Numbers" functionality. I'll add them to my utility arsenal and make good use of them. I also learned a little about Applescript. I had a hard time trying to modify the instances of the selection, but your approach using text lists and item delimiters before setting the selected text seems elegant and easy - once you know it.
So, thank you again and have a nice day
Roland
Yes I use a Numbers to do this myself.
Another way is to open a new BBEdit text document, paste the unnumbered variable names in there, then use "Add/Remove Line Number" in the "Text Menu. This will add line numbers at the beginning and you can just select them in a rectangular fashion and move them to wherever you like.
Patrick
On 12 Aug 2011, at 02:14, Lewis Kirk wrote:
> Uh, this is not a BBEdit solution, but in a spreadsheet program (Excel, OpenOffice), you can type in 'variable1' in a cell, then type in 'variable2' in the cell below it, select the cells, and then pull down as far as you want. The number will increment as you pull. Then copy and paste.
>
> I wrote an Applescript to do this until I discovered the spreadsheet.
>
> --
>Script one will operate on the selected variable name OR let you
>enter a variable name IF no text is selected...
Place the cursor anywhere in a line consisting of "variable1"
This script supposes you want n similar variables with incrementing
indices. You fill in n in the dialog and the required lines are
created:
variable1
variable2
variable3
variable4
What's more, the same script could be used for far more complicated
operations. No need to fill down, no need to spend waste seconds
making a rectangular selection. Quick, short and recyclable.
tell application "BBEdit"
set _currentline to the startLine of the selection
tell the front document to select line _currentline
set _s to the contents of line _currentline of the front document
tell me to set _dd to display dialog "
Create how many variables in series '" & _s & "'" default answer "4"
set _n to text returned of _dd
set _perloutput to do shell script "perl <<'END'
$count = " & _n & ";
$s = qq~" & _s & "~;
$_ = " & _s & ";
/([^\\d]+)(\\d)/i and ($varname, $n) = ($1,$2);
for (1..$count) {print qq~$varname$_\\n~}
END"
set the selection to _perloutput
select insertion point after selection
end tell
--JD
What are you, some sort of AppleScript ninja?
;)
(yes, I know, thus the winky smile)
Place the cursor anywhere in a line consisting of "variable1"
>On Aug 12, 2011, at 11:50, John Delacour wrote:
>
>>Place the cursor anywhere in a line consisting of "variable1"
>
>...the line content is not propagated on my system. Starting with
>the cursor in the variable line the variable gets eaten and the
>result:
>
>someVariable
>-->
>1
>2
>3
>4
To repeat the line you quote, Chris:
At 17:50 +0100 12/08/2011, I wrote:
>Place the cursor anywhere in a line consisting of "variable1"
... or someVariable1 or var_1 if you like. If you want to start with
something not ending with an integer then the script would need to be
slightly different; it hasn't got eyes in the back of its head :-;
JD
______________________________________________________________________
Aha! I missed that. Thanks John. :)
--
Chris