gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:21:33 25 4
gpt4 key购买 nike

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

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)

最佳答案

单行方式,没有 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

关于Applescript:在没有扩展名的文件夹中获取文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4278704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com