10

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

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)
4

8 回答 8

6

来自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
于 2012-09-21T17:23:12.633 回答
5

这是一种苹果式的方法,可以让 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
于 2013-10-16T23:25:41.213 回答
5

单行方式,没有 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
于 2016-12-20T19:41:15.640 回答
3

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

Finder 字典确实具有名称扩展属性,因此您可以执行以下操作:

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 想要的事情,请发布它,因为我仍然觉得应该有一种更简单的方式来做这件事。也许还有一个脚本添加也可以促进这一点。有人知道吗?

于 2010-11-26T10:34:13.720 回答
2

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

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
于 2010-11-25T17:27:07.133 回答
2

基于 Lauri Ranta 的不错的解决方案,它适用于 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
于 2012-10-16T14:46:22.723 回答
1

在一个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
于 2015-12-29T04:25:30.007 回答
1

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

set theFileName to "test.jpg"
set thePrefix to text 1 thru ((offset of "." in theFileName) - 1) of theFileName
于 2019-12-19T14:46:32.937 回答