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

Applescript open file with TextEdit and export them as pdf

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

谢谢

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 YOUR USERNAME/Library/Workflows/Applications/Folder Actions 文件夹中的“Convert_To_PDF.scpt”。您根本不需要使用 Automator。您只需按住 Control 键并单击您想要作为“常用文件夹”的任何文件夹,您就会看到:

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

因为您将该 AppleScript 保存在 /Library/Workflows/Applications/Folder Actions 文件夹中,当您单击 + 号将脚本添加到您的文件夹以进行文件夹操作时,它将自动出现在可供选择的脚本列表中来自

您可能需要稍微调整 AppleScript 代码中的延迟设置。不管怎样,这在最新版本的 Sierra 中对我有用。