Hello (especially Kino),
I just tried to write a little macro which can be useful (at least
for me). Its purpose is to calculate several non-contiguous selected
digits according to an arithmetic operator that you are prompted to
enter. For example, if you select "123" and "2", and if enter "*" at
the prompt, it will display "123*2 = 246", and it will put in the
current clipboard the string "= 246". Here is this macro:
## non_contig_arith
# Select several digits in non-contuiguous selections;
# Enter the operator (default "+"); the macro calculates;
# displays the result and put it on the current clipboard.
$doc = Document.active
$selectedTexts = $doc.selectedSubstrings
if $selectedTexts[0] == ''
exit 'There is no selected text...'
end
if $selectedTexts.count < 2
exit 'There is only one selection...'
end
$theOperator = Prompt Input 'Enter the operator', '', '', '+'
$theExpression = ''
foreach $num in $selectedTexts
$theNum = Cast to Int $num
if $theNum == 0
if $num != '0'
exit "$num is not a digit"
end
end
$theExpression &= $num & $theOperator
end
$theLength = $theExpression.length
$theLength = $theLength - 1
$theRange = Range.new (0, $theLength)
$theExpression = $theExpression.substringInRange ($theRange)
$res = ''
Begin Perl
$res = eval ($theExpression);
End
$output = "= $res"
Prompt "$theExpression $output"
Write Clipboard $output
# End of macro
One thing that I could not understand is the value of
"$selectedTexts.count". I thought that it should be "undefined" if
there is no active selection, "0" if there is only one selection, and
"1 (or more)" if there are two or more than two selections. But it is
"1" even if there is no selection at all, and 2, if there is only one
selection. Is this normal?
Anyway, this is just an essay, and I would appreciate your thought on
it.
Thank you very much in advance.
Best regard,
Nobumi Iyanaga
Tokyo,
Japan
|