2016-08-31 63 views
1

我正在使用Mac,并且正在为公司准备帐户。我在Microsoft Word中制作的每个工资单都有一个优惠券编号。由于交易错过了,所有凭证号码都是错误的,所以现在有数百个错误的薪资单。我想创建一个脚本,可以发现下面牛瘟(找到段落的开头,正文:VCH,任何字符,直到\ R):将Grep Applescript自动化为Word文档

^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 

非常感谢!

回答

1

没有特殊字符表示一行的开始。

在该款的开始搜索,脚本必须使用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