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

发布于 2024-10-04 19:25:21 字数 1294 浏览 5 评论 0原文

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

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

How can I Change the strings in myFiles so that those do not include the file extension?

例如,我可以获得 {"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)

I can get the names of all files in a folder by doing this:

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

How can I change the strings in myFiles so that they do not include the file extension?

I could for example get {"foo.mov", "bar.mov"}, but would like to have {"foo", "bar"}.


Current solution

Based on the accepted answer I came up with the code below. Let me know if it can be made cleaner or more efficient somehow.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

讽刺将军 2024-10-11 19:25:21

来自 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

From 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
九厘米的零° 2024-10-11 19:25:21

单行方式执行此操作,没有 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

Single line way of doing it, no Finder, no System Events. So more efficient and faster. Side effect (could be good, or bad): a file name ending with "." will have this character stripped out. Using "reverse of every character" makes it works if the name as more than one period.

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

The solution as a handler to process a list of names:

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
梦初启 2024-10-11 19:25:21

这是一个苹果脚本式的方法,可以让 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

Here's an applescriptish method to get Finder's idea of what the stripped filename is but please note it will only work if you have NOT enabled the option in Finder's preferences to "Show all filename extensions":

set extension hidden of thisFile to true
set thisName to displayed name of thisFile
-- display dialog "hey"
set extension hidden of thisFile to false
渔村楼浪 2024-10-11 19:25:21

这是一个完整的脚本,可以完成您想要的操作。我最初不愿意发布它,因为我认为有人会提供一些简单的单行作为解决方案。希望这个解决方案不是 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想要的操作,请发布它,因为我仍然觉得应该有一个更简单的方法。也许添加脚本也可以促进这一点。有人知道吗?

Here's a full script that does what you wanted. I was reluctant to post it originally because I figured there was some simple one-liner which someone would offer as a solution. Hopefully this solution is not a Rube Goldberg way of doing things.

The Finder dictionary does have a name extension property so you can do something like:

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

So the above will get you just the extension of the first file on the user's desktop. It seems like there would be a simple function for getting the (base name - extension) but I didn't find one.

Here's the script for getting just the filenames without extension for every file in an entire directory:

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

Though the above script does work if anyone knows a simpler way of doing what the OP wanted please post it cause I still get the sense that there should be a simpler way of doing it. Maybe there's a scripting addition which facilitates this as well. Anyone know?

将军与妓 2024-10-11 19:25:21

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

Based on Lauri Ranta's nice solution, which works for extensions that Finder doesn't know about:

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
执手闯天涯 2024-10-11 19:25:21

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

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

I don't know how to remove the extensions when you use the "every file"
syntax but if you don't mind looping (loop not shown in example) through each file then this will work:

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
倒带 2024-10-11 19:25:21

在告诉“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

Within a tell "Finder" block this collects file names stripped of the extension in 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
鹤仙姿 2024-10-11 19:25:21

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

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

For a single file I found the answer here, copied below.

set theFileName to "test.jpg"
set thePrefix to text 1 thru ((offset of "." in theFileName) - 1) of theFileName
半城柳色半声笛 2024-10-11 19:25:21

这比您需要的多一点,但它可以处理多个“。”在文件名中。

假设将文件别名传递给该方法。

on Process(myFileAlias)

    set myFile to myFileAlias as string

    set AppleScript's text item delimiters to ":"
    set myItemCount to the number of text items in myFile
    set myFileName to the last text item of myFile
    set myFilePath to text items 1 through (myItemCount - 1) of myFile

    set AppleScript's text item delimiters to "."
    set myItemCount to the number of text items in myFileName
    set myExtension to the last text item of myFile

    // This line is the key.
    set myShortFilename to text items 1 through (myItemCount - 1) of myFileName as string

    log (myFileName)
    log (myShortFilename)
end

This is a little more than you need, but it will handle more than one "." in a file name.

Assuming that a file alias is passed in to the method.

on Process(myFileAlias)

    set myFile to myFileAlias as string

    set AppleScript's text item delimiters to ":"
    set myItemCount to the number of text items in myFile
    set myFileName to the last text item of myFile
    set myFilePath to text items 1 through (myItemCount - 1) of myFile

    set AppleScript's text item delimiters to "."
    set myItemCount to the number of text items in myFileName
    set myExtension to the last text item of myFile

    // This line is the key.
    set myShortFilename to text items 1 through (myItemCount - 1) of myFileName as string

    log (myFileName)
    log (myShortFilename)
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文