Convert2PDF on MAC

打印投标文档前一般需要转换为PDF:)投标文档含封面之类的有几个到上十个Word文档,一个个打开转换稍显麻烦,弄了一个脚本来做这个事情。

property
theList : {"doc", "docx"}

on run {input, parameters}
set output to {}
tell application "Microsoft Word" to set theOldDefaultPath to get default file path file path type documents path
repeat with x in input
try
set theDoc to contents of x
tell application "Finder"
set theFilePath to container of theDoc as text

set ext to name extension of theDoc
if ext is in theList then
set theName to name of theDoc
copy length of theName to l
copy length of ext to exl

set n to l - exl - 1
copy characters 1 through n of theName as string to theFilename

set theFilename to theFilename & ".pdf"

tell application "Microsoft Word"
set default file path file path type documents path path theFilePath
open theDoc
set theActiveDoc to the active document
save as theActiveDoc file format format PDF file name theFilename
copy (POSIX path of (theFilePath & theFilename as string)) to end of output
close theActiveDoc without saving
end tell
end if
end tell
end try
end repeat
tell application "Microsoft Word" to set default file path file path type documents path path theOldDefaultPath

return output
end run

上面的代码是从网上弄来的,来源在这里:

Convert Word to PDF – Mac OS X

我做了一点点修改,原文中关闭文档的代码是:

close theActiveDoc

修改为:

close theActiveDoc without saving

这样一来就不会跑出一个让你保存代码的对话框转换后也不会保留很多打开的文件了。

文章里没写如何安装为Finder的Service(相当于Windows Explorer的右键菜单),反而是写的用Automator来操作。用Service的方法很简单:
1. 打开Automator,新建一个Service,选择Select Files or Folder in Finder。

2. 新增一个Action,类型选择Run AppleScript,将上面的代码完整的复制进去。(删除里面的默认的框架代码,只保留本文的代码)

3. 保存,取一个名字,比如:Convert2PDF。
4. 在Finder中选择所有待转换的doc/docx文件,右键选择service,菜单里就会有Convert2Pdf的选项了。然后静待一切完工吧

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *