Pages

Wednesday, August 3, 2011

A followup on my workflow post

This is a followup to my last post on working with Scrivener, TextExpander and Word in response to a couple of comments on that post.

The first question was:
        I have wondered about styles and Scriv, more for bringing into InDesign. I would happily go through Word (OK, happily's a bit strong!). How does your script work? Could I please grab a copy?

My Word script works by applying Word styles in places that it finds the tags that I placed in Scrivener using TextExpander.

Here is the main, executable, function in the script:

Sub styleDocument()
    ' Change the whole doc to para style
    ActiveDocument.Select
    Selection.Style = "para"
    
    ' Replace the auto generated HTML style headers with styles
    replace ("ChapterTitle")
    replace ("H1")
    replace ("H2")
    replace ("H3")
    styleTaggedText ("c80")
    styleTaggedText ("ic")
    styleTaggedText ("slug")
End Sub

First, it selects the entire document and changes the style of all text in the document to the "para" style. The para style is defined by my publisher in their Word template. Next, I call two of my other functions which you'll see in a second: replace and styleTaggedText.

When I compile a chapter from Scrivener, I use the Formatting compilation options to add certain tags at different levels in the outline. For instance, I add the tag <ChapterTitle> at the top level, <H1> and the second level, <H2> at the third level, etc. When I compile my chapter, Scrivener automatically adds these tags to the RTF file.

When I run my script, the replace function gets called, for each of these levels as you can see above. Here is the replace function:

Private Sub replace(textReplace)
    With ActiveDocument.Content.Find
        .Forward = True
        .Text = "<" + textReplace + ">"
        While (.Execute)
            .Parent.Style = textReplace
            .Parent.Text = ""
        Wend
    End With
End Sub

This function searches for the tag and styles the text with the same style name. Again, the styles ChapterTitle, H1, H2 and H3 are pre-defined in my publisher's Word template.

The other part of the script calls my styleTaggedText function:

Private Sub styleTaggedText(tag As String)

    Dim styleRange As Range
    Set styleRange = ActiveDocument.Content
    Dim tempRange As Range
    
    ' Keep looping until there are no more c80 tags
    While (ActiveDocument.Content.Find.Execute("<" + tag + ">"))
    
        ' Set the style range start and end
        Set tempRange = ActiveDocument.Content
        With tempRange.Find
            .Forward = True
            .Text = "<" + tag + ">"
            .Execute
            .Parent.Text = ""
             styleRange.Start = tempRange.End
        End With
        
        Set tempRange = ActiveDocument.Content
        With tempRange.Find
            .Forward = True
            .Text = "</" + tag + ">"
            .Execute
            .Parent.Text = ""
            styleRange.End = tempRange.Start
        End With
        
        ' Style the range
        If tag = "c80" Then
            styleRange.Style = "Normal"
        ElseIf tag = "ic" Then
                styleRange.Style = "InlineCode"
        ElseIf tag = "slug" Then
                styleRange.Style = "slug"
        End If
    Wend

End Sub

This function styles the text between my open and close tags with the given style name. Since I do technical writing, I have to apply styles inline. For example, code needs to be styled InlineCode if it is interspersed with text in a sentence. So, when I am writing in Scrivener, I write something like this:
Then, click on the <ic>-viewDidUnload</ic> method under the FirstProgramViewController.

When I run the styleTaggedText function in my Word script and pass in "ic", any text between the open (<ic>) and close (</ic>) tags gets styled with the InlineCode style.

Hopefully this gives a bit more insight into how the Word script works.

As I am writing, I am constantly using my tags. This is where TextExpander comes in. Instead of typing the opening and closing tags every time, I have shortcuts in TextExpander to write the tags for me. Additionally, in the TextExpander snippets, I can place the cursor anywhere that I want. So, to type that example above, I would type:
Then, click on the xic -viewDidUnload method under the FirstProgramViewController.
The xic snippet inserts <ic></ic> into Scrivener and places the cursor between the tags. Then, I just type the code that I want and use CMD-Left Arrow to go to the end of the sentence, past the closing tag.

The other question was:
        Hi. I'm wondering if your workflow would work equally well with an app similar to TextExpander but cheaper. For example, Snippets, yType, and TypeIt4Me are all available on the Mac App Store for much less money but appear to do roughly the same thing. Any suggestions? Is there something that makes TextExpander worth the extra money?

I believe that this workflow would work fine with any app similar to TextExpander. Probably even Apple’s own text replacement functionality built into OS X. The one thing that I’m not sure about is the ability to put the cursor anywhere you want after your shortcut is expanded. My xic snippet is coded as <ic>%|</ic> in TextExpander. The %| is used to indicate the position of the cursor after the expansion. So, when I type xic, TextExpander expands that to the <ic></ic> tags and puts the cursor between the two, ready for me to type my styled code. That feature is very valuable to me as it saves me a fair amount of time.

Hopefully that answers your questions. If not, let me know in the comments and I’ll try to do a better job of explaining!