【问题标题】:AppleScript - How to get a folder's first child path (only when the first child is a folder, not a file)?AppleScript - 如何获取文件夹的第一个子路径(仅当第一个子是文件夹而不是文件时)?
【发布时间】:2021-10-15 18:44:50
【问题描述】:

如何获得文件夹的第一个子路径(当第一个子是文件夹时)?

说你有...

Folder 1 ¬
  File 1
  File 2
  Folder 2 ¬
    File A
    File B
    File C
    File D

用例:

我选择了一批文件夹,它们都与Folder 1 处于同一级别: set foldersToProcess to choose folder with multiple selections allowed

然后,我遍历每个排队的文件夹foldersToProcess,在此过程中,我想查看Folder 1(即Folder 2)的第一个子文件夹,每次。

我该怎么做?

【问题讨论】:

    标签: applescript parent-child script


    【解决方案1】:

    您应该使用 try block,因为可能某些所选文件夹根本不包含子文件夹。因此,如果没有 try 块,它会在请求空列表的文件夹 1 时抛出错误。

    将第一个文件夹作为系统事件的文件夹引用

    set chosenFolders to (choose folder with multiple selections allowed)
    
    set firstFolders to {}
    repeat with anAlias in chosenFolders
        try
            tell application "System Events" to ¬
                set end of firstFolders to folder 1 of folder (anAlias as text)
        end try
    end repeat
    

    要获取第一个文件夹的 HFS 路径,请编辑相应的代码行:

    tell application "System Events" to ¬
                set end of firstFolders to path of folder 1 of folder (anAlias as text)
    

    要获取第一个文件夹的 Posix 路径,编辑相应的代码行:

    tell application "System Events" to ¬
                set end of firstFolders to POSIX path of folder 1 of folder (anAlias as text)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 2014-12-30
      • 2015-01-25
      相关资源
      最近更新 更多