【问题标题】:Applescript: Get filenames in folder without extensionApplescript:在没有扩展名的文件夹中获取文件名
【发布时间】:2011-05-15 18:56:38
【问题描述】:

我可以通过这样做来获取文件夹中所有文件的名称:

tell application "Finder"
    set myFiles to name of every file of somePath
end tell

如何更改myFiles 中的字符串,使其不包含文件扩展名?

例如,我可以得到{"foo.mov", "bar.mov"},但我想得到{"foo", "bar"}


当前解决方案

根据接受的答案,我想出了下面的代码。让我知道它是否可以以某种方式变得更清洁或更高效。

-- Gets a list of filenames from the
on filenames from _folder

    -- Get filenames and extensions
    tell application "Finder"
        set _filenames to name of every file of _folder
        set _extensions to name extension of every file of _folder
    end tell

    -- Collect names (filename - dot and extension)
    set _names to {}
    repeat with n from 1 to count of _filenames

        set _filename to item n of _filenames
        set _extension to item n of _extensions

        if _extension is not "" then
            set _length to (count of _filename) - (count of _extension) - 1
            set end of _names to text 1 thru _length of _filename
        else
            set end of _names to _filename
        end if

    end repeat

    -- Done
    return _names
end filenames

-- Example usage
return filenames from (path to desktop)

【问题讨论】:

    标签: applescript filenames finder


    【解决方案1】:

    来自http://www.macosxautomation.com/applescript/sbrt/index.html

    on remove_extension(this_name)
      if this_name contains "." then
        set this_name to ¬
        (the reverse of every character of this_name) as string
        set x to the offset of "." in this_name
        set this_name to (text (x + 1) thru -1 of this_name)
        set this_name to (the reverse of every character of this_name) as string
      end if
      return this_name
    end remove_extension
    

    【讨论】:

      【解决方案2】:

      单行方式,没有 Finder,没有系统事件。所以更高效,更快。副作用(可能是好的,也可能是坏的):以“.”结尾的文件名将删除此字符。如果名称超过一个句点,则使用“每个字符的反转”使其有效。

      set aName to text 1 thru ((aName's length) - (offset of "." in ¬
          (the reverse of every character of aName) as text)) of aName
      

      作为处理程序来处理名称列表的解决方案:

      on RemoveNameExt(aList)
          set CleanedList to {}
          repeat with aName in aList
              set the end of CleanedList to text 1 thru ((aName's length) - (offset of ¬
                  "." in (the reverse of every character of aName) as text)) of aName
          end repeat
          return CleanedList
      end RemoveNameExt
      

      【讨论】:

        【解决方案3】:

        这是一种苹果式的方法,可以让 Finder 了解剥离的文件名是什么,但请注意,只有在 Finder 的首选项中没有启用“显示所有文件扩展名”选项时,它才会起作用:

        set extension hidden of thisFile to true
        set thisName to displayed name of thisFile
        -- display dialog "hey"
        set extension hidden of thisFile to false
        

        【讨论】:

        • 此方法的唯一问题是用户可能在脚本运行之前隐藏了扩展。作为一种解决方法,您应该添加类似set was_hidden to extension hidden of thisFile 的内容,然后将最后一行更改为set extension hidden of thisFile to was_hidden
        【解决方案4】:

        这是一个完整的脚本,可以满足您的需求。我最初不愿意发布它,因为我认为有人会提供一些简单的单线作为解决方案。希望这个解决方案不是Rube Goldberg 的做事方式。

        Finder 字典确实具有 name extension 属性,因此您可以执行以下操作:

        tell application "Finder"
           set myFiles to name extension of file 1 of (path to desktop)
        end tell
        

        因此,以上内容将为您提供用户桌面上第一个文件的扩展名。似乎有一个简单的函数来获取(基本名称 - 扩展名),但我没有找到。

        以下是获取整个目录中每个文件的不带扩展名的文件名的脚本:

        set filesFound to {}
        set filesFound2 to {}
        set nextItem to 1
        
        tell application "Finder"
          set myFiles to name of every file of (path to desktop) --change path to whatever path you want   
        end tell
        
        --loop used for populating list filesFound with all filenames found (name + extension)
        repeat with i in myFiles
          set end of filesFound to (item nextItem of myFiles)
          set nextItem to (nextItem + 1)
        end repeat
        
        set nextItem to 1 --reset counter to 1
        
        --loop used for pulling each filename from list filesFound and then strip the extension   
        --from filename and populate a new list called filesFound2
        repeat with i in filesFound
          set myFile2 to item nextItem of filesFound
          set myFile3 to text 1 thru ((offset of "." in myFile2) - 1) of myFile2
          set end of filesFound2 to myFile3
          set nextItem to (nextItem + 1)
        end repeat
        
        return filesFound2
        

        虽然上面的脚本确实有效,但如果有人知道一种更简单的方式来做 OP 想要的事情,请发布它,因为我仍然觉得应该有一种更简单的方式来做这件事。也许有一个脚本添加也可以促进这一点。有人知道吗?

        【讨论】:

        • 为什么要将 myFiles 中的所有文件名复制到 filesFound 中?不能直接使用 myFiles 吗?
        • 我直接从 myFiles 尝试了它,但它没有用......但是,我今天早上 3 点在喝咖啡前醒来时就写了这个,所以我很确定我过于复杂了它。你找到更简单的解决方案了吗?如果有,可以分享一下吗?
        • 您的代码让我动了脑筋,在geekality.net/?p=1385 上写了关于我的解决方案的博客。将代码粘贴到我的问题中,以便在此处可用:)
        • 酷。感谢分享。我现在要去看看。
        • Svish,我刚刚在您的博客上查看了您的解决方案。是的,这就是我一发现“名称扩展”属性就想做的事情,但我从来没有把它拼凑起来。那是一个很好的解决方案。感谢分享。
        【解决方案5】:

        当您使用“每个文件”时,我不知道如何删除扩展名 语法,但如果您不介意在每个文件中循环(示例中未显示循环),那么这将起作用:

        tell application "Finder"
          set myFile to name of file 1 of somePath
          set myFile2 to text 1 thru ((offset of "." in myFile) - 1) of myFile
        end tell
        

        【讨论】:

        • OP 必须遍历每个文件名才能获得所需的结果。
        • 我可以循环播放并更改每个文件名还是必须将其全部复制到新列表中?
        • 这仅在文件名部分没有. 时有效;它找到第一个.,而不是最后一个。有关正确的解决方案,请参阅本页底部:macosxautomation.com/applescript/sbrt/index.html
        【解决方案6】:

        基于 Lauri Ranta 的 nice solution,它适用于 Finder 不知道的扩展:

        set delims to AppleScript's text item delimiters
        set AppleScript's text item delimiters to "."
        set myNames to {}
        tell application "Finder"
            set myFiles to name of every file of (path to Desktop)
            repeat with myfile in myFiles
                set myname to name of file myfile
                if myname contains "." then set myname to (text items 1 thru -2 of myname) as text
                set end of myNames to myname
            end repeat
        end tell
        set AppleScript's text item delimiters to delims
        return myNames
        

        【讨论】:

          【解决方案7】:

          在一个告诉“Finder”块中,这会收集在 myNames 中去除扩展名的文件名:

          repeat with f in myFiles
              set myNames's end to ¬
                  (f's name as text)'s text 1 thru -(((f's name extension as text)'s length) + 2)
          end repeat
          

          【讨论】:

            【解决方案8】:

            对于单个文件,我找到了答案here,复制如下。

            set theFileName to "test.jpg"
            set thePrefix to text 1 thru ((offset of "." in theFileName) - 1) of theFileName
            

            【讨论】:

            • 这不适用于具有多个“.”的文件。以他们的名义
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-02-14
            • 2019-07-07
            相关资源
            最近更新 更多