AppleScript无法识别符合预期格式的文件名问题求助
AppleScript文件名匹配错误排查与修复
我正在开发一款AppleScript,用于处理格式为firstname lastname OL.pdf的PDF文件:将文件复制到新建的firstname lastname文件夹,并删除原文件。但运行脚本时,符合该格式的文件却触发“Filename 'firstname lastname OL.pdf' does not match expected format”错误。以下是使用的代码:
use AppleScript version "2.4" -- Yosemite (10.10) or later use scripting additions property cloud_folder : "/Users/will/Documents" (* There are three ways of invoking this script: - as a folder action (automated) - by running the script directly (as an app, or within Script Editor - by saving it as an app and dropping a file on it *) on adding folder items to theFolder after receiving theNewItems -- Called after items have been added to a folder -- -- theFolder is a reference to the modified folder -- theNewItems is a list of references to the items added to the folder repeat with each_file in theNewItems my handle_file(each_file) end repeat end adding folder items to on run -- called if the script is run directly -- prompt the user for a file to process handle_file(choose file) end run on open of theFiles -- Executed when files are dropped on the script repeat with aFile in theFiles handle_file(aFile) end repeat end open on handle_file(theFile) tell application "Finder" set name_extension to name extension of theFile -- check if we have a pdf file if name_extension is "pdf" then -- we at least have a PDF file set filename to name of theFile try -- this assumes filename matches expectations ("first last OL.pdf") -- any other formats throw an error and need additional logic handling -- break out the filename into its components set {oldTID, my text item delimiters} to {my text item delimiters, space} set {first_name, last_name, OL} to text items 1 through 3 of filename -- check to match the 'OL' string in the filename if OL ≠ "OL" then error on error -- the file name does not match the expected format display dialog "Filename '" & filename & "' does not match expected format" with icon stop buttons {"Cancel"} default button "Cancel" end try set my text item delimiters to oldTID -- extract the applicant's name set full_name to first_name & space & last_name as text -- do we have an existing folder for this person? if exists folder full_name of cloud_folder then -- we do, so notify the user display dialog "Folder for '" & full_name & "' already exists" with icon stop buttons {"Cancel"} default button "Cancel" else -- make a folder for the applicant set new_folder to make new folder at folder cloud_folder with properties {name:full_name} --and duplicate the file there duplicate theFile to new_folder -- optional: delete the original file: uncomment the next line -- delete theFile end if end if end tell end handle_file
问题原因
核心错误出在文件名拆分逻辑:
- 原脚本直接对完整文件名(包含
.pdf扩展名)按空格拆分,拆分后第三个文本项是OL.pdf而非预期的OL,导致OL ≠ "OL"判断触发错误。 - 文本分隔符恢复语句
set my text item delimiters to oldTID放在try块外部,若try内触发错误,分隔符不会被恢复,可能影响后续脚本逻辑。
修复后的脚本
use AppleScript version "2.4" -- Yosemite (10.10) or later use scripting additions property cloud_folder : "/Users/will/Documents" on adding folder items to theFolder after receiving theNewItems repeat with each_file in theNewItems my handle_file(each_file) end repeat end adding folder items to on run handle_file(choose file) end run on open of theFiles repeat with aFile in theFiles handle_file(aFile) end repeat end open on handle_file(theFile) tell application "Finder" set name_extension to name extension of theFile if name_extension is "pdf" then set filename to name of theFile -- 先移除扩展名,只保留文件名主体 set filename_base to text 1 thru ((length of filename) - (length of name_extension) - 1) of filename try set {oldTID, my text item delimiters} to {my text item delimiters, space} set name_parts to text items of filename_base -- 验证拆分后的部分数量是否为3,且第三部分是OL if (count of name_parts) ≠ 3 or item 3 of name_parts ≠ "OL" then error set first_name to item 1 of name_parts set last_name to item 2 of name_parts -- 立即恢复文本分隔符 set my text item delimiters to oldTID on error set my text item delimiters to oldTID -- 出错时也恢复分隔符 display dialog "Filename '" & filename & "' does not match expected format" with icon stop buttons {"Cancel"} default button "Cancel" return -- 终止当前文件处理 end try set full_name to first_name & space & last_name as text if exists folder full_name of folder cloud_folder then display dialog "Folder for '" & full_name & "' already exists" with icon stop buttons {"Cancel"} default button "Cancel" else set new_folder to make new folder at folder cloud_folder with properties {name:full_name} duplicate theFile to new_folder -- 取消注释以删除原文件 -- delete theFile end if end if end tell end handle_file
关键修复点
- 先剥离扩展名:处理文件名前先移除
.pdf后缀,得到纯文件名主体(如firstname lastname OL),再按空格拆分。 - 增加合法性验证:检查拆分后的部分数量是否为3,同时验证第三部分确实是
OL,避免因名字含空格等特殊情况误判。 - 确保分隔符恢复:在
try块内完成分隔符恢复,且出错时也强制恢复,防止影响后续脚本运行。 - 错误处理优化:出错后立即终止当前文件处理,避免后续逻辑执行。
内容的提问来源于stack exchange,提问作者will02392
相关产品推荐
相关产品推荐

