在applescript中获取无法识别的文件扩展名

发布于 2024-12-18 14:05:55 字数 899 浏览 3 评论 0原文

我想知道如何从字符串中仅返回文件扩展名。我已经尝试过“将变量设置为...的扩展名”,详细信息请参见 这个问题,但这似乎只适用于公认的扩展。这个想法是将扩展名为“.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 技术交流群。

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

发布评论

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

评论(2

梦醒灬来后我 2024-12-25 14:05:55

该名称包含文件扩展名,无论 Finder 是否识别它。所以就像这样对名字进行排序......

tell application "Finder"
    set metaFiles to (every item in entire contents of this_folder whose name ends with "meta") as alias list
end tell

The name includes the file extension, whether the Finder recognizes it or not. So just sort on the name like this...

tell application "Finder"
    set metaFiles to (every item in entire contents of this_folder whose name ends with "meta") as alias list
end tell
香橙ぽ 2024-12-25 14:05:55

如果您没有获得扩展名,请确保确实有扩展名,并且您没有查看名称的末尾。如果要移动文件,您还需要获取路径,而不仅仅是名称。我也不认为列出扩展名是您想要的 - 几个不同的字符用于单词边界,但句点不是其中之一。

为什么不直接向 Finder 询问您的文件项目呢?

tell application "Finder"
    set metaFiles to (every item in entire contents of this_folder whose name extension is "meta") as alias list
end tell

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?

tell application "Finder"
    set metaFiles to (every item in entire contents of this_folder whose name extension is "meta") as alias list
end tell
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文