Hello Nobumi,
On Dec 25, 2010, at 10:35 PM, Nobumi Iyanaga wrote:
> 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
Yeah, NWP macro does not have a proper command for that kind of verification. That is a bit annoying, but, in a certain sense, it is those shortcomings which make us more familiar with the macro language. Doing trials and errors to write a viable alternative, surely we learn something.
> $theLength = $theExpression.length
> $theLength = $theLength - 1
> $theRange = Range.new (0, $theLength)
You can condense these three lines into:
$theRange = Range.new (0, $theExpression.length - 1)
> $theExpression = $theExpression.substringInRange ($theRange)
For such a purpose, I think you can use join command advantageously, like in the macro below.
### another version ####
$doc = Document.active
$nums = $doc.selectedSubstrings
if $nums.count < 2
exit 'The macro requires two or more selected numerals, exiting...'
end
foreach $num in $nums
$check = $num * 1
if $check != $num
exit "\"$num\" is not a number, exiting..."
end
end
$op = Prompt Input 'Enter the operator', '', '', '+'
# Ideally we should check the validity of user input
# but I don't know what operators are supported in Perl...
$exp = $nums.join $op
$res = undefined
# In this kind of macros, Perl preambles would be overkilling but anyway...
Set Include Perl UTF Preamble false
Set Exported Perl Variables 'exp', 'res'
Begin Perl
$res = eval ($exp);
End
Write Clipboard "= $res"
exit "$exp = $res"
### end ###
Each time I see your $res, I imagine something Latin ;-)
Kino
|