variables and interpolated literals

Get help using and writing Nisus Writer Pro macros.
Post Reply
credneb
Posts: 187
Joined: 2007-03-28 07:30:34

variables and interpolated literals

Post by credneb »

Getting back to learning the new macro language, I succeeded in writing a simple macro that even works. For reference, it's pasted below.

It simply builds a block of lines each starting with a number followed by a tab and newline character.
Following the line
$text = $prefix & $firstStep

I tried variations of
$text += "\t + \n"
$text & "\t + \n"
$text += "\t & \n"
and others to assemble the text added to the clipboard first. Some got complaints, but none succeeded in adding a tab and newline to the text added to the clipboard. (The pasted text was a single line.)

To add a tab or newline to text on the clipboard, do they have to be explicitly appended? Is there no way to assemble a string variable containing them that can then be appended to a clipboard?

Thank you.

Cliff Bender

***********
#NisusMacroProperty Name: _FlowChart Numbers

$message = 'What is the prefix?'
$prefix = Prompt Input $message, '', 'OK', 'S'

$message = 'What is the first number?'
$firstStep = Prompt Input $message, '', 'OK', '100'

$message = 'What is the last number?'
$lastStep = Prompt Input $message, '', 'OK', '10'

If $lastStep <= $firstStep
Die 'Sorry, the numbers are invalid.'
else
New Clipboard 'flowclip'
Switch to Clipboard 'flowclip'
Write Clipboard 'START'
Append to Clipboard "\n"
While $firstStep <= $lastStep
$text = $prefix & $firstStep
Append to Clipboard $text
Append to Clipboard "\t\n"
$firstStep = $firstStep + 1
end
Append to Clipboard "END \n"
Paste
end
Delete Clipboard 'flowclip'
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: variables and interpolated literals

Post by martin »

credneb wrote:To add a tab or newline to text on the clipboard, do they have to be explicitly appended? Is there no way to assemble a string variable containing them that can then be appended to a clipboard?
There is a way, use the "&=" operator to append a string to an existing variable, eg:

Code: Select all

$text = $prefix & $firstStep
$text &= "\t\n"
You could also do:

Code: Select all

$text = "$prefix$firstStep\t\n"
Also, some unsolicited advice: you don't need to use a temporary clipboard to build your result. You could simply use a text variable, which is going to be faster:

Code: Select all

$message = 'What is the prefix?'
$prefix = Prompt Input $message, '', 'OK', 'S'

$message = 'What is the first number?'
$firstStep = Prompt Input $message, '', 'OK', '100'

$message = 'What is the last number?'
$lastStep = Prompt Input $message, '', 'OK', '110'

If $lastStep <= $firstStep
	Die 'Sorry, the numbers are invalid.'
else
	$fullText = "START\n"
	While $firstStep <= $lastStep
		$fullText &= "$prefix$firstStep"
		$fullText &= "\t\n"
		$firstStep = $firstStep + 1
	end
	$fullText &= "END\n"
	Insert Text $fullText
end
Also, the "NisusMacroProperty Name" comment/directive isn't needed for macros authored by users. It's used internally by us to localized the display name of the macro for other languages, but unless you're also planning on updating all the NWP "lproj" folders to translate your macro title, the macro's file name should suffice.
credneb
Posts: 187
Joined: 2007-03-28 07:30:34

Re: variables and interpolated literals

Post by credneb »

Thank you very much for the help, AND for the unsolicited advice. It helps the learning process and I like fast...
Your revised version is much more elegant, and as an example it answers several questions.

Thanks again.
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: variables and interpolated literals

Post by martin »

Glad to help- happy macro writing!
Post Reply