Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top