Macro to assign style

Get help using and writing Nisus Writer Pro macros.
Post Reply
johseb
Posts: 47
Joined: 2016-02-13 10:01:29

Macro to assign style

Post by johseb »

I cobbled togheter a macro to select and assign paragraph style using the keyboard.

Code: Select all

$doc = Document.active
if $doc == undefined
	exit  # exit silently
end

$selection = $doc.textSelection

$paras = $doc.paragraphStyles
# $charas = $doc.characterStyles

$theStyle = Prompt Options 'Select a style for ¶', '', 'Okay', $paras

$theStyle.apply
The macro works but it has a couple of issues:

1. The radio button selection list can be "navigated" only by up/down arrows and does not accept type-ahead to select an item.
The ideal solution would be something along the lines of the Command Palette you can find in several text editors (e.g. Sublime text). Possible to add such an interface element to the ones provided by macro language?
Screen Shot 2019-08-17 at 02.38.29.png
Screen Shot 2019-08-17 at 02.38.29.png (54.57 KiB) Viewed 12970 times
Screen Shot 2019-08-17 at 02.42.39.png
Screen Shot 2019-08-17 at 02.42.39.png (91.67 KiB) Viewed 12970 times
2. I can prepare another macro to assign character style but I would like to make a single macro listing both paragraph and character styles (I wasn't able to concatenate the two arrays defining the style lists). I would really appreciate any help.

Thanks in advance :)
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Macro to assign style

Post by phspaelti »

Re 2: the command you need would be

Code: Select all

$paras.appendItemsFromArray $charas
99% I’m typing this from my phone
philip
johseb
Posts: 47
Joined: 2016-02-13 10:01:29

Re: Macro to assign style

Post by johseb »

Hi Philip, thanks for you reply!

The exact code turned out to be:

Code: Select all

$paras.appendValuesFromArray $charas
Re my #1: let's see what Martin thinks about it. My opinion is that a Command Palette would be a valuable addition not only to the macro language but to to NWP interface as well.
It proved to be a handy and effective tool (that's why its presence is spreading among modern editors).
User avatar
xiamenese
Posts: 543
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Re: Macro to assign style

Post by xiamenese »

Can I ask what may be stupid questions? Why do you need a macro to do this? You can very easily create keyboard shortcuts for each of your styles in Nisus Preferences, so why would having to invoke a macro and then a combination of key strokes to select a given style be an improvement? To me it seems like re-inventing the wheel.

Mark
johseb
Posts: 47
Joined: 2016-02-13 10:01:29

Re: Macro to assign style

Post by johseb »

Hi Mark, maybe you're right but my problem with keyboard shorcuts is that I forget them all the time.

Of course I have shorcuts for the most used commands (e.g. Headings when it comes to styles) and I'm not trying to find an alternative to ⌘S or ⌘B. I was just exploring an alternative fot the less used commands/interactions.
My attempt is mainly based on Keyboard Maestro macros/palettes so I'm not using the radio button list prompt shown on my first post. I didn't mention that to avoid confusion since it has nothing to do with NWP macro language.

Unfortunately it's difficult to obtain the results you can get from a native Command Palette (featuring fuzzy matching) so I'll also try to make a better use of NWP keyboard shorcuts system and leverage the multikey shortcuts to build a mnemonically effective scheme.

Ah, I forgot: it was also an occasion to see how NWP macros work and start using them (never done before); so bear with me if you think it was a simplicistic or silly attempt.
User avatar
martin
Official Nisus Person
Posts: 5228
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Macro to assign style

Post by martin »

I think a macro command showing a UI with fuzzy/partial text matching and filtering would be a useful addition. I can foresee other situations where such a feature would be useful. Thanks for the idea, I'll file it as a potential future enhancement.
johseb wrote: 2019-08-18 04:53:08Unfortunately it's difficult to obtain the results you can get from a native Command Palette (featuring fuzzy matching) so I'll also try to make a better use of NWP keyboard shorcuts system and leverage the multikey shortcuts to build a mnemonically effective scheme.
I'll share my workflow with you, which should get you most of what you want. Naturally it's best when frequently used styles have unique shortcuts, eg: ⌘H1 for Heading 1. But as you said, it's hard to remember shortcuts for all infrequently used styles.

What I do is have a keyboard shortcut for the entire Paragraph Style menu. I use ⌘PS for the menu Format > Paragraph Style. If you trigger the keyboard shortcut you'll see the full menu appear, which then allows further typing to match entries on that menu, eg: type "T" to select the "Title" style on the menu, and Return to apply it. Likewise I have keyboard shortcuts for the other style menus, eg: ⌘CS for Character Style.

I hope that helps!
johseb
Posts: 47
Joined: 2016-02-13 10:01:29

Re: Macro to assign style

Post by johseb »

Martin, your suggestion will be indeed useful. Thank you!
Your worflow shares with a Command Palette the same "type ahead" concept; a palette will only be "smarter".
Post Reply