Applescript: Get filenames in folder without extension

前端 未结 8 1793
忘掉有多难
忘掉有多难 2020-12-11 03:36

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

相关标签:
8条回答
  • 2020-12-11 04:01

    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
    
    0 讨论(0)
  • 2020-12-11 04:02

    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
    
    0 讨论(0)
  • 2020-12-11 04:08

    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
    
    0 讨论(0)
  • 2020-12-11 04:11

    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?

    0 讨论(0)
  • 2020-12-11 04:17

    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
    
    0 讨论(0)
  • 2020-12-11 04:17

    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
    
    0 讨论(0)
提交回复
热议问题