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

时间:2010-11-25 15:41:07

标签: applescript filenames finder

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

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)

8 个答案:

答案 0 :(得分: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

答案 1 :(得分:4)

这是一个Applecriptish方法,可以让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

答案 2 :(得分:4)

单行方式,没有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 :(得分: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想要的东西,上面的脚本确实有用,请发布它,因为我仍然觉得应该有一个更简单的方法。也许有一个脚本添加也有助于此。有人知道吗?

答案 4 :(得分: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

答案 5 :(得分:2)

基于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

答案 6 :(得分:1)

在一个告诉" 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

答案 7 :(得分:0)

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

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