在applescript中获取无法识别的文件扩展名
我想知道如何从字符串中仅返回文件扩展名。我已经尝试过“将变量设置为...的扩展名”,详细信息请参见 这个问题,但这似乎只适用于公认的扩展。这个想法是将扩展名为“.meta”的文件分类到它们自己的集合中。
我现在所拥有的看起来像
tell application "Finder'
set everyName to name of every item in entire contents of this_folder
end tell
set metaFiles to {}
repeat with n from 1 to count of everyName
set currentName to item n of everyName
set currentExt to last word of currentName --this assignment fails
if currentExt is "meta" then
set end of metaFiles to currentExt
end if
end repeat
我对 applescript 是全新的,所以我感谢任何和所有的帮助/指导。谢谢!
编辑:Hacky 解决方案
我通过使用此处描述的 split 函数解决了这个问题在每个句点后分解文件名。我抓住最后一个字符串,确保它与第一个字符串不同,以防没有句点字符,然后存储相应的文件名。
I was wondering how to return just the file extension from a string. I've tried the 'set variable to name extension of...' detailed in this question, but that only seems to work for recognized extensions. The idea is to sort files with the extension '.meta' into their own collection.
What I have now looks like
tell application "Finder'
set everyName to name of every item in entire contents of this_folder
end tell
set metaFiles to {}
repeat with n from 1 to count of everyName
set currentName to item n of everyName
set currentExt to last word of currentName --this assignment fails
if currentExt is "meta" then
set end of metaFiles to currentExt
end if
end repeat
I'm brand new to applescript so I appreciate any and all help/direction. Thanks!
Edit: Hacky Solution
I solved this by using the split function described here to break up the filename after every period. I grabbed the last string, made sure it wasn't the same as the first string in case there were no period characters, and then stored the corresponding filename.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该名称包含文件扩展名,无论 Finder 是否识别它。所以就像这样对名字进行排序......
The name includes the file extension, whether the Finder recognizes it or not. So just sort on the name like this...
如果您没有获得扩展名,请确保确实有扩展名,并且您没有查看名称的末尾。如果要移动文件,您还需要获取路径,而不仅仅是名称。我也不认为列出扩展名是您想要的 - 几个不同的字符用于单词边界,但句点不是其中之一。
为什么不直接向 Finder 询问您的文件项目呢?
If you aren't getting a name extension, make sure there actually is one and that you aren't looking at the end of the name. If you are going to be moving files around, you will also need to get the path, and not just the name. I don't think that making a list of your extensions is what you are going for, either - several different characters are used for word boundaries, but a period isn't one of them.
Why not just ask Finder for your file items?