Skip to main content
deleted 28 characters in body; edited tags
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

Thanks for the feedback and feelFeel free to use this if it would be useful for you.

Thanks for the feedback and feel free to use this if it would be useful for you.

Feel free to use this if it would be useful for you.

Rollback to Revision 2
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

EDIT: Previously, when I ran this script on file names with multiple points and no pre-existing extension, text in the filename following the last point would be lost. As a workaround, I added a button to the dialogA test case that forces the new extension to be appended to the full filename. This mode could be useful if you want to keep a record of the previous extension in the filename as well.does not function correctly:

  • The selected file's name has multiple points, but no extension. Here, my script replaces the text after the last point with the new extension.
# Ask user for new extension to be used
set dRecordnewExt to the text returned of (display dialog "Enter extension:" default answer "Do not include first extension point" buttons {"Cancel", "Force Appendation", "OK"} default button "OK" cancel button "Cancel")

set newExt to text returned of dRecord
set dButton to button returned of dRecord

# To change the extension in the default manner
if dButton = "OK" then
    
    tell application "Finder"
        
        # Make a list of the selected files in Finder
        set selFiles to selection as list
        
        set TID to AppleScript's text item delimiters
        
        repeat with eachFile in selFiles
            
            # Make a text item list of the file name delimited by points
            set filePath to eachFile as text
            set AppleScript's text item delimiters to {":"}
            set fileName to last text item of filePath
            set AppleScript's text item delimiters to {"."}
            set textItems to text items of fileName
            
            # Handle case where there is currently no extension, but one should be added.
            if number of textItems is 1 then
                set name of eachFile to fileName & "." & newExt
                
                # If an extension does already exist...
            else
                
                set newName to ""
                set numItems to number of items in textItems
                set n to 1
                
                repeat numItems times
                    
                    # If the current text item is not the extension, add it & "." to the new file name.  No need here to consider the last text item since it's the old extension and we want to get rid of it.
                    if n is not numItems then
                        set newName to newName & item n in textItems & "."
                        set n to n + 1
                    end if
                    
                end repeat
                
                set name of eachFile to newName & newExt
                
            end if
            
        end repeat
        
        # Is this line necessary?  I just saw it at the end of someone else's script that had earlier set TID to AppleScript's text item delimiters.
        set AppleScript's text item delimiters to TID
        
    end tell
    
# To strictly append the new extension to the current filename  
else if dButton = "Force Appendation" then
    
    tell application "Finder"
        
        # Make a list of the selected files in Finder
        set selFiles to selection as list
        
        repeat with eachFile in selFiles
            
            set eachName to name of eachFile as text
            
            set name of eachFile to eachName & "." & newExt
            
        end repeat
        
    end tell
    
end if

EDIT: Previously, when I ran this script on file names with multiple points and no pre-existing extension, text in the filename following the last point would be lost. As a workaround, I added a button to the dialog that forces the new extension to be appended to the full filename. This mode could be useful if you want to keep a record of the previous extension in the filename as well.

# Ask user for new extension to be used
set dRecord to (display dialog "Enter extension:" default answer "Do not include first extension point" buttons {"Cancel", "Force Appendation", "OK"} default button "OK" cancel button "Cancel")

set newExt to text returned of dRecord
set dButton to button returned of dRecord

# To change the extension in the default manner
if dButton = "OK" then
    
    tell application "Finder"
        
        # Make a list of the selected files in Finder
        set selFiles to selection as list
        
        set TID to AppleScript's text item delimiters
        
        repeat with eachFile in selFiles
            
            # Make a text item list of the file name delimited by points
            set filePath to eachFile as text
            set AppleScript's text item delimiters to {":"}
            set fileName to last text item of filePath
            set AppleScript's text item delimiters to {"."}
            set textItems to text items of fileName
            
            # Handle case where there is currently no extension, but one should be added.
            if number of textItems is 1 then
                set name of eachFile to fileName & "." & newExt
                
                # If an extension does already exist...
            else
                
                set newName to ""
                set numItems to number of items in textItems
                set n to 1
                
                repeat numItems times
                    
                    # If the current text item is not the extension, add it & "." to the new file name.  No need here to consider the last text item since it's the old extension and we want to get rid of it.
                    if n is not numItems then
                        set newName to newName & item n in textItems & "."
                        set n to n + 1
                    end if
                    
                end repeat
                
                set name of eachFile to newName & newExt
                
            end if
            
        end repeat
        
        # Is this line necessary?  I just saw it at the end of someone else's script that had earlier set TID to AppleScript's text item delimiters.
        set AppleScript's text item delimiters to TID
        
    end tell
    
# To strictly append the new extension to the current filename  
else if dButton = "Force Appendation" then
    
    tell application "Finder"
        
        # Make a list of the selected files in Finder
        set selFiles to selection as list
        
        repeat with eachFile in selFiles
            
            set eachName to name of eachFile as text
            
            set name of eachFile to eachName & "." & newExt
            
        end repeat
        
    end tell
    
end if

A test case that does not function correctly:

  • The selected file's name has multiple points, but no extension. Here, my script replaces the text after the last point with the new extension.
# Ask user for new extension to be used
set newExt to the text returned of (display dialog "Enter extension:" default answer "Do not include first point")


tell application "Finder"

    # Make a list of the selected files in Finder
    set selFiles to selection as list

    set TID to AppleScript's text item delimiters

    repeat with eachFile in selFiles
    
        # Make a text item list of the file name delimited by points
        set filePath to eachFile as text
        set AppleScript's text item delimiters to {":"}
        set fileName to last text item of filePath
        set AppleScript's text item delimiters to {"."}
        set textItems to text items of fileName
    
        # Handle case where there is currently no extension, but one should be added.
        if number of textItems is 1 then
            set name of eachFile to fileName & "." & newExt
        
        # If an extension does already exist...
        else
        
            set newName to ""
            set numItems to number of items in textItems
            set n to 1
        
            repeat numItems times
            
                # If the current text item is not the extension, add it & "." to the new file name.  No need here to consider the last text item since it's the old extension and we want to get rid of it.
                if n is not numItems then
                    set newName to newName & item n in textItems & "."
                    set n to n + 1
                end if
            
            end repeat
        
            set name of eachFile to newName & newExt
        
        end if
    
    end repeat

    # Is this line necessary?  I just saw it at the end of someone else's script that had earlier set TID to AppleScript's text item delimiters.
    set AppleScript's text item delimiters to TID

end tell
Updated code with additional functionality
Source Link

A test case that does not function correctlyEDIT: Previously, when I ran this script on file names with multiple points and no pre-existing extension, text in the filename following the last point would be lost. As a workaround, I added a button to the dialog that forces the new extension to be appended to the full filename. This mode could be useful if you want to keep a record of the previous extension in the filename as well.

  • The selected file's name has multiple points, but no extension. Here, my script replaces the text after the last point with the new extension.
# Ask user for new extension to be used
set newExtdRecord to the text returned of (display dialog "Enter extension:" default answer "Do not include first extension point" buttons {"Cancel", "Force Appendation", "OK"} default button "OK" cancel button "Cancel")

set newExt to text returned of dRecord
set dButton to button returned of dRecord

# To change the extension in the default manner
if dButton = "OK" then
    
    tell application "Finder"
        
        # Make a list of the selected files in Finder
        set selFiles to selection as list
        
        set TID to AppleScript's text item delimiters
        
        repeat with eachFile in selFiles
            
            # Make a text item list of the file name delimited by points
            set filePath to eachFile as text
            set AppleScript's text item delimiters to {":"}
            set fileName to last text item of filePath
            set AppleScript's text item delimiters to {"."}
            set textItems to text items of fileName
            
            # Handle case where there is currently no extension, but one should be added.
            if number of textItems is 1 then
                set name of eachFile to fileName & "." & newExt
                
                # If an extension does already exist...
            else
                
                set newName to ""
                set numItems to number of items in textItems
                set n to 1
                
                repeat numItems times
                    
                    # If the current text item is not the extension, add it & "." to the new file name.  No need here to consider the last text item since it's the old extension and we want to get rid of it.
                    if n is not numItems then
                        set newName to newName & item n in textItems & "."
                        set n to n + 1
                    end if
                    
                end repeat
                
                set name of eachFile to newName & newExt
                
            end if
            
        end repeat
        
        # Is this line necessary?  I just saw it at the end of someone else's script that had earlier set TID to AppleScript's text item delimiters.
        set AppleScript's text item delimiters to TID
        
    end tell
    
# To strictly append the new extension to the current filename  
else if dButton = "Force Appendation" then
    
    tell application "Finder"
        
        # Make a list of the selected files in Finder
        set selFiles to selection as list
        
        repeat with eachFile in selFiles
            
            set eachName to name of eachFile as text
            
            set name of eachFile to eachName & "." & newExt
            
        end repeat
        
    end tell
    
end if

A test case that does not function correctly:

  • The selected file's name has multiple points, but no extension. Here, my script replaces the text after the last point with the new extension.
# Ask user for new extension to be used
set newExt to the text returned of (display dialog "Enter extension:" default answer "Do not include first point")


tell application "Finder"

    # Make a list of the selected files in Finder
    set selFiles to selection as list

    set TID to AppleScript's text item delimiters

    repeat with eachFile in selFiles
    
        # Make a text item list of the file name delimited by points
        set filePath to eachFile as text
        set AppleScript's text item delimiters to {":"}
        set fileName to last text item of filePath
        set AppleScript's text item delimiters to {"."}
        set textItems to text items of fileName
    
        # Handle case where there is currently no extension, but one should be added.
        if number of textItems is 1 then
            set name of eachFile to fileName & "." & newExt
        
        # If an extension does already exist...
        else
        
            set newName to ""
            set numItems to number of items in textItems
            set n to 1
        
            repeat numItems times
            
                # If the current text item is not the extension, add it & "." to the new file name.  No need here to consider the last text item since it's the old extension and we want to get rid of it.
                if n is not numItems then
                    set newName to newName & item n in textItems & "."
                    set n to n + 1
                end if
            
            end repeat
        
            set name of eachFile to newName & newExt
        
        end if
    
    end repeat

    # Is this line necessary?  I just saw it at the end of someone else's script that had earlier set TID to AppleScript's text item delimiters.
    set AppleScript's text item delimiters to TID

end tell

EDIT: Previously, when I ran this script on file names with multiple points and no pre-existing extension, text in the filename following the last point would be lost. As a workaround, I added a button to the dialog that forces the new extension to be appended to the full filename. This mode could be useful if you want to keep a record of the previous extension in the filename as well.

# Ask user for new extension to be used
set dRecord to (display dialog "Enter extension:" default answer "Do not include first extension point" buttons {"Cancel", "Force Appendation", "OK"} default button "OK" cancel button "Cancel")

set newExt to text returned of dRecord
set dButton to button returned of dRecord

# To change the extension in the default manner
if dButton = "OK" then
    
    tell application "Finder"
        
        # Make a list of the selected files in Finder
        set selFiles to selection as list
        
        set TID to AppleScript's text item delimiters
        
        repeat with eachFile in selFiles
            
            # Make a text item list of the file name delimited by points
            set filePath to eachFile as text
            set AppleScript's text item delimiters to {":"}
            set fileName to last text item of filePath
            set AppleScript's text item delimiters to {"."}
            set textItems to text items of fileName
            
            # Handle case where there is currently no extension, but one should be added.
            if number of textItems is 1 then
                set name of eachFile to fileName & "." & newExt
                
                # If an extension does already exist...
            else
                
                set newName to ""
                set numItems to number of items in textItems
                set n to 1
                
                repeat numItems times
                    
                    # If the current text item is not the extension, add it & "." to the new file name.  No need here to consider the last text item since it's the old extension and we want to get rid of it.
                    if n is not numItems then
                        set newName to newName & item n in textItems & "."
                        set n to n + 1
                    end if
                    
                end repeat
                
                set name of eachFile to newName & newExt
                
            end if
            
        end repeat
        
        # Is this line necessary?  I just saw it at the end of someone else's script that had earlier set TID to AppleScript's text item delimiters.
        set AppleScript's text item delimiters to TID
        
    end tell
    
# To strictly append the new extension to the current filename  
else if dButton = "Force Appendation" then
    
    tell application "Finder"
        
        # Make a list of the selected files in Finder
        set selFiles to selection as list
        
        repeat with eachFile in selFiles
            
            set eachName to name of eachFile as text
            
            set name of eachFile to eachName & "." & newExt
            
        end repeat
        
    end tell
    
end if
I found a test case that breaks the functionality of my code. Adding description of this case.
Source Link
Loading
Source Link
Loading