Microsoft Word - 无法使用苹果脚本导出 PDF 文件



我试图将docx转换为pdf,并通过从原始文件名中获取一些字母来重命名文件。

在Automator中,我使用了"获取指定的查找器项目"one_answers"运行AppleScript"。

我执行并在Microsoft Word 中收到一条弹出消息

"无法导出文件。出现问题,您的文件现在无法导出。请稍后再试。">

有人知道怎么了吗?请帮帮我。谢谢

  • Microsoft Word:16.35(20030802(
  • 自动售货机:2.10(492(
  • macOS:10.15.4
on run {input, parameters}
repeat with aFile in input
tell application "Microsoft Word"
launch
try
open aFile
tell document 1
set fileName to aFile as text
set newName to "p" & (text 14 thru 15 of fileName) & "-" & (text 11 thru 12 of fileName) & "-" & (text 8 thru 9 of fileName)
set pdfOutput to (newName & ".pdf")
save as file name pdfOutput file format format PDF

end tell
close document 1 saving yes
on error
try
close document 1 saving no
end try
end try
end tell
end repeat
return input
end run

如果我已经理解你的意图,问题是这一行:

set fileName to aFile as text

您应该将其更改为

set fileName to the name as text

如果没有进一步的实验,我无法确定,部分原因是这取决于你的路径名到底是什么样子,但在代码中你有

set fileName to aFile as text

这意味着fileName包含文档路径+文件的完整AFS路径名,带有":"分隔符,并且表示等

text 14 thru 15 of fileName

正在从路径中挑选字符,可能包括":"字符,这可能会导致您看到的问题,而我认为您的意图是从文件名中挑选字符。

(此外,我还没有考虑过文件名不包含代码当前引用的8个或更多字符的情况。(

如果按原样运行脚本,PDF将最终保存在像/Users/<your_name>/Library/Containers/com.microsoft.Word/Data/Documents/这样的文件夹中(就像@little-snarky评论的那样(。

试试这个方法:

on run {input, parameters}
repeat with aFile in input
tell application "Finder" to set theFolder to (folder of aFile) as text -- !!!
tell application "Microsoft Word"
launch
try
open aFile
tell document 1
set fileName to aFile as text
set loc to location of aFile
set newName to "p" & (text 14 thru 15 of fileName) & "-" & (text 11 thru 12 of fileName) & "-" & (text 8 thru 9 of fileName)
set pdfOutput to (theFolder & newName & ".pdf") -- !!!
save as file name pdfOutput file format format PDF

end tell
close document 1 saving yes
on error
try
close document 1 saving no
end try
end try
end tell
end repeat
return input
end run

更改在以-- !!!作为注释的行中

最新更新