Applescript使用TextEdit打开文件并将其导出为pdf

时间:2017-08-16 19:19:28

标签: pdf applescript export textedit

这是我的第一个苹果。我使用automator来启动脚本 我将新文件删除到文件夹(文件夹操作)。

在Automator中我有2个动作: 1-获取指定的Finder项目 2- Apple脚本

但我不能让它运行。打开文件后,脚本会在没有警告的情况下停止。

这是脚本:

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。

由于

1 个答案:

答案 0 :(得分: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应用程序。请谨慎使用此版本,因为如果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保存为/ Users / INSERT您的USERNAME / Library / Workflows / Applications / Folder Actions文件夹中的“Convert_To_PDF.scpt”。您根本不需要使用Automator。您只需要控制+单击任何您想要的文件夹作为“热文件夹”,您将看到:

enter image description here

选择“文件夹操作设置”后,您将看到:

enter image description here

因为您在/ Library / Workflows / Applications / Folder Actions文件夹中保存了AppleScript,所以当您单击+号将脚本添加到文件夹中以执行文件夹操作时,它将自动在脚本列表中可用。选择

enter image description here

enter image description here

您可能需要稍微调整AppleScript代码中的延迟设置。无论哪种方式,这都适用于我最新版本的Sierra。