借助AppleScript从Excel批量创建指定命名格式的文件夹
批量从Excel列生成指定格式文件夹的AppleScript修正方案
嘿,我看你之前用AppleScript批量生成文件夹,但命名格式没达到预期的LASTNAME, FIRSTNAME - ID要求。别担心,我帮你调整了脚本,能精准从Excel的对应列提取信息并生成符合要求的文件夹。
先明确前提
假设你的Excel表格里:
- A列是姓氏(LASTNAME)
- B列是名字(FIRSTNAME)
- C列是ID
- 第一行是表头(如果没有表头,记得把脚本里的
startRow改成1)
修正后的AppleScript代码
tell application "Microsoft Excel" activate set targetSheet to active sheet set lastRow to count of rows of used range of targetSheet set startRow to 2 -- 跳过表头行,没有表头就改成1 -- 让用户选择保存文件夹的目标路径 set saveFolder to choose folder with prompt "选择要创建文件夹的目标位置" repeat with currentRow from startRow to lastRow -- 从对应列提取内容 set lastName to value of cell ("A" & currentRow) of targetSheet as string set firstName to value of cell ("B" & currentRow) of targetSheet as string set idNumber to value of cell ("C" & currentRow) of targetSheet as string -- 拼接成指定格式的文件夹名称 set folderName to lastName & ", " & firstName & " - " & idNumber -- 构建完整的文件夹路径 set folderPath to saveFolder as string & folderName -- 创建文件夹,已存在则跳过避免报错 try do shell script "mkdir -p " & quoted form of POSIX path of folderPath on error errMsg display dialog "创建文件夹 " & folderName & " 时出错:" & errMsg buttons {"确定"} default button 1 end try end repeat display dialog "文件夹批量创建完成!" buttons {"确定"} default button 1 end tell
使用说明
- 打开你的Excel文件,确保要处理的工作表是当前激活的状态
- 打开「脚本编辑器」(Launchpad里找,或者用Spotlight搜索)
- 把上面的代码粘贴进去,根据你的实际列位置和是否有表头调整
startRow以及列标识(比如A/B/C改成你的对应列) - 点击运行按钮(▶️),按照提示选择要存放文件夹的位置,等待脚本执行完成
这个脚本加入了错误处理,如果遇到已存在的文件夹会自动跳过,不会中断整个批量进程,适合处理数千个文件夹的场景。
内容的提问来源于stack exchange,提问作者brownbw
相关产品推荐
相关产品推荐

