2012-04-22 49 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來設置它。

希望這對於其他問題有點更全面的解決方案,但看起來您已經對這個孤立問題有了答案。

相關問題