File Name Length

I must preface this post with the fact that I am fairly new applescript but catch on quick.

I work in an office that uses an old version of quark and classic. This forces us to restrict our file names to something like 31 characters. So we are devising a file naming conventions that contains vital information in the file name but limiting it to the 31 characters. To make it easier on everyone who uses all the other programs and operating systems that don’t restrict file name lengths I wanted to come up with a droplet script that will essentially tell them that the file name is too long and that they need to shorten it by so many characters.

I have attempted to start small, hoping to build on the idea as I learn more.
My initial script was intended to just capture the length of the file name dropped on the script and display and alert with the length of the name.

on open thisItem
	tell application "Finder"
		set itemName to displayed name of file thisItem
		set charcount to count of characters of itemName
		display alert charcount giving up after 3
	end tell
end open

For some reason I get and error like: “Can’t make some data into the expected type.”
What am I doing wrong?

you where close only problem is that on open gives you a list of items not a single item even if you only put one item on so it is best to loop through



on open theitems
	repeat with thisItem in theitems
		tell application "Finder"
			set itemName to displayed name of file thisItem
			set charcount to count of characters of itemName
			display alert charcount giving up after 3
		end tell
	end repeat
end open

or if you know you only want to tackle one item you can do it like this


on open theitems
	set thisItem to item 1 of theitems
	tell application "Finder"
		set itemName to displayed name of file thisItem
		set charcount to count of characters of itemName
		display alert charcount giving up after 3
	end tell
end open

mm

charcount is of class number (integer). display alert expects a string.

display alert (charcount as text) giving up after 3

I recommend to use name instead of displayed name because the name extension is always a part of the file name even it won’t be displayed.

Great!
I got that part working. Now I have moved on to presenting a display dialog that shows the old name and asks for a new one. Here is where I am so far.

property max_char : 27

on open theitems
	repeat with thisItem in theitems
		tell application "Finder"
			set itemName to displayed name of thisItem
			set extName to name extension of thisItem
			set charCounts to count of characters of itemName
			set extCounts to (count of character of extName) + 1
			if charCounts - extCounts > max_char then
				display dialog "This name is too long. Please shorten." default answer itemName
				set displayed name of file thisItem to text returned
			end if
		end tell
	end repeat

Every thing works fine till I want to get the text returned and use it to make the new file name. I would also like to take this a step further and if the new name is still to long prompt to try again. I think I have that part figured out, but I did not want to proceed until I get this part fixed. Any thoughts?

try it this way


property max_char : 27

on open theitems
	repeat with thisItem in theitems
		tell application "Finder"
			set itemName to displayed name of thisItem
			set extName to name extension of thisItem
			set charCounts to count of characters of itemName
			set extCounts to (count of character of extName) + 1
			if charCounts - extCounts > max_char then
				
				set displayed name of file thisItem to text returned of (display dialog "This name is too long. Please shorten." default answer itemName)
			end if
		end tell
	end repeat
end open