Styles 'Document' not a regular object? Error unknown macro command.

Get help using and writing Nisus Writer Pro macros.
Post Reply
mpawp
Posts: 3
Joined: 2019-11-15 11:03:03

Styles 'Document' not a regular object? Error unknown macro command.

Post by mpawp »

Hi,

I have written a macro to style text based on markup, but Nisus Writer Pro 3.0.4 reports a difficult to comprehend error:
Unknown macro command:
Document.styleWithName "Emphatic"


From the Macro Language reference:
Style Object
A Style object represents a single user-defined style which collects together a group of text attributes in a particular document. Style objects can be retrieved using:
Style.editingStyles
Document.allStyles
Document.styleWithName <———
Selection.characterStyle
[…]
.styleWithName name v1.2
Returns the Style object with the given name, or the undefined value if no such style exists.

The document is a standard Nisus Writer Pro document with the emphasis and bold styles added to the document’s collection of Character styles. Is perhaps ‘Document’ not regular object or am I missing something? 'Read Document.styleWithName' results in the same error message.

I would be grateful for some assistance.

###### the Macro ########
$aDoc=Document.Active
$range = Range.new(0,$aDoc.text.length)
$selection = TextSelection.new($aDoc.text, $range)
$aDoc.setSelection($selection)
$data = $aDoc.selection.substring

$styles = Hash.new
$styles["Emphatic"] = Document.styleWithName "Emphatic"
$styles["Strong"] = Document.styleWithName "Strong"

Begin Perl
$data =~ s/(@@@)(emph|bld)\((.+?)\)((?:emph|bld)_end)/$1${2}&&&&&&&&&&&&&&&${3}${4}&&&&&&&&&&&&&&&/gs;
End

$aDoc.text.replaceInRange $range, $data

$aDoc.find("@@@emph&&&&&&&&&&&&&&&.+?emph_end&&&&&&&&&&&&&&&", "E")
$styles["Emphatic"].apply
$aDoc.findAndReplace("@@@emph&&&&&&&&&&&&&&&(.+?)emph_end&&&&&&&&&&&&&&&", “\\1”, "aE")


$aDoc.find(@@@bld&&&&&&&&&&&&&&&.+?bld_end&&&&&&&&&&&&&&&, 'E')
$styles["Strong"].apply
$aDoc.findAndReplace("@@@bld&&&&&&&&&&&&&&&(.+?)bld_end&&&&&&&&&&&&&&&", “\\1”, "aE")


Regards,
Martin
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Styles 'Document' not a regular object? Error unknown macro command.

Post by phspaelti »

Hello Martin,
The command will need to be applied to a specific instance of a document object. You can't apply it to the constructor. So in your case it would presumably have to be:

Code: Select all

$styles = Hash.new
$styles["Emphatic"] = $aDoc.styleWithName "Emphatic"
$styles["Strong"] = $aDoc.styleWithName "Strong"
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Styles 'Document' not a regular object? Error unknown macro command.

Post by phspaelti »

And on a more general note, may I ask what you are trying to do here? It looks like you are using a very baroque form of style markup. But why are you going to the trouble of first inserting this markup using perl only to remove it again after applying the style? If you skip all that perl you won't really need the complex construction of a selection just to select the whole document. In fact even for that you don't need it. Select All should work fine.

Anyhow, you will likely find a few more errors in your current code.
  • Hashes need {} not [] for the index
  • The .find command will work only for text objects not document objects
But most of all, you will be disappointed by the lack of an effect from the .apply command. For this to work you need to actually select something. The .find command does not create selections. It works on the text object. To get a selection you will need to either set the selection result obtained from the .find command, or use the menu command Find.
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Styles 'Document' not a regular object? Error unknown macro command.

Post by phspaelti »

So here is my suggestion how to do what you seem to want:

Code: Select all

$aDoc=Document.Active
$styles = Hash.new
$styles{"Emphatic"} = $aDoc.styleWithName "Emphatic"
$styles{"Strong"} = $aDoc.styleWithName "Strong"

$bits = $aDoc.text.findAndReplace('@@@emph\((.+?)\)emph_end', '\1', 'Ea')
$aDoc.setSelection $bits
$styles{"Emphatic"}.apply

$bits = $aDoc.text.findAndReplace('@@@bld\((.+?)\)bld_end', '\1', 'Ea')
$aDoc.setSelection $bits
$styles{"Strong"}.apply
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Styles 'Document' not a regular object? Error unknown macro command.

Post by phspaelti »

Addendum: Actually the above macro will only work correctly, if both types of mark-up are indeed present. To have it work correctly for all cases, one should wrap the style application in a test to avoid having it applied to the wrong thing.

Code: Select all

$bits = $aDoc.text.findAndReplace('@@@emph\((.+?)\)emph_end', '\1', 'Ea')
if $bits.count
    $aDoc.setSelection $bits
    $styles{"Emphatic"}.apply
end
philip
Post Reply