Applescript:在没有扩展名的文件夹中获取文件名 [英] Applescript: Get filenames in folder without extension

查看:39
本文介绍了Applescript:在没有扩展名的文件夹中获取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

如何更改 myFiles 中的字符串,使其不包含文件扩展名?

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

例如,我可以得到 {"foo.mov", "bar.mov"},但想要 {"foo", "bar"}.

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

根据接受的答案,我想出了下面的代码.让我知道是否可以以某种方式使其更清洁或更高效.

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)

推荐答案

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

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.

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

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

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

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?

这篇关于Applescript:在没有扩展名的文件夹中获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆