Replacing spaces in names with underscores

I am beginning my way to Applescript…

Still very much a novice, I am attempting to make use of scripting to insert an underscore “_” to all filenames (in a folder) that have spaces between them or vice versa. I feel this is possible. Any known script or archives that I can dig to perform this task?

pharme :wink:

Some sample code here:
http://www.macscripter.net/scriptbuilders/category.php?search=underscore


global listFiles
set m to choose folder
set srcPath to (m as string)
set listFiles to {}

global delim
set delim to AppleScript's text item delimiters

tell me to AddFolder(srcPath)
set s to ""
set cnt to display dialog "Modify the names of " & (count of listFiles) & " files?
This Script will remove spaces, tabs, &, *, \, !, #, @, and a whole host of other crap from filenames. It just removes them, no substitution is done."
if the button returned of cnt is "OK" then
	tell application "Finder"
		repeat with i from 1 to count of listFiles
			set elem to (item i of listFiles) as string
			set fileItem to alias elem
			set strName to name of fileItem
			tell me to set strName to FixName(strName, " ")
			tell me to set strName to FixName(strName, "	")
			tell me to set strName to FixName(strName, "~")
			tell me to set strName to FixName(strName, "`")
			tell me to set strName to FixName(strName, "!")
			tell me to set strName to FixName(strName, "@")
			tell me to set strName to FixName(strName, "#")
			tell me to set strName to FixName(strName, "$")
			tell me to set strName to FixName(strName, "%")
			tell me to set strName to FixName(strName, "^")
			tell me to set strName to FixName(strName, "&")
			tell me to set strName to FixName(strName, "*")
			tell me to set strName to FixName(strName, "(")
			tell me to set strName to FixName(strName, ")")
			tell me to set strName to FixName(strName, "=")
			tell me to set strName to FixName(strName, "+")
			tell me to set strName to FixName(strName, "{")
			tell me to set strName to FixName(strName, "}")
			tell me to set strName to FixName(strName, "[")
			tell me to set strName to FixName(strName, "]")
			tell me to set strName to FixName(strName, "|")
			tell me to set strName to FixName(strName, "\")
			tell me to set strName to FixName(strName, "/")
			tell me to set strName to FixName(strName, "'")
			tell me to set strName to FixName(strName, ",")
			tell me to set strName to FixName(strName, """)
			set name of fileItem to strName
		end repeat
	end tell
end if
set AppleScript's text item delimiters to delim
display dialog "This Script is complete." buttons "OK"

on FixName(currentName, fixString)
	set AppleScript's text item delimiters to fixString
	set listName to every text item of currentName
	set AppleScript's text item delimiters to ""
	return (listName as string)
end FixName
on AddFolder(pt)
	tell application "Finder"
		set topFolder to alias pt
		set srcContents to every item of topFolder
		repeat with i from 1 to count of srcContents
			set elem to item i of srcContents
			if last character of (elem as string) is ":" then
				copy elem to the end of listFiles
				tell me to AddFolder(elem as string)
			else
				tell me to AddFile(elem as string)
			end if
		end repeat
	end tell
end AddFolder

on AddFile(pt)
	set theFile to alias pt
	copy theFile to the end of listFiles
end AddFile

Thanks jj and tingham for the prompt replies, unfortunately none works as I wanted; to replace the spaces for all filenames in a folder with an underscore.

On jj, Over Score 1.0, it triggered no actions upon hitting run, and Replace Text in Selected Names 1.0 required one to change the file names one by one. I might be proceeding it wrongly, but it should be as simple as hitting run and let actions work on its own, isn’t it?

On tingham, the script rendered manages to removes space between words in the filenames but is not capable to replace them (the spaces) with an underscore.

Nevertheless, I have to thank you for the replies, or perhaps they are indeed the right scripts, just that i missed out on one thing or two.

pharme

:oops: :oops: :oops:

Try this…

set defDel to AppleScript's text item delimiters
tell application "Finder"
	set theFolder to folder (choose folder)
	repeat with thisItem in theFolder
		set thename to name of thisItem
		if thename contains " " then
			set AppleScript's text item delimiters to " "
			set newname to text items of thename
			set AppleScript's text item delimiters to "_"
			set name of thisItem to (newname as string)
			set AppleScript's text item delimiters to defDel
		end if
	end repeat
end tell

Thanks Carl, that just neatly fixed the job.

Appreciated.

pharme