Peter
Peter

Reputation: 3

(AppleScript) Add Hard Return around Paragraph spacing Before/After in Microsoft Word

I am "hunting" for AppleScript that allow me to add Hard Return to text where Paragraph Spacing Before/After is used.

For instance I am receiving a Word Document with Space Before (or After) and would like to add Carriage Return (Hard Returns) around text using Spacing After/Before.

Before:

text 1

text 2

text 3

After:

text 1
¶
text 2
¶
text 3
¶

Sometimes Word Document have mixture of Spacing Before and After in the document so would like to include both conditions. Now, since I am receiving Word Documents with different values for Space Before/After - I am unable to give a fixed value.

Is this possible?

Upvotes: 0

Views: 516

Answers (1)

Tom
Tom

Reputation: 41

Try this script:

tell application "Microsoft Word"
  tell active document
    set spacedBeforePars to paragraph_id of every paragraph where space before is greater than 0
    set spacedAfterPars to paragraph_id of every paragraph where space after is greater than 0
    repeat with theID in spacedBeforePars
      set thePar to (every paragraph whose paragraph_id is theID)
      set thePar to item 1 of the thePar
      insert paragraph at before text object of thePar
    end repeat
    repeat with theID in spacedAfterPars
      set thePar to (every paragraph whose paragraph_id is theID)
      set thePar to item 1 of the thePar
      insert paragraph at after text object of thePar
    end repeat
    set space before of every paragraph to 0
    set space after of every paragraph to 0
    execute find find object of text object find text "^13{3,}" replace with "^p^p" replace replace all match wildcards yes
  end tell
end tell

Every paragraph that had a space before gets an empty paragraph before, and every paragraph that had a space after gets an empty paragraph after. The spaces before/after are set to 0pt and multiple empty paragraphs are reduced to one. (Tested with Word Mac 2011 14.7.2.)

Note: It seems you have crossposted your question on the Keyboard Maestro forum. I have posted the same script over there.

Update:

This script does the same but will run considerably faster:

tell application "Microsoft Word"
  tell active document
    set spacedPars to every paragraph whose (space before is greater than 0 or space after is greater than 0)
    repeat with thePar in spacedPars
      if space before of thePar is greater than 0 then
        insert text "†" at before text object of thePar
        # Optional: remove paragraph spacing
        set space before of thePar to 0
      end if
      if space after of thePar is greater than 0 then
        insert text "†" at after text object of thePar
        # Optional: remove paragraph spacing
        set space after of thePar to 0
      end if
    end repeat
    set makePars to find object of text object
    set content of makePars to "†{1,}"
    set content of replacement of makePars to "^p"
    # Optional: Remove any style from the inserted empty paragraphs (e.g. list bullets)
    set style of replacement of makePars to style plain text
    execute find makePars match wildcards yes replace replace all
  end tell
end tell

Replace the temporary placeholder character () with a unique character that is not present in your text. You can also use a string of more than one characters. Don’t forget to adapt the regex.

  • If you delete the two optional set space… lines (see comments in the script) the original vertical paragraph spacing will be left untouched, but the script will be even faster.

  • To get rid of all styles (except most local overrides) delete all the three lines that are commented with “Optional” and add this line after the end repeat:

    set style of text object to style plain text

Upvotes: 1

Related Questions