2012-04-22 30 views
1

我需要定期从word文件生成PDF文件,而且我已经厌倦了用手做文件。让我的苹果脚本在当前目录中处理指定的文件?

手动操作,我所做的只是打开一个文件,然后单击“另存为PDF”。所以,有人会认为applescript会是一个很好的方法来做到这一点。 [如果你有另一种方法比applescript,我打开它]

我快到了,下面的脚本工作,除了完整的路径是硬编码。

tell application "Microsoft Word" 
    open file "Macintosh HD:Users:me:repos:training:class:Activities:ActivityGuide.docx" 
    set doc to document "ActivityGuide.docx" 
    save as doc file name "Macintosh HD:Users:me:repos:training:class:Activities:ActivityGuide.pdf" file format format PDF 
end tell 

我需要它在其他机器上为其他人工作,所以路径的用户名和其他部分可能会改变。如果我可以从脚本的当前目录中执行此操作,我将被设置。

我发现这一点:Applescript to launch file from current folder?

tell application "Finder" 
    open file "somefile.txt" of folder of (file (path to me)) 
end tell 

这对于从Word中的当前目录中打开一个应用程序,通过查找工作,但我想我需要用“的Microsoft Word”的应用程序中打开它,如果我将使用“另存为”。但是,如果将“Finder”的应用程序更改为“Microsoft Word”,则此方法无法使用。

欢迎任何建议。 [编辑:清晰度]

回答

1

试试这个

set x to path to me 
tell application "Finder" to set tFile to (file "ActivityGuide.docx" of (container of x)) as alias 

set newFile to (text 1 thru -5 of (tFile as string)) & "pdf" 
tell application "Microsoft Word" 
    open tFile 
    tell document 1 
     save as file name newFile file format format PDF 
     close saving no 
    end tell 
end tell 
+0

精美的作品。谢谢您的帮助! – ThoughtfulHacking 2012-04-22 23:17:13

0

另一件事,可能会对你有所帮助下系统预置:键盘:快捷键:应用程序快捷方式:所有应用

建立一个快捷方式是命名为“Save as PDF ...”将快捷方式分配给命令+ P,因为大多数应用程序是打印的快捷方式,然后只需按两次,它就会提示您将其另存为PDF(我从David Sparks那里获得了该提示),根据应用程序的不同,它会默认文件从哪里打开。您也可以使用应用程序的默认文件夹X来设置它。

希望这对于其他问题有点更全面的解决方案,但看起来您已经对这个孤立问题有了答案。

相关问题