Applescript:在没有扩展名的文件夹中获取文件名

Svi*_*ish 7 filenames applescript finder

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

tell application "Finder"
    set myFiles to name of every file of somePath
end tell
Run Code Online (Sandbox Code Playgroud)

如何更改字符串以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)
Run Code Online (Sandbox Code Playgroud)

Dan*_*Dan 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
Run Code Online (Sandbox Code Playgroud)


小智 5

这是一个Applescript方法,可让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
Run Code Online (Sandbox Code Playgroud)

  • 这种方法的唯一问题是,在运行脚本之前,用户可能已经隐藏了扩展名。作为一种解决方法,您应该将“ set was_hidden设置为thisFile的隐藏扩展名”添加类似的内容,然后将最后一行更改为“将thisFile的隐藏扩展名设置为was_hidden”。 (2认同)

Jea*_*tiC 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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)


小智 4

这是一个完整的脚本,可以完成您想要的操作。我最初不愿意发布它,因为我认为有人会提供一些简单的单行作为解决方案。希望这个解决方案不是鲁布·戈德堡的做事方式。

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

tell application "Finder"
   set myFiles to name extension of file 1 of (path to desktop)
end tell
Run Code Online (Sandbox Code Playgroud)

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

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

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
Run Code Online (Sandbox Code Playgroud)

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