AppleScript的:在文件夹中获取文件名没有扩展名 [英] Applescript: Get filenames in folder without extension
问题描述
我可以这样做,得到的所有文件名的文件夹中:
告诉应用程序发现者
设置为MYFILES的somePath每个文件名
告诉结束
我如何更改 MYFILES
的字符串,以便它们不包括文件扩展名?
我可能例如获得 {foo.mov,bar.mov}
,但想有 {富 酒吧}
。
当前的解决方案
根据我想出了下面的code接受的答案。让我知道,如果它可以制成清洁或更有效弄好了。
- 获取从文件名列表
从_folder文件名 - 获取文件名和扩展名
告诉应用程序发现者
设置_filenames为_folder的每一个文件的名称
集_extensions到_folder的每一个文件的扩展名
告诉结束 - 收集名称(文件名 - 点和扩展)
集_names到{}
与n重复1至数_filenames的 设置为_filename的_filenamesñ项
设置_extension为_extensions的项n, 如果_extension不是,则
设置为_length(_filename计数) - (_extension计数) - 1
_names的设置结束文本1直通_length _filename的
其他
_names到_filename的设定结束
万一 重复结束 - 完成
返回_names
最终的文件名 - 用法示例
从(路径到桌面)返回文件名
下面是一个完整的脚本,做你想要的。我舍不得最初发布它,因为我想有一些简单的一行其中有人会提供最便捷的解决方案。希望这个解决方案不是 Rube戈尔登伯格方式。
在查找字典确实有扩展名属性,因此,你可以这样做:
告诉应用程序发现者
集MYFILES到文件1的扩展名(路径为桌面)
告诉结束
所以上面将让你在用户桌面上的第一个文件只是扩展。好像有将是获取(基本名称 - 扩展名)一个简单的功能,但我没有找到一个
下面是获取只是文件名不带扩展为整个目录中的每个文件中的脚本:
设置为filesFound {}
设置filesFound2为{}
设置nextItem 1告诉应用程序发现者
设置为MYFILES(路径为桌面)的每个文件的名称--change路径你想要的任何路径
告诉结束--loop用于填充列表filesFound与发现的所有文件名(名称+扩展名)
与我重复MYFILES
设置filesFound的结束(MYFILES项目nextItem)
设置nextItem至(nextItem + 1)的
重复结束设置nextItem 1 --reset计数器1--loop用于从列表filesFound拉动每个文件名,然后剥去扩展
--from文件名和填充一个名为filesFound2新名单
与我重复filesFound
设置myFile2为filesFound项目nextItem
设置myFile3文本1至(在myFile2(偏移)。 - 1)myFile2的
filesFound2的设置结束myFile3
设置nextItem至(nextItem + 1)的
重复结束返回filesFound2
虽然上面的脚本不工作,如果有人知道做什么OP想请张贴因为我仍然得到这个意义上,应该有这样做的一个简单的方法的简单的方法。也许有一个脚本除了这有利于这一点。任何人都知道?
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)
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?
这篇关于AppleScript的:在文件夹中获取文件名没有扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!