AppleScript: Get name of files in a folder without extension

Have been dipping my toes into some AppleScript today. Boy do I prefer languages that are not so wordy and English… And boy is some things not the easiest to do… Anyways, I wanted to get the names of all files in a certain folder. This is quite easy, but the twist is that I didn’t want the file extension. This was not so easy…

So, in case anyone else needs to do this, or if anyone would happen to have a better, or faster, or more robust way to do this, I share it here. Please comment if you do know of anything which can improve it.

I asked a question on StackOverflow and got some code that worked. I then rewrote it a bit and ended up with this:

-- 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
            set end of _names to _filename
        end if
    end repeat
    -- Done
    return _names
end filenames

-- Example usage
return filenames from (path to desktop)

Hope that can help someone! If you have any improvements, please post a comment below 🙂

  • Mason Rove

    Cool. That works.

    • Sure does! Thanks for getting me past my mind block 😛

  • Hello! I really appreciate you creating this blog. It helps ignorant people like me with their problems. I am, by all means, NOT a programmer but I am trying. So you’ll know where I’m coming from. I have a really stupid question. “How do you change the path?”. Every time I change “desktop” to “Documents” or to anything else, for that matter, on the last line I get an error.

    • That’s probably because ‘desktop’ is a special keyword. You’d probably have to swap it out with a string of some sort. Don’t really have a Mac I can test it out on now, so you’ll have to figure it out through apple script documentation or asking on