2017-08-16 69 views
1

這是我的第一個applescript。我使用automator啓動腳本作爲 我將新文件放到一個文件夾(文件夾操作)。使用TextEdit打開文件並將它們導出爲pdf

在Automator中我有2個作用: 1獲得指定的Finder項 2-蘋果腳本

但我不能讓它運行。腳本在打開文件後沒有警告地停止。

下面是腳本:

on run {input, parameters} 

tell application "TextEdit" to activate 

repeat with theFile in input 

    set theFilePath to theFile as alias 

    tell application "TextEdit" to open theFilePath 

    tell application "System Events" 
     tell process "TextEdit" 
      set foremost to true 
      click menu item "Export as PDF..." of menu "File" of menu bar 1 
      click button "Save" 
      click menu item "Close" of menu "File" of menu bar 1 
     end tell 
    end tell 

end repeat 

return input 
end run 

誰能幫助我在這?

我只想使用TextEdit將指定文件夾中的所有文件導出爲pdf。

感謝

回答

0
on adding folder items to this_folder after receiving these_items 
    repeat with i from 1 to number of items in these_items 
     set this_item to item i of these_items 
     tell application "TextEdit" 
      activate 
      open this_item 
      delay 1 
      tell application "System Events" 
       click menu item "Export as PDF…" of menu 1 of menu bar item "File" of menu bar 1 of application process "TextEdit" 
       delay 1 
       key code 36 
       delay 1 
       key code 13 using command down 
      end tell 
     end tell 
    end repeat 
end adding folder items to 

此備用版本將文件轉換成.PDF後退出文本編輯應用程序。請小心使用此版本,因爲如果TextEdit已在運行,我將其設置爲退出而不保存任何打開的文檔。

on adding folder items to this_folder after receiving these_items 
    repeat with i from 1 to number of items in these_items 
     set this_item to item i of these_items 
     tell application "TextEdit" 
      activate 
      open this_item 
      delay 1 
      tell application "System Events" 
       click menu item "Export as PDF…" of menu 1 of menu bar item "File" of menu bar 1 of application process "TextEdit" 
       delay 1 
       key code 36 
       delay 1 
       key code 13 using command down 
      end tell 
     end tell 
    end repeat 
    ignoring application responses 
     tell application "TextEdit" to quit without saving 
    end ignoring 
end adding folder items to 

如果您保存此以下的AppleScript在你的/用戶腳本編輯器爲「Convert_To_PDF.scpt」 /插入您的用戶名/庫/工作流程/應用程序/文件夾操作的文件夾。根本不需要使用Automator。你只需要控制+單擊您要爲您的「熱文件夾」的任何文件夾,你會看到:

enter image description here

您選擇「文件夾操作設置」後,你會看到:

enter image description here

因爲您將AppleScript保存在/ Library/Workflows/Applications/Folder Actions文件夾中,所以當您單擊+號爲文件夾操作添加腳本到文件夾時,它將自動出現在腳本可供選擇

enter image description here

enter image description here

您可能需要在代碼的AppleScript來調整延遲設置一點點。無論哪種方式,這在最新版本的Sierra中都適用於我。

+0

「key code 36」和「key code 13 using command down」代表什麼? – t4ncr3d3

+0

「按鍵代碼36」是按下返回鍵,並且「使用命令按下的按鍵代碼13」是按下命令並且「w」是用於關閉窗口或文檔的快捷方式 – wch1zpink

相關問題