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:
After AppleScript hard returns will be added:
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.
This is one way you might do it. I haven't tested this thoroughly, you may need to adjust it further. Highlight the text in question and trigger this macro.
@cfriend Thank you for your solution. I am afraid that this may not work correctly on longer documents with tables and images. I would like to avoid any copying and pasting
I am more concerned about these attributes (note that they are different from doc to doc):
Yes, Word’s AppleScript dictionary definitely knows the properties space before and space after. So, it should be doable with AppleScript. Unfortunately ATM I don’t the time to write a script.
— [deleted non-working script; new script see below] —
‌
Every paragraph that had a space before gets a Return at the beginning, and every paragraph that had a space after gets a Return at the end. The spaces before/after are set to 0.
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
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
set space before of thePar to 0
insert text "†" at before text object of thePar
end if
if space after of thePar is greater than 0 then
set space after of thePar to 0
insert text "†" at after text object of thePar
end if
end repeat
execute find find object of text object find text "†{1,}" replace with "^p" replace replace all match wildcards yes
end tell
end tell
It is considerably faster. (20s instead of >2min.)
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.
‌ However, your new sample text poses a new challenge:
Since the additionally inserted empty paragraphs take the style of the “parent” paragraph , we end up with empty paragraphs that are styled as list, that is, an empty line with a leading bullet.
If this is a problem (and I fear it is) I have to come up with a more sophisticated replacement that removes the style…
@Tom my workflow in InDesign involves removing all Paragraph and Character styles from Word Doc (but preserving the local overrides), I then run script to look for Character attributes (such as Bold, Italic etc.) - that script create Character Styles based on the mentioned attributes. Paragraph Style: I use one Body Text.
So at the end I don’t care about WordDocument Paragraph/Character styles as long as the Bold, Italics etc. are preserved (if that makes sense)
For the sake of delivering clean work, here is a variant that sets the style of any newly inserted empty paragraph to the base style “plain text”.
That is, no lonely list bullets in empty paragraphs. Styles of the existing paragraphs are left untouched.
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
set space before of thePar to 0
insert text "†" at before text object of thePar
end if
if space after of thePar is greater than 0 then
set space after of thePar to 0
insert text "†" at after text object of thePar
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: style of the inserted empty paragraphs
set style of replacement of makePars to style plain text
execute find makePars match wildcards yes replace replace all
end tell
end tell
Please give it test run. If it’s fine I will also update my answer to your post on StackOverflow.
It sets every style to the style “plain text”, while preserving local overrides (it seems). Since it doesn’t set the spacing of individual paragraphs to zero, it will be faster than the other variants!
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
end if
if space after of thePar is greater than 0 then
insert text "†" at after text object of thePar
end if
end repeat
set style of text object to style plain text
set makePars to find object of text object
set content of makePars to "†{1,}"
set content of replacement of makePars to "^p"
execute find makePars match wildcards yes replace replace all
end tell
end tell
If you don’t care about the space before/after of the existing paragraphs (since you are removing the Word styles in InDesign) then you can remove these lines from the second last variant:
set space before of thePar to 0
set space after of thePar to 0
This will make it way faster. (~ As fast the last variant).