代码之家  ›  专栏  ›  技术社区  ›  Svish

Applescript:在不带扩展名的文件夹中获取文件名

  •  7
  • Svish  · 技术社区  · 15 年前

    通过执行以下操作,我可以获取文件夹中所有文件的名称:

    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)
    
    7 回复  |  直到 15 年前
        1
  •  3
  •   user225057user225057    15 年前

    这是一个完整的脚本,可以满足你的要求。我不愿意把它贴出来,因为我觉得有一个简单的一行,有人会提供一个解决方案。希望这个解决方案不是 Rube Goldberg 做事的方式。

    查找词典确实有 名称扩展名 属性,以便您可以执行以下操作:

    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
    

    尽管上面的脚本是有效的,如果有人知道一个简单的方法来做什么,请张贴它,因为我仍然有感觉,应该有一个更简单的方法来做。也许还有一个脚本添加,这也有助于这一点。有人知道吗?

        2
  •  6
  •   Dan    13 年前

    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
    
        3
  •  5
  •   arno    12 年前

    下面是一个applescriptish方法,可以让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
    
        4
  •  4
  •   Jean.O.matiC    9 年前

    单线方式,无查找程序,无系统事件。所以效率更高,速度更快。副作用(可能是好的,也可能是坏的):以“.”结尾的文件名将删除此字符。如果名称是多个句点,则使用“每个字符的倒序”可以使其工作。

    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
    
        5
  •  2
  •   user225057user225057    15 年前

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

    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
    
        6
  •  2
  •   Community Mohan Dere    9 年前

    基于劳里·兰塔的 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
  •  1
  •   SGIII    10 年前

    在tell“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