作为Automator工作流的一部分,在AppleScript脚本中标识文件的路径

作为Automator工作流的一部分,在AppleScript脚本中标识文件的路径,applescript,automator,Applescript,Automator,对于Automotor和AppleScript,我仍然处于学习曲线的最底部,因此我对不可避免地导致这个问题的基本理解的缺乏表示歉意 我正在MacBookAir上运行MacOSX 10.15.6(Catalina)。我的最终目标是获取一个包含.pages文件(或其他兼容文件类型)的文件夹,并通过打开页面然后导出到与新文件类型相同的文件夹,将其批量转换为.pdf(或其他)。我已设置了一个自动机脚本,该脚本包含: 我获取指定的查找程序项-定义包含文件的文件夹 二,。获取文件夹内容-列出文件夹中的所有文

对于Automotor和AppleScript,我仍然处于学习曲线的最底部,因此我对不可避免地导致这个问题的基本理解的缺乏表示歉意

我正在MacBookAir上运行MacOSX 10.15.6(Catalina)。我的最终目标是获取一个包含.pages文件(或其他兼容文件类型)的文件夹,并通过打开页面然后导出到与新文件类型相同的文件夹,将其批量转换为.pdf(或其他)。我已设置了一个自动机脚本,该脚本包含: 我获取指定的查找程序项-定义包含文件的文件夹 二,。获取文件夹内容-列出文件夹中的所有文档 iii.AppleScript打开每个文档并导出为PDF

甚至在到达“export”位之前(我已经在下面的位中注释掉了export命令),当我试图获取包含该文件的目录的路径时,AppleScript抛出了一个错误。AppleScript看起来像:

on run {input, parameters}
    
    repeat with theFile in input
        
        tell application "Pages"
            set theDoc to open theFile
            set theDocName to name of theDoc
            set theName to (characters 1 thru -7 of theDocName) as text
            set thePDFPath to ((path to theFile as text) & theName & ".pdf") as text
            -- export theDoc to thePDFPath as PDF
            close theDoc
            
        end tell
        
    end repeat
    
end run
我得到的错误是:

“运行AppleScript”操作遇到错误:“页面获得 错误:无法将别名“path:to:directory:test.pages”转换为类型 不变。”


我已经为此挣扎了一段时间,到目前为止,我在网上找到的任何建议都没有帮助解决这个问题。非常感谢您的帮助。

path to
仅返回应用程序或脚本的路径,或文件系统中某些位置的路径,如
主文件夹
文档文件夹
。不过,您不需要使用任何东西来获取路径,因为文件已经是对输入中某个项目的引用-您可以将其强制为文本。此外,一旦构建了文件路径,Pages就需要导出一个文件说明符

请注意,文件路径包含扩展名,因此需要进行一些操作以将其与名称的其余部分分开-在这里,我添加了一个处理程序,将文件路径拆分为包含文件夹、名称和扩展名,以便可以根据需要对其进行修改:

on run {input, parameters}
   repeat with theFile in input
      set {folderPath, fileName, extension} to getNamePieces from theFile
      tell application "Pages"
         set theDoc to open theFile
         set theDocName to name of theDoc
         set theName to (characters 1 thru -7 of theDocName)
         set thePDFPath to (folderPath & fileName & theName & ".pdf")
         export theDoc to file thePDFPath as PDF
         close theDoc
      end tell
   end repeat
end run

to getNamePieces from someItem
   tell application "System Events" to tell disk item (someItem as text)
      set theContainer to the path of container
      set {theName, theExtension} to {name, name extension}
   end tell
   if theExtension is not "" then
      set theName to text 1 thru -((count theExtension) + 2) of theName
      set theExtension to "." & theExtension
   end if
   return {theContainer, theName, theExtension}
end getNamePieces

回答得很好。因为test.pages被导出到testtest.pdf,所以我不得不做一个小的编辑,但除此之外,这个答案工作得非常好。我不太了解细节,但这是一个很好的开始,特别是添加了描述。谢谢。当然,这个答案远远超出了最初的有限问题,提供了一个有效的解决方案,可以将文档批量转换为不同的格式。好东西。