Reply to topic  [ 9 posts ] 
Extracting and using style name 
Author Message

Joined: 2009-04-28 09:09:13
Posts: 15
Location: Minneapolis, MN
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


2010-04-13 07:27:21
Profile

Joined: 2008-05-17 04:02:32
Posts: 400
If I understand you well, this macro will do the job.
Code:
$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:
$sels = $doc.text.findAll '^', 'E', '-am'
with
Code:
$sels = $doc.text.findAll '^(?![\n\f])', 'E', '-am'


2010-04-13 08:12:10
Profile

Joined: 2009-04-28 09:09:13
Posts: 15
Location: Minneapolis, MN
Kino,
It works perfectly! I can't thank you enough.

Regards,

Duff


2010-04-14 08:01:29
Profile

Joined: 2009-04-28 09:09:13
Posts: 15
Location: Minneapolis, MN
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


2010-04-16 09:07:01
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
Here's an adjusted version of Kino's macro that ensures the InDesign tags do not have any character case changes applied:
Code:
$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


2010-04-16 10:21:39
Profile WWW

Joined: 2009-04-28 09:09:13
Posts: 15
Location: Minneapolis, MN
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


2010-04-16 10:46:28
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
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.


2010-04-16 11:19:48
Profile WWW

Joined: 2009-04-28 09:09:13
Posts: 15
Location: Minneapolis, MN
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


2010-04-16 11:31:20
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
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:
$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:
$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.


2010-04-16 13:57:56
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software