Extracting and using style name

Get help using and writing Nisus Writer Pro macros.
Post Reply
macduffee
Posts: 15
Joined: 2009-04-28 09:09:13
Location: Minneapolis, MN

Extracting and using style name

Post by macduffee » 2010-04-13 07:27:21

Once again, I am working on a file that will be used in InDesign. The manuscript is set in Pages, using styles throughout. I export out of Pages into RTF and then into Nisus. The macro that I would like to have help creating would look at each paragraph, recognize the applied style, extract that style name and insert it before the paragraph in the InDesign tagged text format.

Example:
The first paragraph style name is "Heading 1".
The second paragraph style name is "Body".

Individual Study
This participant’s guide also works well for personal or individual study. Simply move at your own pace through the lessons and use the discussion questions as prompters for note taking, journaling, and personal reflection.

Would become:

<pstyle:Heading 1>Individual Study
<pstyle:Body>This participant’s guide also works well for personal or individual study. Simply move at your own pace through the lessons and use the discussion questions as prompters for note taking, journaling, and personal reflection.

http://editorium.com/ has a program that does this in Word, but I'm a Nisus man.

Thanks!

Duff

Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Extracting and using style name

Post by Kino » 2010-04-13 08:12:10

If I understand you well, this macro will do the job.

Code: Select all

$prefix = Cast to String '<pstyle:'
$suffix = Cast to String '>'

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

$sels = $doc.text.findAll '^', 'E', '-am'
foreach $sel in reversed $sels
	$attr = $sel.text.displayAttributesAtIndex $sel.location
	$tag = $prefix & $attr.paragraphStyleName
	$tag &= $suffix
	$sel.text.insertAtIndex $sel.location, $tag
end
It inserts tags in empty paragraphs too. If you want it to skip them, replace

Code: Select all

$sels = $doc.text.findAll '^', 'E', '-am'
with

Code: Select all

$sels = $doc.text.findAll '^(?![\n\f])', 'E', '-am'

macduffee
Posts: 15
Joined: 2009-04-28 09:09:13
Location: Minneapolis, MN

Re: Extracting and using style name

Post by macduffee » 2010-04-14 08:01:29

Kino,
It works perfectly! I can't thank you enough.

Regards,

Duff

macduffee
Posts: 15
Joined: 2009-04-28 09:09:13
Location: Minneapolis, MN

Re: Extracting and using style name

Post by macduffee » 2010-04-16 09:07:01

Kino,
I did find one problem. When the InDesign Tagged Text code and the Style name are inserted, they take on the style of that paragraph. This causes a problem when the style is all caps or small caps as InDesign needs to see the code in lowercase. What code would I use to make the inserted text no style or forced to lowercase?

Thanks again for your help,

Duff

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

Re: Extracting and using style name

Post by martin » 2010-04-16 10:21:39

Here's an adjusted version of Kino's macro that ensures the InDesign tags do not have any character case changes applied:

Code: Select all

$prefix = Cast to String '<pstyle:'
$suffix = Cast to String '>'

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

$sels = $doc.text.findAll '^', 'E', '-am'
foreach $sel in reversed $sels
	$attr = $sel.text.displayAttributesAtIndex $sel.location
	$tag = $prefix & $attr.paragraphStyleName
	$tag &= $suffix
	$sel.text.insertAtIndex $sel.location, $tag
	$sel.length = $tag.length
	
	# force tag to not use any case change
	$doc.setSelection($sel)
	Menu 'Character Case:No Case Change'
end

macduffee
Posts: 15
Joined: 2009-04-28 09:09:13
Location: Minneapolis, MN

Re: Extracting and using style name

Post by macduffee » 2010-04-16 10:46:28

Martin,
I should think these things out before I post. The new macro works, but now I find that I will need to ensure that the InDesign code doesn't take on any of the formatting of the paragraph (bold, italic, etc). My second macro searches for these formats and inserts a code for the character styles. If this is a pain, I can run the character style macro first but I have a reason for not wanting to do that which has escaped me at this moment.

Duff

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

Re: Extracting and using style name

Post by martin » 2010-04-16 11:19:48

Ahh okay, well here's one question: do you really need the InDesign tags to be devoid of all formatting (eg: bold, italics, color, etc) or just proper named Character Styles (eg: Emphatic, Default Font, etc)? The latter is much easier to accomplish.

macduffee
Posts: 15
Joined: 2009-04-28 09:09:13
Location: Minneapolis, MN

Re: Extracting and using style name

Post by macduffee » 2010-04-16 11:31:20

Martin,
I need both. A paragraph style to set the indents, space before and after, drop cap, first line, font, leading etc. Within the paragraph we then use character styles to modify, bold, italic, small caps, superscript etc. If we have the style name come in with the same format as the text then the codes can be swapped around and wont work.

An example,
Original text:
To Vanderbilt Medical Center, thank you for your concern, loving care, and patience with the hundreds

Should be coded like this,

<pstyle:Body A><cstyle:b>To Vanderbilt Medical Center<cstyle:>, thank you for your concern, loving care, and patience with the hundreds

If the macro that installs the pstyle code formats as bold, then the line looks like this:

<cstyle:b><pstyle:Body A>To Vanderbilt Medical Center<cstyle:>, thank you for your concern, loving care, and patience with the hundreds

with the cstyle tag before the pstyle tag.


Duff

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

Re: Extracting and using style name

Post by martin » 2010-04-16 13:57:56

So what you want is all InDesign tags at the start of each paragraph formatted as "plain" text, without any attributes. The trouble is that one cannot apply Normal or some other default style to just the tags, as that would supplant the style from the rest of your paragraph as well. The only way you can achieve your goal is to override the paragraph style's formatting, one attribute at a time. Here's a macro that does such a thing:

Code: Select all

$prefix = Cast to String '<pstyle:'
$suffix = Cast to String '>'

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

# list of menus that we need to ensure are on/off
$menusStates = Hash.new
$menusStates{'Bold'} = 0
$menusStates{'Italic'} = 0
$menusStates{'No Super/Sub'} = 1
$menusStates{'No Case Change'} = 1

# go through all paragraphs in the document
$sels = $doc.text.findAll '^', 'E', '-am'
foreach $sel in reversed $sels
	$attr = $sel.text.displayAttributesAtIndex $sel.location
	$tag = $prefix & $attr.paragraphStyleName
	$tag &= $suffix
	$sel.text.insertAtIndex $sel.location, $tag

	# select inserted tag for processing
	$sel.length = $tag.length
	$doc.setSelection($sel)

	# ensure certain formatting properties are on/off
	Menu 'Character Style:Remove Style'
	ForEach $menuName, $desiredState in $menusStates
		$state = Menu State $menuName
		If $state != $desiredState
			Menu $menuName	# toggle state on/off
		End
	End
end
The trouble with this approach is one must list all menus that need to be at some "plain" state. In the macro above I've covered the attributes you mentioned (eg: bold, italic, superscript, and case change), but likely you have others. For every attribute your other macros care about, you'll need to add a relevant entry to the $menuStates hash.

As an example, let's say you also need to ensure the text coloring is black. You'd want to add this line to the macro:

Code: Select all

$menusStates{'Text Color:Black'} = 1
You use a 1 to ensure the menu is checked (attribute is applied), and a 0 to ensure the attribute/menu is off/unchecked.

Hopefully that makes sense. Please let me know if you have any questions.

Post Reply