Page 1 of 1
Macro Editing request
Posted: 2025-08-11 06:33:23
by Windsor
Hello everyone,
I have a macro that couldn't use it. An error message comes up when I launch it. It says:
Unknown menu item path: Select Range $nextPos, 0
I am attaching the macro.
Could someone, please, see if it is fixable and fix it?
Thanks,
Nerses
Re: Macro Editing request
Posted: 2025-08-11 08:55:09
by Þorvarður
Hello Nerses,
I'm afraid you forgot to attach the macro…
Þ.
Re: Macro Editing request
Posted: 2025-08-11 18:10:53
by Windsor
Actually, I did. Here I will do it again. Thanks for the attention.
I hope this time it will be seen.
Thanks,
Nerses
Re: Macro Editing request
Posted: 2025-08-12 20:51:43
by phspaelti
Where to begin?
The line
is indeed not a known menu item.
The
Select type commands strangely do not have any way to select by range or position. You could change that line to
Code: Select all
Adjust Selection Start By $range.length
Then the macro should complete.
But you won't be happy with the result. That's because the line
does not give you the word. It gives you the whole text of the document! And since there is only one document, your wordlist will have only 1 "word".
You can fix that problem as follows:
But don't try this on a really long document. It will give you a nice show of leisurely walking through your document, one word at a time. Perfect for a tea break!
There are two problems here:
- You are doing all the work via the GUI, which is sloooow
- You are switching over to Perl for each word in the list
I recommend getting rid of both of these things. The second of these is actually a problem for another reason. The Perl lines are not going to do what you want either. Let's say you have a word with a soft-hypen "mes[-]sage". The Find command of your macro will not actually find this word as a word. It will find it as two 'words': "mes" and "sage", and there won't be any soft-hyphen to remove. So actually you should really remove those characters on the whole text before you start.
And finally the best way to get a list of unique items is to use a Hash, instead of an Array. Hash keys are required to be unique, which will give you what you want for free.
Adopting all these modifications, you will get:
Code: Select all
# NisusMacroProperty Name: Word List Case-Sensitive with Hyphen/En-Dash Handling (Bulletproof End)
$doc = Document.active
If ! $doc
Prompt "No document open — open a document and re-run the macro."
Exit
End
$matches = Hash.new
$pattern = '([\p{L}\p{M}\d\p{Pd}]+)'
$text = $doc.text.copy
$text.findAndReplace '[\x{00AD}\x{200B}\x{200C}\x{200D}\x{FEFF}]', '', 'Ea'
$words = $text.findAll($pattern, 'Ea').arrayByMakingValuesDoCommand 'substring'
foreach $word in $words
$matches{$word} = 1
end
$wordlist = $matches.keys
# Sort results case-sensitively
Define Command CompareStrings( $a, $b )
If $a == $b
Return 0
End
If $a < $b
Return -1
End
Return 1
End
$wordlist.sortWithCommand( @command(CompareStrings) )
# Output to new doc
Document.newWithText $wordlist.join("\n")
Prompt "Created word list — new document contains " & $wordlist.count & " entries."
Re: Macro Editing request
Posted: 2025-08-12 21:55:22
by Windsor
Phillip
I cannot thank you enough! Wow! Yes, you're right, this macro is with the speed of light!
Could I ask you for a minor editing:
I ran this macro on a sample "test drive" document, which includes English and Armenian paragraphs. Just to test it. I realized that I need to add a line in the macro to not to split the words that have Armenian punctuation marks. See, the Armenian punctuation is syllabic, it is not at the end of the word like English. For example if you want to ask the question "What?" (as you can see the question mark is at the end of the word). In Armenian it is at the end of the last syllable (vowel). If the word is one syllabic, then on the only vowel. So, for the word What? the Armenian is Ի՞նչ. As you can see the question mark is between the first letter which I and the second letter which is N. (the whole word is Inch –> I?nch. So, could you, please, insert a line that the macro should NOT split a word that has Armenian punctuation marks?
Here are they:
՛ (accent/emphasis mark)
՜ (exclamation mark)
՞ (question mark)
՟ (abbreviation mark)
Thanks a million, in advance.
Nerses
Re: Macro Editing request
Posted: 2025-08-14 03:26:22
by phspaelti
Hello Nerses,
I wonder where you got this macro from.
Anyhow the crucial bit is the Find pattern:
Code: Select all
$pattern = '([\p{L}\p{M}\d\p{Pd}]+)'
Honestly I don't know these codes. The `\p{ … }' bits are Script blocks of the Unicode set, but usually they are spelled out, e.g., '\p{Latin}'. The '\p{L}' seems to be some very inclusive set of alphabetics, but I have no idea what \p{M} is.
Anyhow since you want Armenian you can just add it like this:
Code: Select all
$pattern = '([\p{L}\p{M}\d\p{Pd}\p{InArmenian}]+)'
Then it should work, since Unicode knows how Armenian works and it will include the relevant characters.
Good luck!