【问题标题】:Identify path to file in AppleScript script as part of Automator workflow在 AppleScript 脚本中识别文件路径作为 Automator 工作流程的一部分
【发布时间】:2020-12-27 09:33:51
【问题描述】:

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

我在 MacBook Air 上运行 MacOSX 10.15.6 (Catalina)。我的最终目标是获取一个包含 .pages 文件(或其他兼容文件类型)的文件夹,并通过使用 Pages 打开然后导出到与新文件类型相同的文件夹来批量转换为 .pdf(或其他)。我设置了一个 Automator 脚本,其中包含: 一世。 Get Specified Finder Items - 定义包含文件的文件夹 ii.获取文件夹内容 - 列出文件夹中的所有文档 iii. AppleScript 打开每个文档并导出为 PDF

甚至在到达“导出”位之前(我已经在下面的位中注释掉了导出命令),当我尝试获取包含文件的目录的路径时,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”操作遇到错误:“Pages got an 错误:无法将别名“path:to:directory:test.pages”转换为类型 不变。”

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

【问题讨论】:

    标签: applescript automator


    【解决方案1】:

    path to 仅返回应用程序或脚本的路径,或文件系统中某些位置的路径,例如 home folderdocuments folder。不过,您不需要使用任何东西来获取路径,因为 theFile 已经是对输入中某个项目的引用 - 您可以将其强制转换为文本。此外,一旦您构建了文件路径,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 但除此之外,答案真的很好。我不太了解细节,但这是一个很好的起点,尤其是添加了描述。谢谢。
    • 当然,这个答案远远超出了最初的有限问题,并提供了将文档批量转换为不同格式的有效解决方案。很棒的东西。
    猜你喜欢
    • 2011-12-27
    • 2015-08-13
    • 2020-12-17
    • 2020-04-30
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    相关资源
    最近更新 更多