Aplying bold, italic, normal without using menu

Get help using and writing Nisus Writer Pro macros.
Post Reply
Pat
Posts: 28
Joined: 2009-09-04 13:11:25

Aplying bold, italic, normal without using menu

Post by Pat » 2009-12-08 06:12:42

Hi again,

This very simple. Up to now, when I want to apply a bold attribute I just write "Bold" in the macro. However, when I change the language of my computer (from english to french), the names in the format menu are not the same and the macro doesn't work anymore. Also, sometimes I would like to make sure that the found expressions is neither bold or italic but there is no "regular" command in the format menu and the macro does not seem to find the commands in the tooldrawer. For me, it would be also useful to know how to apply "superscript", "subscript", "underline", etc. to a found expression.

Thank you,

Pat

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

Re: Aplying bold, italic, normal without using menu

Post by phspaelti » 2009-12-08 08:24:33

At some point it might be good to know what you are trying to do with your macro. I generally try to write my macros so they work with the kinds of things that Nisus can do easily.

Previously Henry suggested using Find and Replace to apply the formatting directly. Note that when you do it that way you in fact won't have any problem with the localization. So

Code: Select all

Find and Replace $findstring, '\0', 'EaU'
will apply any formatting that you apply to the '\0' string. This will work for subscript as well as for bold, italic, etc. A limitation is however that this does not work to *remove* such formatting.

A more elaborate coding solutions could be done with the "Localize" command like this:

Code: Select all

$doBold = Localize “Bold”
Menu $doBold
I haven't really tested this with another language, but if it works as advertised it should translate the "Bold" string into your current language.

To turn "bold" etc. off you could first check the menu with the "Menu State" command so the following will turn bold off.

Code: Select all

If Menu State “Bold” 
Bold
end
But overall I am wondering whether you might not be better off working with (user defined) styles. The style names will be immune to localization, and you can easily change their definitions to get different results. You could keep the set of styles in a style library. It's very easy to import a whole collection of styles that way. Styles are also exclusive, which may be a plus (or a minus) depending on the situation.

Hope this helps
Philip
philip

Pat
Posts: 28
Joined: 2009-09-04 13:11:25

Re: Aplying bold, italic, normal without using menu

Post by Pat » 2009-12-08 17:58:05

Thank you Phillip.

I'm using a reading method for children that uses colors. I also use this color system with other texts and that is why I have made this macro (which saves me a lot of time even though it is far from perfect).

Pat

Groucho
Posts: 496
Joined: 2007-03-03 09:55:06
Location: Europe

Re: Aplying bold, italic, normal without using menu

Post by Groucho » 2009-12-09 07:28:10

I think macros should refer to menu commands as they are in English, no matter what localization you are using. Nisus might then provide a table with English commands and their localized names.

Greetings, Henry.

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

Re: Aplying bold, italic, normal without using menu

Post by phspaelti » 2009-12-09 07:39:45

Hi again.
Here is an example of what I meant by trying to write the macro to Nisus' strengths. Instead of searching for each letter one at a time and then doing something, this macro looks at the letters one by one and then applies what is needed.

This macro works by first defining a hash, which associates each letter with a command. It then checks each letter and then carries out the command associated with the letter. I am not sure this is really a better way to do this, but it is another way.

Philip

Code: Select all

# Macro color letters
#
# The following lines set up a hash which defines a command
# to be performed for each letter.
# Further letters can be added as needed.
# Localize is used to allow the macro to work under different system language.
#
$commands = Hash.new
$commands{‘a’} = Localize “Text Color:Red”
$commands{‘e’} = Localize “Text Color:Blue”
$commands{‘i’} = Localize “Text Color:Orange”
$commands{‘o’} = Localize “Text Color:Dark Green”
$commands{‘u’} = Localize “Text Color:Cyan”
#
# The main macro goes through the document text letter by letter.
# If there is a command associated with that letter, it is applied.
#
$doc = Document.active
$text = $doc.text
$len = $text.length
$i = 1
while $i < $len
	$sel = TextSelection.newWithLocationAndLength $text, $i, 1
	$doc.setSelection $sel
	if $commands{$sel.subtext}
		Menu $commands{$sel.subtext}
	end
	$i += 1
end
philip

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

Re: Aplying bold, italic, normal without using menu

Post by phspaelti » 2009-12-09 08:11:11

Groucho wrote:I think macros should refer to menu commands as they are in English, no matter what localization you are using. Nisus might then provide a table with English commands and their localized names.
Do you means "…should be able to refer to …?"
The ability to refer to menu commands in the local language is just a given. I believe that the Nisus macro feature just passes on the menu command to the user interface. So as long as that command matches something on a menu somewhere then the command is carried out. That is why you can use a user defined macro/style/etc. as a macro command.

As far as being able to use English instead of the localized language, that is possible thanks to the "Localize" command.

What might be a nice idea however might be a sort of global "Localize". Imagine writing "Localize Start/End" before and after a block of menu commands (or at the start of a whole macro) and have it apply to all menu commands in the block (or macro).
philip

User avatar
martin
Official Nisus Person
Posts: 4289
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Aplying bold, italic, normal without using menu

Post by martin » 2009-12-09 12:24:04

phspaelti wrote:I believe that the Nisus macro feature just passes on the menu command to the user interface. So as long as that command matches something on a menu somewhere then the command is carried out. That is why you can use a user defined macro/style/etc. as a macro command.
That's exactly correct.
As far as being able to use English instead of the localized language, that is possible thanks to the "Localize" command.
Yes and no. The Localize command is very limited in what it can translated. It only translates phrases in the “localizable.strings” file found in the application bundle, which does not hold all menu command names. So while "Bold" is known, something like "Double" which one might want to use for the command Format > Underline > Double is not.

Also, some of the translatable phrases take an unexpected form, eg: instead of finding "Red" one must use "_Red". This last peculiarity should be removed, I'll file it as an issue for fixing, but it's something to be aware of.

Code: Select all

	if $commands{$sel.subtext}
		Menu $commands{$sel.subtext}
	end
One small tip on making this more efficient: since you don't need the formatting, this will be faster:

Code: Select all

	if $commands{$sel.substring}
		Menu $commands{$sel.substring}
	end

Pat
Posts: 28
Joined: 2009-09-04 13:11:25

Re: Aplying bold, italic, normal without using menu

Post by Pat » 2009-12-09 22:43:57

It's late and I'm tired but my macro is finally working properly! I would like to thank you all for yours suggestions.

Pat

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

Re: Aplying bold, italic, normal without using menu

Post by phspaelti » 2009-12-10 00:01:09

martin wrote:Also, some of the translatable phrases take an unexpected form, eg: instead of finding "Red" one must use "_Red". This last peculiarity should be removed, I'll file it as an issue for fixing, but it's something to be aware of.
Indeed, I just had a look a this file. Is there some kind of logic behind why some terms have prefixed "_" ?
martin wrote:Yes and no. The Localize command is very limited in what it can translated. It only translates phrases in the “localizable.strings” file found in the application bundle, which does not hold all menu command names. So while "Bold" is known, something like "Double" which one might want to use for the command Format > Underline > Double is not.
So how does that work? Is the term "Double" not localized? I see that the file contains "_Double Underline" even though that does not appear on the menu. I guess my question is what this file really represents.
philip

User avatar
martin
Official Nisus Person
Posts: 4289
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Aplying bold, italic, normal without using menu

Post by martin » 2009-12-10 15:09:43

The "Localizable.strings" file contains phrases that NWP will display to the user. These might be entire sentences, like questions found in prompts, or small phrases used the undo menu or other GUI elements. The words "Double Underline" are used in the stylesheet view, as an attribute bubble. That underscore prefix is just to help us during development; if something appears in the GUI with an underscore, we know it isn't being translated properly.

Post Reply