AppleScript运行无报错但未生成保存的docx文件,求助解决方案
AppleScript脚本无报错但未生成文件的问题排查与解决
我之前也碰到过类似的诡异情况——脚本跑起来没报错,就是死活看不到生成的文件。你的问题核心出在文件路径的格式处理上,先看下你的原代码:
tell application "Finder" set fl to files of folder POSIX file "/Users/abc/folder" as alias list end tell repeat with f in fl tell application "Microsoft Word" activate open f set theActiveDoc to the active document end tell delay 1 tell application "System Events" keystroke "a" using command down keystroke "c" using command down end tell tell application "Finder" set filename to name of f set the_path to POSIX file "/Users/abc/folder2/" & filename end tell tell application "Microsoft Word" close theActiveDoc saving no set new_document to make new document paste special (text object of selection) data type paste text save as new_document file name the_path close active document end tell end repeat
问题根源
你在拼接目标路径the_path时,直接把POSIX file返回的文件引用对象和字符串文件名拼在了一起,这种混合类型的对象无法被Microsoft Word正确解析成有效的保存路径——Word根本不知道该把文件存到哪,自然不会生成文件,又因为没有触发路径错误的抛出,所以全程无报错。
修复方案
把POSIX路径先转换成纯文本格式,再和文件名拼接,就能得到Word能识别的标准路径:
set the_path to (POSIX file "/Users/abc/folder2/" as text) & filename
修改后对应的代码段应该是这样:
tell application "Finder" set filename to name of f set the_path to (POSIX file "/Users/abc/folder2/" as text) & filename end tell
这样处理后,the_path会变成标准的Mac格式文件路径字符串,Word就能正确识别并把文件保存到指定的folder2目录里了。
内容的提问来源于stack exchange,提问作者user2012677
相关产品推荐
相关产品推荐

