将 Grep Applescript 自动化到 Word 文档
Automate a Grep Applescript to Word Document
我正在使用 Mac 并且我正在为一家公司准备账户。我用 Microsoft Word 制作的每张工资单都有一个凭单编号。因为错过了一笔交易,所以所有凭证号码都是错误的,所以现在有数百张错误的工资单。我想创建一个可以找到以下 GREP 的脚本(找到段落的开头,text:Vch,直到 \r 的任何字符):
^Vch.+\r
然后什么都不替换(从而删除整个句子)。
我正在考虑使用 Applescript,因为它可以打开文档、执行 GREP 查找(棘手的部分)、保存文档并将其另存为 pdf(所有这些都是必需的)。
但显然我的知识让我失望了。字典中的命令,如创建范围,执行查找,都会带来错误。
有没有 Applescript 经验丰富的人可以帮我设计一个脚本?有什么建议么?它应该是这样的:
Tell application "Microsoft Word"
tell active document
set myRange to create range start 0 end 0
tell myRange
execute find find "^Vch.+\r" replace with ""
end tell
end tell
end tell
非常感谢!
没有表示行首的特殊字符。
要在段落开头搜索,脚本必须使用 return & "some text"
您可以使用“^p”作为段落标记,但是当您将match wildcards
属性设置为true
要匹配整个段落,脚本必须使用 return & "some text" & return
,脚本必须使用 replace with return
删除一个段落标记而不是两个。
因为第一段不以段落标记开头,脚本必须使用两个execute find
命令。
通配符是*
tell application "Microsoft Word" -- (tested on version 15.25, Microsoft Office 2016)
-- check the first paragraph
select (characters of paragraph 1 of active document)
execute find (find object of selection) find text ("Vch*" & return) replace with "" replace replace one wrap find find stop with match wildcards and match case without match forward and find format
--to search forward toward the end of the document.
execute find (find object of selection) find text (return & "Vch*" & return) replace with return replace replace all wrap find find continue with match wildcards, match case and match forward without find format
save active document
-- export to PDF in the same directory as the active document
set pdfPath to path of active document & ":" & (get name of active window) & ".pdf"
set pdfPath to my createFile(pdfPath) -- create an empty file if not exists, the handler return a path of type alias (to avoid grant access issue)
save as active document file name pdfPath file format format PDF
end tell
on createFile(f)
do shell script "touch " & quoted form of POSIX path of f
return f as alias
end createFile
我正在使用 Mac 并且我正在为一家公司准备账户。我用 Microsoft Word 制作的每张工资单都有一个凭单编号。因为错过了一笔交易,所以所有凭证号码都是错误的,所以现在有数百张错误的工资单。我想创建一个可以找到以下 GREP 的脚本(找到段落的开头,text:Vch,直到 \r 的任何字符):
^Vch.+\r
然后什么都不替换(从而删除整个句子)。
我正在考虑使用 Applescript,因为它可以打开文档、执行 GREP 查找(棘手的部分)、保存文档并将其另存为 pdf(所有这些都是必需的)。
但显然我的知识让我失望了。字典中的命令,如创建范围,执行查找,都会带来错误。
有没有 Applescript 经验丰富的人可以帮我设计一个脚本?有什么建议么?它应该是这样的:
Tell application "Microsoft Word"
tell active document
set myRange to create range start 0 end 0
tell myRange
execute find find "^Vch.+\r" replace with ""
end tell
end tell
end tell
非常感谢!
没有表示行首的特殊字符。
要在段落开头搜索,脚本必须使用 return & "some text"
您可以使用“^p”作为段落标记,但是当您将match wildcards
属性设置为true
要匹配整个段落,脚本必须使用 return & "some text" & return
,脚本必须使用 replace with return
删除一个段落标记而不是两个。
因为第一段不以段落标记开头,脚本必须使用两个execute find
命令。
通配符是*
tell application "Microsoft Word" -- (tested on version 15.25, Microsoft Office 2016)
-- check the first paragraph
select (characters of paragraph 1 of active document)
execute find (find object of selection) find text ("Vch*" & return) replace with "" replace replace one wrap find find stop with match wildcards and match case without match forward and find format
--to search forward toward the end of the document.
execute find (find object of selection) find text (return & "Vch*" & return) replace with return replace replace all wrap find find continue with match wildcards, match case and match forward without find format
save active document
-- export to PDF in the same directory as the active document
set pdfPath to path of active document & ":" & (get name of active window) & ".pdf"
set pdfPath to my createFile(pdfPath) -- create an empty file if not exists, the handler return a path of type alias (to avoid grant access issue)
save as active document file name pdfPath file format format PDF
end tell
on createFile(f)
do shell script "touch " & quoted form of POSIX path of f
return f as alias
end createFile