【问题标题】:Get unrecognized file extension in applescript在applescript中获取无法识别的文件扩展名
【发布时间】:2011-11-28 21:47:59
【问题描述】:

我想知道如何从字符串中只返回文件扩展名。我尝试了this 问题中详述的“将变量设置为...的扩展名”,但这似乎只适用于公认的扩展名。这个想法是将扩展名为“.meta”的文件分类到他们自己的集合中。

我现在的样子

tell application "Finder'
   set everyName to name of every item in entire contents of this_folder
end tell

set metaFiles to {}

repeat with n from 1 to count of everyName
   set currentName to item n of everyName
   set currentExt to last word of currentName --this assignment fails
   if currentExt is "meta" then
      set end of metaFiles to currentExt
   end if
end repeat

我是 applescript 的新手,所以我很感激任何和所有的帮助/指导。谢谢!

编辑:Hacky 解决方案

我通过使用here 描述的拆分函数在每个句点后分解文件名来解决这个问题。我抓住最后一个字符串,确保它与第一个字符串不同,以防没有句点字符,然后存储相应的文件名。

【问题讨论】:

    标签: applescript filenames string-parsing


    【解决方案1】:

    名称包括文件扩展名,无论 Finder 是否识别它。所以只需像这样对名称进行排序......

    tell application "Finder"
        set metaFiles to (every item in entire contents of this_folder whose name ends with "meta") as alias list
    end tell
    

    【讨论】:

      【解决方案2】:

      如果您没有获得名称扩展名,请确保确实有一个并且您没有查看名称的末尾。如果您要移动文件,您还需要获取路径,而不仅仅是名称。我也不认为列出你的扩展名是你想要的 - 几个不同的字符用于单词边界,但句号不是其中之一。

      为什么不直接向 Finder 询问您的文件项?

      tell application "Finder"
          set metaFiles to (every item in entire contents of this_folder whose name extension is "meta") as alias list
      end tell
      

      【讨论】:

      • 'meta' 扩展名只是您建议的名称的一部分,因此 Finder 不会将 'meta' 扩展名视为有效,并且不会填充 metaFiles 列表。但是,我正在从外部传递这些文件,并且无法修改它们以具有适当的扩展名。
      • 如果没有扩展名,则可以测试名称中的其他内容,例如 regulus6633 发布的名称以“meta”结尾的名称
      猜你喜欢
      • 2018-03-25
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      • 2015-04-02
      • 2020-03-17
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      相关资源
      最近更新 更多