Follow-Up Question to My Post "Text Variables": How Do I Attach a Character Style to a Custom Property?

Everything related to our flagship word processor.
Post Reply
ninjagame
Posts: 152
Joined: 2004-11-27 09:38:42
Location: Hamburg, Germany

Follow-Up Question to My Post "Text Variables": How Do I Attach a Character Style to a Custom Property?

Post by ninjagame »

http://nisus.com/forum/viewtopic.php?f=18&t=7276
How do I attach a character style to a Custom Property I have created?
In order to clarify: Let's say I've created a Custom Property with the value "whatever". I've also attached a shortcut to it. When I apply he shortcut, the value of the Custom Property ist displayed - but only in Plain Text.
But I want "whatever" to be displayed in italics, as in whatever. How do I do this (if possible, that is)?
adryan
Posts: 561
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Follow-Up Question to My Post "Text Variables": How Do I Attach a Character Style to a Custom Property?

Post by adryan »

G’day, ninjagame et al

As I understand it, Document Properties (including Custom Document Properties) are essentially format-free chunks of text. Inserting a Custom Property into a document is akin to using the Paste Text Only command. So you will need to apply any desired special formatting (Styles or whatever) to each occurrence of the Property in the document, preferably when you first insert it. Subsequently changing the value of the Custom Property will change every occurrence of it in the document, but each occurrence will retain the formatting you initially assigned it.

When you think about it, it’s probably not desirable to have Document Properties tied to particular formatting. One can envisage circumstances in which, for example, a document Title (inserted as a Document Property) might need to be displayed with different formatting in different locations in the document.

It’s worth noting that, as far as I can determine at any rate, there is no way to Find an occurrence (or all occurrences) of an inserted Document Property in a document, even via the Macro Language. Placing the insertion point in an occurrence results in selection of the entire chunk of text determined by a Document Property, whence the particular Property applicable here can be seen in the Formatting Examiner Palette. (Note that there appear to be bugs in the Formatting Examiner Palette. It fails to show, for example, all fonts used in selected text. Nor does it indicate that a chunk of text governed by a Document Property lurks within a wider text selection.) But this is hardly a convenient way to find such items; hence my advice to apply any desired formatting at first insertion.

It’s important to note, too, that inserting a Custom Property into a document differs from using the Paste Text Only command in one crucial respect; viz, it does not overwrite multiple non-contiguous selections. Remedying this might be worthwhile in a future update.

I hope this helps.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
ninjagame
Posts: 152
Joined: 2004-11-27 09:38:42
Location: Hamburg, Germany

Re: Follow-Up Question to My Post "Text Variables": How Do I Attach a Character Style to a Custom Property?

Post by ninjagame »

G'day, adryan

Thanks again for the quick answer.
On second thought, and being familiar with Adobe InDesign's handling of text variables, I figured as much.
Here's why I asked the question in the first place: If memory serves right, with Nisus Classic I could achieve the desired result using macros that not only produced the character string as such but also with formatting elements. For example, a space ship's name was to be displayed in italics and in All Caps.
Now I'm wondering if a similar result might be possible with NWP.

Have a nice day!

ninjagame
adryan
Posts: 561
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Follow-Up Question to My Post "Text Variables": How Do I Attach a Character Style to a Custom Property?

Post by adryan »

G’day, ninjagame et al

Yes, you can create a macro that inserts pre-defined text, complete with Styles.

Here’s a quick-and-dirty example that is fairly self-explanatory and doesn’t require familiarity with more technical aspects of the Macro Language. It assumes the document has access to a Character Style named “Style_1”. This Style might include such things as font, font size, color, italicization, small caps, etc. You can change the name of this Style to anything you like, just so long as its names in the macro and in the document are the same. You specify the text to be inserted in the obvious place in the macro.

Code: Select all

$string_1 = "My first chunk of text"

Type Text $string_1
Find $string_1, 'Er'
Format:Character Style:Style_1
Place these commands in a blank NWP document and Save As a Nisus Macro in an appropriate place in the Macros Folder.

Just to continue the example of a document title’s being formatted differently in different places in the document: You could have several of these macros, each pertaining to a different Style yet containing the same (title) string. One advantage of this whole approach is that, in future, you can readily Find All occurrences of the string of any given Style.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
ninjagame
Posts: 152
Joined: 2004-11-27 09:38:42
Location: Hamburg, Germany

Re: Follow-Up Question to My Post "Text Variables": How Do I Attach a Character Style to a Custom Property?

Post by ninjagame »

G'day, adryan

Thank you so very much for your "quick'n'dirty" solution! It works like a charm!
So I have to give up on the Document Property features. But, as you wrote, it might not be a good idea to have Document Properties tied to particular formatting anyway.
Well, can't be helped. But that's a small price to pay.

Have a good day!

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

Re: Follow-Up Question to My Post "Text Variables": How Do I Attach a Character Style to a Custom Property?

Post by phspaelti »

adryan wrote: 2018-11-11 00:31:59Here’s a quick-and-dirty example…
Hello Adrian,
that's a good solution. Just wanted to give two small pointers:
1. If you want to add some text to a document and immediately select it, why not use the "Insert Text" command (instead of "Type")?
2. If you want to search for a text string in a document, it's generally a better idea to use Normal Find (i.e. not use the "E" option). This is because if your text string contains any of the special characters (">+-?[]()" etc.) you might not find what you are looking for.
philip
adryan
Posts: 561
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Follow-Up Question to My Post "Text Variables": How Do I Attach a Character Style to a Custom Property?

Post by adryan »

G'day, Philip et al

Of course you are right on both counts, Philip.

I guess I used Power Find Pro because, while I might very occasionally use Normal Find in direct Find/Replace situations, I almost always need grep in macro situations.

Using the Insert Text command means we can dispense with the Find operation here entirely.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Follow-Up Question to My Post "Text Variables": How Do I Attach a Character Style to a Custom Property?

Post by martin »

You can insert a custom document property that retains its dynamism (ie: the value will automatically update) and apply a style. There's no need to sacrifice automatic updates.

I recommend that you do this either using Glossaries or a more refined macro. Using a Glossary entry is straightforward: just add an abbreviation that expands into the variable with formatting already applied. That requires a separate Glossary entry for each desired property variable and style pairing. A macro can let you pick each component separately at the time of insertion:

Code: Select all

# This macro inserts a document property variable and applies a character style.
$doc = Document.active
$props = $doc.properties
$details = 'The property value will be inserted as an automatically updating variable.'
$propKey = Prompt Options "Insert which document property?", $details, "Insert...", $props.keys

# check if the key exists as a custom property
$prefix = ":Insert:Document Property"
$customPath = "$prefix:Custom Property:$propKey"
If Menu.existsAtPath($customPath)
	Menu $customPath
Else
	# fallback to regular non-custom property
	Menu "$prefix:$propKey"
End

# select the inserted variable
$select = $doc.textSelection
$backwards = TextSelection.newWithLocationAndLength( $select.text, $select.location - 1, 1 )
$doc.setSelection($backwards)

# ask the user if they'd like to apply a style
$styles = $doc.characterStyles
$styleNames = $styles.arrayByMakingValuesDoCommand("name")
$styleName = Prompt Options "Apply a style?", '', "Apply", $styleNames
Menu ":Format:Character Style:$styleName"
If you only use a few document property and style combinations, you could create separate macros for each property and style pairing. But then maybe I'd just go back to the idea of using Glossary entries.
Attachments
Custom Property Inserter.nwm.zip
(2.67 KiB) Downloaded 561 times
ninjagame
Posts: 152
Joined: 2004-11-27 09:38:42
Location: Hamburg, Germany

Re: Follow-Up Question to My Post "Text Variables": How Do I Attach a Character Style to a Custom Property?

Post by ninjagame »

Martin,

Thank you very much for the trouble you took creating the really refined macro. I'll look into it over the weekend in order to decide what works best for me and get back to you.

Best,

ninjagame (Detlef)
Post Reply