Ms word (AppleScript)在Microsoft Word的前后段落间距周围添加硬回车

Ms word (AppleScript)在Microsoft Word的前后段落间距周围添加硬回车,ms-word,applescript,carriage-return,Ms Word,Applescript,Carriage Return,我正在“搜寻”AppleScript,它允许我在使用段落前后间距的文本中添加硬回车 例如,我收到一个Word文档,它的前面(或后面)有空格,我想在文本周围添加回车符(硬回车符),使用后面/前面的空格 之前: text 1 text 2 text 3 text 1 ¶ text 2 ¶ text 3 ¶ 之后: text 1 text 2 text 3 text 1 ¶ text 2 ¶ text 3 ¶ 有时Word文档在文档中前后混合使用空格,因此希望同时包含这两个条件。现在,

我正在“搜寻”AppleScript,它允许我在使用段落前后间距的文本中添加硬回车

例如,我收到一个Word文档,它的前面(或后面)有空格,我想在文本周围添加回车符(硬回车符),使用后面/前面的空格

之前:

text 1

text 2

text 3
text 1
¶
text 2
¶
text 3
¶
之后:

text 1

text 2

text 3
text 1
¶
text 2
¶
text 3
¶
有时Word文档在文档中前后混合使用空格,因此希望同时包含这两个条件。现在,由于我收到的Word文档的前后空格值不同,我无法给出一个固定值

这可能吗?

试试这个脚本:

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
前面有空格的段落前面会有一个空段落,后面有空格的段落后面会有一个空段落。前后空格设置为0pt,多个空段落减少为一个。(使用Word Mac 2011 14.7.2进行测试。)

注意:您似乎已将您的问题交叉张贴在页面上。我在那边贴了同样的剧本

更新: 此脚本执行相同的操作,但运行速度将大大加快:

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
将临时占位符字符(
)替换为文本中不存在的唯一字符。您还可以使用包含多个字符的字符串。别忘了调整正则表达式

  • 如果删除两行可选的
    set space…
    行(请参见脚本中的注释),原始垂直段落间距将保持不变,但脚本速度会更快

  • 要删除所有样式(大多数本地替代除外),请删除所有以“可选”注释的三行,并在
    结束重复后添加这一行:

    将文本对象的样式设置为纯文本样式