【问题标题】:Find absolute paths to folder contents查找文件夹内容的绝对路径
【发布时间】:2011-11-30 05:27:19
【问题描述】:

另一个新手applescript问题。我试图在打开文件夹时获取文件夹内所有文件夹和文件的绝对路径。我想写一些类似的东西

on opening folder this_folder
   tell application "Finder"
      set everyPath to POSIX path of every item in entire contents of this_folder
   end tell

   repeat with n from 1 to count of everyPath
      display dialog item n of everyPath
   end repeat
end opening folder

但这是一块砖头,所以现在我有这个更丑陋的烂摊子。

on opening folder this_folder
   tell application "Finder"
      set everyName to name of every item in entire contents of this_folder
      set everyPath to {}
      repeat with n from 1 to count of everyName
         set end of everyPath to POSIX path of item n of everyName
      end repeat
   end tell

   repeat with n from 1 to count of everyPath
      display dialog item n of everyPath
   end repeat
end opening folder

当我在寻找更像“用户/用户名/文档/文件夹/file.ext”和“用户/用户名/文档/文件夹/子文件夹/file2.ext”的内容时,会显示像“/file.ext”这样的对话框'。

从我倾向于错过过去显而易见的事情来看,我假设有一种简单的方法可以获取我只是忘记的完整路径名,如果能帮助我整理它,我将不胜感激。谢谢!

【问题讨论】:

    标签: macos applescript filepath


    【解决方案1】:

    您无法按照您尝试的方式进行操作。通常,您只需将文件夹路径添加到名称即可获得完整路径。但是,您还会获得子文件夹中文件的名称,因此这不起作用,因为您不知道子文件夹路径。因此,从 Finder 中获取名称对您没有帮助。对于这样的事情,比 Finder 更快的方法可能是使用 unix 程序“find”。

    set this_folder to path to desktop
    
    -- we have to remove the trailing / from the folder path so the
    -- returned paths from the find command are correct
    set posix_folder to text 1 thru -2 of (POSIX path of this_folder)
    
    -- use find to quickly find the items in this_folder
    -- also filter out invisible files and files in invisible folders
    -- also filter files inside of package files like ".app" files
    set theItems to paragraphs of (do shell script "find " & quoted form of posix_folder & " -name \"*.*\" ! -path \"*/.*\" ! -path \"*.app/*\" ! -path \"*.scptd/*\" ! -path \"*.rtfd/*\" ! -path \"*.xcodeproj/*\"")
    

    如果 find 命令不合适,那么您可以以类似的方式使用 mdfind 命令(例如,通过命令行聚焦)。

    【讨论】:

    • 谢谢!这很好用。唯一的问题是我还需要返回子目录的路径,而不仅仅是其中的文件。有没有办法使用 unix 做到这一点?
    猜你喜欢
    • 2011-10-01
    • 1970-01-01
    • 2013-04-11
    • 1970-01-01
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 2010-11-19
    相关资源
    最近更新 更多