Can't Set File Name to New Name

For consistency purposes, we’re being asked to upload all our files to the server in a specific format”initial caps for each word and no spaces. Unless the file is on the Desktop, I get the Syntax Error: “Finder got an error: Can’t set file “My data file copy.rtf” to “MyDataFile.rft”.”


	set validChars to "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_."
	set outputName to {}
	
	tell application "Finder"
		set fileName to selection as text
		set TheName to name of (fileName as alias)
		set capName to do shell script "/bin/echo " & quoted form of TheName & " | /usr/bin/awk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1'"
		set wordCount to number of words in capName
		if wordCount is greater than 2 then
			ignoring case
				set newName to every character of capName
				repeat with eachChar in newName
					if (eachChar as text) is in validChars then
						copy (eachChar as text) to end of outputName
					end if
				end repeat
				set finalName to outputName as text
				duplicate fileName
				set copyFile to selection as text
				set copyName to name of (copyFile as alias)
				set name of file copyName to finalName
			end ignoring
			set the clipboard to "http://www.domain.com/" & finalName
		else
			set the clipboard to "http://www.domain.com/" & TheName
		end if
	end tell

Hi,

copyName is a file name, the Finder expects a file path to an existing file.

PS:

Maybe this is sufficient


tell application "Finder"
	set theFile to item 1 of (get selection)
	set TheName to name of theFile
end tell
set capName to do shell script "/bin/echo " & quoted form of TheName & " | /usr/bin/awk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1' | tr -cd [:alnum:]_. "
set name of theFile to capName
set the clipboard to "http://www.domain.com/" & capName


How do I fix it, because the same procedure seems to work above for extracting the file’s name, capitalizing it and removing any extra characters.

Damn, you’re quick. I’ve been working on a copy of the file, because I want to keep my personal naming convention on my personal machine, and I’m not sure I can revert back to it after I add this script to an Automator workflow that uploads the file to our server.

this duplicates also the file



tell application "Finder"
	set theFile to item 1 of (get selection)
	set TheName to name of theFile
	set duplicatedFile to duplicate theFile
end tell
set capName to do shell script "/bin/echo " & quoted form of TheName & " | /usr/bin/awk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1' | tr -cd [:alnum:]_. "
tell application "Finder" to set name of duplicatedFile to capName
set the clipboard to "http://www.domain.com/" & capName