Applescript 使用 TextEdit 打开文件并将它们导出为 pdf [英] Applescript open file with TextEdit and export them as pdf
问题描述
这是我的第一个applescript.我使用 automator 来启动脚本我将新文件放入文件夹(文件夹操作).
This is my very first applescript. I use automator to launch the script as I drop new files to a folder (Folder action).
在 Automator 中,我有 2 个操作:1- 获取指定的 Finder 项目2- 苹果脚本
In Automator I have 2 action: 1- Get Specified Finder Items 2- The Apple Script
但我无法让它运行.打开文件后,脚本会在没有警告的情况下停止.
But I can't make it running. The script stops without warning after opening the files.
这是脚本:
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.
I just want to export all the files in the specified folders to pdf using 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
end adding folder items to
此替代版本将在将文件转换为 .PDF 后退出 TextEdit 应用程序.使用这个版本时要小心,因为如果 TextEdit 已经在运行,我将它设置为退出而不保存任何打开的文档.
This alternate version will quit TextEdit application after converting the files to .PDF. Be careful using this version because I set it to quit without saving any open documents if TextEdit was already running.
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.您只需要控制并单击您想要作为热文件夹"的任何文件夹,您就会看到:
If you save this following AppleScript in script editor as "Convert_To_PDF.scpt" in your /Users/INSERT YOUR USERNAME/Library/Workflows/Applications/Folder Actions folder. You don't need to use Automator at all. You simply need to control + click any folder you want as your "Hot Folder" and you will see this:
选择文件夹操作设置"后,您将看到:
After you select "Folder Actions Setup", you will see this:
因为您将该 AppleScript 保存在/Library/Workflows/Applications/Folder Actions 文件夹中,所以当您单击 + 号将脚本添加到您的文件夹以进行文件夹操作时,它将自动出现在脚本列表中从中选择
Because you saved that AppleScript in your /Library/Workflows/Applications/Folder Actions folder, when you click the + sign to add a script to your folder for the folder action, it will automatically be available in the list of scripts to choose from
您可能需要稍微调整 AppleScript 代码中的延迟设置.无论哪种方式,这对我来说都适用于最新版本的 Sierra.
You may need to adjust the delay settings in the AppleScript code a little bit. Either way, this functions for me in the latest version of Sierra.
这篇关于Applescript 使用 TextEdit 打开文件并将它们导出为 pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!