Remove all non numeric characters from a filename

I would like to remove all non-numeric characters from a filename. Is there a way to do this with applescript?

For example, I would like to change a filename from 1photo.jpg to 1.jpg

Any and all help is greatly appreciated.

Thanks!

PS- I am running OS X 10.9.5 and Apple Script Editor 2.3.2

Hi,

there are several ways, one of them is the shell tr command


set theFile to choose file
tell application "System Events" to set {name:fileName, name extension:fileExtension} to theFile
set trimmedFileName to (do shell script "echo " & quoted form of fileName & " | tr -d '[:alpha:]'") & fileExtension

Thank you for the reply! I tried it out, and it appears to run, but the filename doesn’t change. I copied and pasted your code into Apple Script Editor and then ran it. The result at the bottom appears correct, but the change isn’t reflected in the actual file in the folder. Am I missing something?

Thank you!!

What you do is set a string of digits “0123456789”. Then set texitem delimiters to these. So, when you get the text items they won’t include these. I think you can do that. Need to test it just theory so far. :slight_smile:

Thanks kel1 for the reply. I am really new to all of this so I am not sure how I would set up what you mentioned. I have seen the tab delimited before, but not sure how to go about it.

Thanks!

Here is a version, I have made out of StefanK’s version, that does the actual renaming if there is something left when all letters are removed from the filename, and there is no file with the produced name in that folder. Otherwise, the file is silently skipped.

set theF to choose file
tell application "System Events"
	tell item (theF as text)
		set {name:fileName, name extension:fileExtension} to it
		set parentPath to path of its container as text
	end tell
end tell
try
	set trimmedFileName to (do shell script "tr -d '[:alpha:]' <<<" & quoted form of fileName & " | grep -E [[:digit:]] ")
on error
	-- no digits in the resulting filename!
	set trimmedFileName to ""
end try
if trimmedFileName is not "" then
	try
		tell application "System Events"
			if not (exists item (parentPath & trimmedFileName & fileExtension)) then
				tell item (theF as text)
					set name of it to (trimmedFileName & fileExtension)
				end tell
			end if
		end tell
	on error e number n
		tell application (path to frontmost application as text)
			display alert "An error occured during rename" & ":
" & e & " # : " & n
		end tell
	end try
end if

Edit
Added a check for digits in the resulting filename.
And rudimentary error handling for the case that the file is write protected.

What you do is set as text item delimiters to 1234567890. Then yogurt the text items. Then coerce to string.

Edited: that should eliminate all the digits.

Thanks McUsr11! I tried that and it also removes the file extension and seems to change it to a Unix Executable File. Is it possible to keep the file extension and file type to remain the same? Thanks for all your help!

instead of typing all the digits into a list you do it programmatically

gl,

Hello.

It should keep the file extension now.

Thanks McUsrII! That worked like a charm. Really appreciate the help!!

How about returning all the words from a file name, use them as text item delimiters, then set the text items to “” and return the text?

like this:

set mfilename to "123 herz"
set tw to words of mfilename
repeat with i from 1 to (count tw)
	try
		(item i of tw) as number
		set item i of tw to missing value
	end try
end repeat
set tw to tw's text
set end of tw to space
set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, tw}
set bits to text items of mfilename
set AppleScript's text item delimiters to ""
set newfilename to bits as text
set AppleScript's text item delimiters to tids
return newfilename
-- > "123"

I hindsightly added some code for checking for errors during rename (post #6) :slight_smile:

The status of word vary according to the local settings so basing the process upon this structure is not a reliable scheme.

As a filename is not a huge string, good old scheme may be good.

set theOriginalName to "azs | er12 | ”34_56#@¢.jpg"

# Here, put code grabbing the file path in filePath and the file name in theOriginalName

set tempList to my decoupe(theOriginalName, ".")
set shortName to my recolle(items 1 thru -2 of tempList, ".")
set theExt to item -1 of tempList

set cleanList to {}
repeat with aChar in (characters of shortName)
	set aChar to contents of aChar
	if "0123456789" contains aChar then set end of cleanList to aChar
end repeat
set cleanName to my recolle(cleanList, "") & "." & theExt
--> "123456.jpg"

# Here code to set name of file filePath to cleanName

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

on recolle(l, d)
	local oTIDs, t
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set t to l as text
	set AppleScript's text item delimiters to oTIDs
	return t
end recolle

#=====

Yvan KOENIG (VALLAURIS, France) vendredi 20 mars 2015 09:17:45