使用TextEdit打开文件并将它们导出为pdf

使用TextEdit打开文件并将它们导出为pdf

问题描述:

这是我的第一个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。

感谢

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