Page 1 of 1

Variable from clipboard to perl

Posted: 2008-08-27 01:27:03
by dakofi
I get the variables with Nisus Writer Pro macro as follow into clipboard 1 and clipboard 2

#counter für z.B. 30-35
Find and Replace ‘([[:digit:]]+)\-([[:digit:]]+)’, ‘\1\2’, ‘aEU’ #Bis-Nummer suchen
Begin Perl

I need to count them up to get the result 30.31.32.33.34.35 back into the text.

Unfortunately I don't know how to give the parameters 30 and 35 from the clipboard to pearl nor to count up with perl, because I never used perl before.

Thank you for your help

dakofi

Re: Variable from clipboard to perl

Posted: 2008-08-27 05:30:18
by Kino
dakofi wrote: Find and Replace ‘([[:digit:]]+)\-([[:digit:]]+)’, ‘\1\2’, ‘aEU’ #Bis-Nummer suchen
Begin Perl
Your code does not put anything in clipboards and you don't need using Perl to do calculation of that kind. What you are trying to do is not very clear to me but am I right in assuming you want to convert all occurrences of number ranges such as "30-35" into "30.31.32.33.34.35" or something alike? If so, the following macro will do the job.

Code: Select all

 # Work on selection(s). If there is no selection, the whole document
# will be processed.

# Expand, for example, 1-3, 22-25, etc. to "1.2.3", "22.23.24.25", etc.

# You can customize the separator -- "." in the examples above -- by
# changing « . » in « $separator = '.' », the very first command of
# this macro.

$separator = '.'

Require Application Version '3.1'

$firstSel = TextSelection.active
if $firstSel == undefined
	Exit 'No open document, exit...'
elsif ! $firstSel.length
	Select All Document
end
$doc = Document.active

$numFound = Find All '[0-9]+\-[0-9]+', 'Es'
if ! $numFound
	Exit 'Nothing found, exit...'
end
$selections = $doc.textSelections

foreach $sel in reversed $selections
	$nums = $sel.subtext
	$nums = $nums.split '-'
	$i = $nums.firstValue
	$j = $nums.lastValue
	if $i < $j
		$c = Array.new
		while $i <= $j
			$c.appendValue $i
			$i += 1
		end
		$c = $c.join $separator
		$c = Cast to String $c
		$sel.text.replaceInRange $sel.range, $c
	end
end

Re: Variable from clipboard to perl

Posted: 2008-08-28 00:11:24
by dakofi
Hi Kino

Thank you very much for solving my problem.
I guess that your macro is written in perl, isn't it?
Where can I learn more about the macro language?

regards

dakofi

Re: Variable from clipboard to perl

Posted: 2008-08-28 03:07:23
by Kino
dakofi wrote:I guess that your macro is written in perl, isn't it?
No, no, there is no Perl code in it. Nisus Pro macro language having been enchanced a lot in NW‌ Pro 1.1, we seldom need Perl.
Where can I learn more about the macro language?
Nisus Macro Reference.rtf you'll find in the "Read Me" folder of NW Pro dmg/zip may look somewhat difficult to understand. Unfortunately a tutorial is not available yet. I think the best way to learn it would be to write your macros by modifying macros written by others, using them as a kind of template. (My macro above is not a good example for it is a bit tricky.) Official macro repository is http://nisus.com/pro/macros/.
Navigating in this forum, you'll be able to find other many macros posted here.

Re: Variable from clipboard to perl

Posted: 2008-08-28 04:24:07
by dakofi
Hi Kino

Thank you very much for your reply.

regards

dakofi