11

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)
Svish
  • 152,914
  • 173
  • 462
  • 620

9 Answers9

7

From http://www.macosxautomation.com/applescript/sbrt/index.html :

on remove_extension(this_name)
  if this_name contains "." then
    set this_name to ¬
    (the reverse of every character of this_name) as string
    set x to the offset of "." in this_name
    set this_name to (text (x + 1) thru -1 of this_name)
    set this_name to (the reverse of every character of this_name) as string
  end if
  return this_name
end remove_extension
Dan
  • 1,257
  • 2
  • 15
  • 31
  • What this is doing is converting a string to a character array, then reversing the character array instead. In my case this was a hack because string reverse failed due to unusual characters. – mwal Dec 13 '22 at 17:57
  • I also think that in the final step you do not need 'of every character'. – mwal Dec 13 '22 at 18:05
5

Here's an applescriptish method to get Finder's idea of what the stripped filename is but please note it will only work if you have NOT enabled the option in Finder's preferences to "Show all filename extensions":

set extension hidden of thisFile to true
set thisName to displayed name of thisFile
-- display dialog "hey"
set extension hidden of thisFile to false
RobK
  • 156
  • 1
  • 9
arno
  • 51
  • 1
  • 1
  • 2
    The only problem with this method is that the user may have had the extension hidden prior to the script running. As a workaround, you should add something like `set was_hidden to extension hidden of thisFile` and then change the last line to `set extension hidden of thisFile to was_hidden` – kabiroberai Aug 23 '16 at 15:06
5

Single line way of doing it, no Finder, no System Events. So more efficient and faster. Side effect (could be good, or bad): a file name ending with "." will have this character stripped out. Using "reverse of every character" makes it works if the name as more than one period.

set aName to text 1 thru ((aName's length) - (offset of "." in ¬
    (the reverse of every character of aName) as text)) of aName

The solution as a handler to process a list of names:

on RemoveNameExt(aList)
    set CleanedList to {}
    repeat with aName in aList
        set the end of CleanedList to text 1 thru ((aName's length) - (offset of ¬
            "." in (the reverse of every character of aName) as text)) of aName
    end repeat
    return CleanedList
end RemoveNameExt
Jean.O.matiC
  • 51
  • 1
  • 2
4

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?

  • Why are you copying all the filesnames from myFiles into filesFound? Can't you just use myFiles directly? – Svish Nov 26 '10 at 13:22
  • I tried it from myFiles directly but it wasn't working...however, I wrote that as soon as I woke up this morning at 3AM pre-coffee so I am quite sure I over-complicated it. Did you find a simpler solution? If so, can you share it? –  Nov 26 '10 at 14:16
  • Your code got my brain moving, blogged about my solution at http://www.geekality.net/?p=1385. Pasting the code in my question so it's available here :) – Svish Nov 26 '10 at 14:17
  • Cool. Thanks for sharing. I am going to check it out now. –  Nov 26 '10 at 14:17
  • Svish, I just checked out your solution on your blog. Yes, that is what I wanted to do once I discovered the "name extension" property but I never pieced it together. That was a good solution. Thanks for sharing. –  Nov 26 '10 at 14:22
2

Based on Lauri Ranta's nice solution, which works for extensions that Finder doesn't know about:

set delims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
set myNames to {}
tell application "Finder"
    set myFiles to name of every file of (path to Desktop)
    repeat with myfile in myFiles
        set myname to name of file myfile
        if myname contains "." then set myname to (text items 1 thru -2 of myname) as text
        set end of myNames to myname
    end repeat
end tell
set AppleScript's text item delimiters to delims
return myNames
Community
  • 1
  • 1
stvs
  • 111
  • 1
  • 7
1

Within a tell "Finder" block this collects file names stripped of the extension in myNames:

repeat with f in myFiles
    set myNames's end to ¬
        (f's name as text)'s text 1 thru -(((f's name extension as text)'s length) + 2)
end repeat
SGIII
  • 11
  • 2
1

I don't know how to remove the extensions when you use the "every file" syntax but if you don't mind looping (loop not shown in example) through each file then this will work:

tell application "Finder"
  set myFile to name of file 1 of somePath
  set myFile2 to text 1 thru ((offset of "." in myFile) - 1) of myFile
end tell
  • The OP will have to loop through each file name in order to get the desired results. – Philip Regan Nov 26 '10 at 07:59
  • Can I do the looping and just change every filename or will I have to copy it all to a new list? – Svish Nov 26 '10 at 08:48
  • 2
    This only works if there are no `.` in the filename portion; it finds the FIRST `.`, not the LAST. For proper solution, see the bottom of this page: http://www.macosxautomation.com/applescript/sbrt/index.html – Dan Sep 21 '12 at 17:22
  • You need to do this from the RIGHT of the text. Otherwise, if there is more than one "." in the file name, it will only get the first one. – Alex Zavatone Sep 13 '22 at 13:37
1

For a single file I found the answer here, copied below.

set theFileName to "test.jpg"
set thePrefix to text 1 thru ((offset of "." in theFileName) - 1) of theFileName
OrigamiEye
  • 864
  • 1
  • 12
  • 31
0

This is a little more than you need, but it will handle more than one "." in a file name.

Assuming that a file alias is passed in to the method.

on Process(myFileAlias)

    set myFile to myFileAlias as string

    set AppleScript's text item delimiters to ":"
    set myItemCount to the number of text items in myFile
    set myFileName to the last text item of myFile
    set myFilePath to text items 1 through (myItemCount - 1) of myFile

    set AppleScript's text item delimiters to "."
    set myItemCount to the number of text items in myFileName
    set myExtension to the last text item of myFile

    // This line is the key.
    set myShortFilename to text items 1 through (myItemCount - 1) of myFileName as string

    log (myFileName)
    log (myShortFilename)
end
Alex Zavatone
  • 4,106
  • 36
  • 54