Why is this code incorrect?

Get help using and writing Nisus Writer Pro macros.
Post Reply
Þorvarður
Posts: 236
Joined: 2012-12-19 05:02:52

Why is this code incorrect?

Post by Þorvarður » 2015-09-25 21:31:03

I have a macro that has always worked flawlessly. Today I keep getting an error message regarding "unexpected text in macro source code":
1.png
1.png (28.49 KiB) Viewed 4109 times
It's the orange colored part that is the culprit.
2.png
2.png (12.64 KiB) Viewed 4109 times
Is this due to some new changes in the macro language? Is this not anymore part of the macro language?

I checked "&=" and "&" in Nisus Macro Reference (ca. p. 5), but their use seem not to have changed from previous versions.

User avatar
phspaelti
Posts: 889
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Why is this code incorrect?

Post by phspaelti » 2015-09-25 23:22:18

This has never been part of the macro language. In fact it doesn't even make sense. It looks that somehow an accidental '=' snuck in there. The "&=" should just be an "&".

Code: Select all

$path = $path & '/' & $fileName

I might also note that after that correction this piece of code will work fine in NW 2.1. but not in earlier versions. In earlier versions, one could have written something like this:

Code: Select all

$path &= '/' & $fileName
(At least, I'm pretty sure that would have worked. Don't have an older version around to test it.) But this will work fine in 2.1 as well.

So from that you can see how the wrong code might have come about.
philip

User avatar
phspaelti
Posts: 889
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Why is this code incorrect?

Post by phspaelti » 2015-09-25 23:29:51

A second thought:
What happens before the offending line? How is $path set? I'm wondering if the original code was something like this:

Code: Select all

$path = ???
$path &= '/' & $fileName
where '???' and (the return) have somehow gone missing…
In pre-NW 2.1. this kind of code was the common way to write what one can now write as:

Code: Select all

$path = ??? & '/' & $fileName
NB: '???' might be something like a variable $folderName.
philip

Þorvarður
Posts: 236
Joined: 2012-12-19 05:02:52

Re: Why is this code incorrect?

Post by Þorvarður » 2015-09-26 00:47:58

phspaelti wrote:This has never been part of the macro language. In fact it doesn't even make sense.
Here is the whole macro. It's based on a macro written by Kino, but I guess I must have screwed it up. A little knowledge is a dangerous thing. :wink:

To achieve the same goal, a much shorter and simpler macro would of course be enough:

Code: Select all

$myAnswer = Prompt 'Achtung! Achtung!', 'Do you really want to save this file as Nisus New File.dot?', 'No', 'Yes'
If $myAnswer == "No"
		Exit
	else
		Save As '/Users/Homefolder/Library/Application Support/Nisus Writer Pro/Nisus New File.dot'
End
I remember now that the original purpose was also to open the enclosing folder, i.e. /Users/Homefolder/Library/Application Support/Nisus Writer Pro. The macro has been working fine since 2013; therefore I was surprised that it now suddenly stopped working. :P
Attachments
Save As %22Nisus New File%22 Template in Library.nwm.zip
(4.23 KiB) Downloaded 79 times

Þorvarður
Posts: 236
Joined: 2012-12-19 05:02:52

Re: Why is this code incorrect?

Post by Þorvarður » 2015-09-26 00:58:17

phspaelti wrote:The "&=" should just be an "&".

Code: Select all

$path = $path & '/' & $fileName
I changed this one line, and happy days are here again!!! :D
https://www.youtube.com/watch?v=UH01FhqMdc8

User avatar
phspaelti
Posts: 889
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Why is this code incorrect?

Post by phspaelti » 2015-09-26 01:14:41

I see. Looking at the code, the macro does seem to have to some history :)
And in fact there is quite a bit of stale code, including some that may have unintended consequences. So I would recommend that you trash it and replace it with your simpler version. That way you can be sure you know what it's doing.

But one last point, in NW 2.1 the location of the Nisus New File is now in the ~/Nisus Documents/Templates/' folder…
philip

Þorvarður
Posts: 236
Joined: 2012-12-19 05:02:52

Re: Why is this code incorrect?

Post by Þorvarður » 2015-09-26 02:46:53

phspaelti wrote:in NW 2.1 the location of the Nisus New File is now in the ~/Nisus Documents/Templates/' folder…
It's good that you mention this. Is the "Nisus Documents" folder also not more inside the Documents folder? Mine is here:
/Users/Home/Documents/Nisus Documents

Which files should now be in the "Nisus Writer Pro" folder (< Application Support < Library < Home)?

I have:
1 Icons folder (with icons used for the Toolbar)
2 Idiomas (empty folder)
3 InputMethodIcons (folder with PNG image files to display the input method)
4 Języki (empty folder)
5 Languages (Unix Executable Files) and Hyphenation subfolder
6 LibreImporter (with two subfolders: LibreOffice and a folder called NisusInputFiles, which is empty)
7 Sprachen (folder with Unix Executable files)

8 Document Manager Settings
9 Document Manager Window Settings
10 Font Menu Cache
(8-10 are all Unix Executable Files)

11 Plain Text Template.dot
12 Recently Opened Documents.plist
13 Recently Saved Documents.plist
14 Reopen Documents.plist
15 Special Characters.plist
16 uniqueSequenceNumber.txt

User avatar
phspaelti
Posts: 889
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Why is this code incorrect?

Post by phspaelti » 2015-09-26 07:35:57

Hi Þorvarður,
Sorry. I didn't mean to confuse things, I was just being casual. Yes, my 'Nisus Documents folder' is also in 'Documents' (not in the home folder.)

As to the Application Support folder; I wouldn't worry about it. NWP takes care of it. The contents is basically as you say:
  • 3 folders (Languages, LibreImporter, InputMethodIcons)
  • The plist files for the document manager and the font cache
  • 'Original' versions of the template files, and a 'Macros' folder
The template files and the 'Macros' folder are not relevant. I once asked Martin about them, but I forgot what he said. :) (They may be some kind of 'backup' to restore the 'factory settings')

The extra folders you are seeing ('Idiomas', 'Sprachen', 'Języki') are presumably the result of you starting up Nisus with different localizations (Spanish, German, Polish).
philip

Þorvarður
Posts: 236
Joined: 2012-12-19 05:02:52

Re: Why is this code incorrect?

Post by Þorvarður » 2015-09-28 07:19:48

phspaelti wrote:This has never been part of the macro language. In fact it doesn't even make sense.
Philip, you say the operator "&=" does not exist and would make no sense; but what does it then mean in the Macro Reference when it says:
"both values are treated as strings and concatenated (eg: the string on the right of the operator is appended to the string on the left)" ?

Is "&=" perhaps an old, not more valid operator that they have just forgotten to remove from the Macro Reference?

I looked through my old macro notes and found this (see screenshot). Since the Reference gives no examples, I made up my own, and we can see on the screenshot that it worked as described. But you are right, now it doesn't function anymore.

https://copy.com/1FacId7kFt4DnBQA

User avatar
phspaelti
Posts: 889
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Why is this code incorrect?

Post by phspaelti » 2015-09-28 07:32:36

Þorvarður wrote:Philip, you say the operator "&=" does not exist and would make no sense;
No, I never said that. What doesn't exist/make sense is the sequence of operators:

Code: Select all

$a = $a &= …
Þorvarður wrote:Is "&=" perhaps an old, not more valid operator that they have just forgotten to remove from the Macro Reference?
The "&=" operator is perfectly valid. It's a string append operator.
philip

Þorvarður
Posts: 236
Joined: 2012-12-19 05:02:52

Re: Why is this code incorrect?

Post by Þorvarður » 2015-09-28 22:56:57

The "&=" operator is perfectly valid.
Thank you Philip for the clarification.
It's a string append operator.
Could you give me examples of how to use this correctly? It would be very helpful.

Actually, I assumed I was appending one string to another string, but neither [1] nor [2] work.

[1]
$a = 'The Macro Reference is poorly written. '
$b = 'There are no examples.'
$addition = $a &= $b
Prompt $addition

[2]
$addition = "The Macro Reference is poorly written. " &= "There are no examples."
Prompt $addition

User avatar
phspaelti
Posts: 889
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Why is this code incorrect?

Post by phspaelti » 2015-09-29 01:10:04

Þorvarður wrote:Could you give me examples of how to use this correctly? It would be very helpful.

Actually, I assumed I was appending one string to another string, but neither [1] nor [2] work.

[1]
$a = 'The Macro Reference is poorly written. '
$b = 'There are no examples.'
$addition = $a &= $b
Prompt $addition

Code: Select all

$a = 'The Macro Reference is poorly written. '
$b = 'There are no examples.'
$a &= $b
Prompt $a
Þorvarður wrote: [2]
$addition = "The Macro Reference is poorly written. " &= "There are no examples."
Prompt $addition

Code: Select all

$addition = "The Macro Reference is poorly written. " 
$addition &= "There are no examples."
Prompt $addition
The '&=' operator will append to the existing string variable. So the line always has to start '$variable &=', and $variable has to exist, for it to be valid.
With versions of Nisus before 2.1 this operator was very important, because the macro language had a rule that limited one to a single operator per line. So if you wanted to add three or more strings together, you always needed to do it in steps:

Code: Select all

$example = 'This ' & 'is a '
$example &= 'stupid ' & 'example!'
Note how the '&=' operator even allows you to 'bend' the single operator rule, since that last line actually adds three things together.
But now in 2.1 you can write this directly as a single assignment, so the importance of '&=' is reduced:

Code: Select all

$example = 'This ' & 'is a ' & 'stupid ' & 'example!'
philip

Þorvarður
Posts: 236
Joined: 2012-12-19 05:02:52

Re: Why is this code incorrect?

Post by Þorvarður » 2015-09-29 03:36:37

So the line always has to start '$variable &='
So that was the crux of the matter! Now I understand. I think the Nisus people may have forgotten to make this clear in the Macro Reference.

Thank you also very much for the example, which, by the way, was not so stupid at all. :-)

Post Reply