onrun {input, parameters} -- 第一步:选择源文件夹(放pages/docx) -- 就像告诉机器人:“原材料在哪里?” try set sourceFolder tochoose folderwith prompt "👉 选择要转换的源文件夹(包含.pages/.docx文件)" onerror display dialog"已取消选取源文件夹,程序退出" buttons {"确定"} with icon info return endtry
-- 第二步:选择目标文件夹(存PDF) -- 就像告诉机器人:“成品放哪里?” try set outputFolder tochoose folderwith prompt "👉 选择PDF的输出目标文件夹" onerror display dialog"已取消选取目标文件夹,程序退出" buttons {"确定"} with icon info return endtry
-- 转换核心逻辑 set outputPOSIX toPOSIX pathof outputFolder tellapplication"Finder" set targetFiles toeveryfilein sourceFolder whosename extension isin {"pages", "docx"} endtell
-- 检测是否有目标文件 set fileTotal tocountof targetFiles if fileTotal = 0then display dialog"源文件夹中未找到.pages或.docx文件!" buttons {"确定"} with icon caution return endif
-- 批量转换(强制Pages打开,彻底避开WPS) -- 这里就是机器人开始干活了:打开 -> 导出 -> 关闭 set successNum to0 repeatwith oneFile in targetFiles tellapplication"Finder" set fName tonameof oneFile set fExt toname extension of oneFile set pureName totext1thru ((lengthof fName) - (lengthof fExt) - 1) of fName endtell set pdfPath to outputPOSIX & pureName & ".pdf"
try tellapplication"Pages" open (oneFile asalias) export front document toPOSIX file pdfPath as PDF with properties {image quality: Best} close front document saving no endtell set successNum to successNum + 1 onerror err display dialog"文件「" & fName & "」转换失败:" & err buttons {"继续"} with icon warning endtry endrepeat