number of characters in filenames

Can anyone help me with the following:

In need a script (droplet) that checks filenames including the suffix, they shouldn’t be longer than 27 characters
When found, I would like to have them placed in a subfolder of the current folder
Also a popup for the result - when ok: just say so - when not ok: a button that directs me to the subfolder

Any help is greatly appreciated!!

–Peter–

This should get you started:

set theFile to choose file

tell application "Finder"
	set TheName to name of theFile
	set TheLength to (count of characters of TheName)
end tell

Hi Peter
give this is script a try! If you drop a folder of files onto it the script will check the length of the files name and if it exceeds 27 characters it’ll create a new folder and place the offending files in it. When it’s finished processing the files it’ll tell you how many files are ok and how many have been moved to the new folder and ask if you want to open the sub-folder.

on open (source_folder)
	set folder_path to the source_folder as string
	set exceeded_length_folder to folder_path & "exceeded_length_folder"
	set this_folder to item 1 of source_folder
	tell application "Finder"
		set item_list to every file of the this_folder
		repeat with thefile in item_list
			set file_name to the name of thefile
			set string_count to count characters of file_name
			if string_count > 27 then
				if exists alias exceeded_length_folder then
					move thefile to folder exceeded_length_folder
				else
					make new folder of source_folder with properties {name:"exceeded_length_folder"}
					move thefile to folder exceeded_length_folder
				end if
			end if
		end repeat
		set OK_Files to every file of alias folder_path
		set OK_Count to count items of OK_Files
		set myfilecount to the result as string
		display dialog myfilecount & " files do not exceed 27 Characters"
		if exists alias exceeded_length_folder then
			set exceeded_Files to every file of alias exceeded_length_folder
			set exceeded_Count to count items of exceeded_Files
			set myexceededfilecount to the result as string
			set button_pressed to button returned of (display dialog myexceededfilecount & " files exceeds 27 Characters" & return & "Open exceeded_length_folder?" buttons {"No", "Yes"} default button "Yes")
			if button_pressed = "Yes" then
				open alias exceeded_length_folder
			end if
		end if
	end tell
end open

You’ll need to save this as an application script.

You don’t need to get characters of a string to count them. e.g.

set n to “someitemname”
count n

or:

set n to “someitemname”
length of n

gl,

Thanks for the quick response, the script works!!
But what I would really like is to drop files on the application and not a folder
So the script has to get the path of the files and create a subfolder

–Peter–

Hi Peter,
See if this is closer to what you want.!!

on open thefiles
	repeat with this_file in thefiles
		set this_files_path to this_file as string
		tell application "Finder" to set filename to the name of this_file
		count filename
		set char_count to the result
		set myoffset to offset of filename in this_files_path
		set myoffset to myoffset - 1
		set file_path to characters 1 thru myoffset of this_files_path as string
		set exceeded_length_folder to file_path & "exceeded_length_folder"
		if char_count > 27 then
			tell application "Finder"
				if exists alias exceeded_length_folder then
					move this_file to folder exceeded_length_folder
				else
					make new folder of alias file_path with properties {name:"exceeded_length_folder"}
					move this_file to folder exceeded_length_folder
				end if
				set button_pressed to button returned of (display dialog filename & ": exceeds 27 Characters" & return & return & "Open exceeded_length_folder?" buttons {"No", "Yes"} default button "Yes" with icon 0)
				if button_pressed = "Yes" then
					open alias exceeded_length_folder
				end if
			end tell
		else
			tell application "Finder" to display dialog filename & ": is OK" with icon 1
		end if
	end repeat
end open

Thanks to Kel for pointing out my uneccessary keystrokes!:smiley:

Hé,
This one is suits my needs better.
The only thing is that I get a ppopup for every file that is okay.
When dropping 30 or more files…
Is it possible to have just one popup for files that are ok no need for filenames or count just aconfirmation

Thanks so far

Hey Peter,
try this!

on open thefiles
	count thefiles
	set number_of_files to the result
	--display dialog number_of_files
	set ok_flag to 0
	set bad_flag to 0
	repeat with this_file in thefiles
		set this_files_path to this_file as string
		tell application "Finder" to set filename to the name of this_file
		count filename
		set char_count to the result
		set myoffset to offset of filename in this_files_path
		set myoffset to myoffset - 1
		set file_path to characters 1 thru myoffset of this_files_path as string
		set exceeded_length_folder to file_path & "exceeded_length_folder"
		if char_count > 27 then
			tell application "Finder"
				if exists alias exceeded_length_folder then
					move this_file to folder exceeded_length_folder
				else
					make new folder of alias file_path with properties {name:"exceeded_length_folder"}
					move this_file to folder exceeded_length_folder
				end if
				set button_pressed to button returned of (display dialog filename & ": exceeds 27 Characters" & return & return & "Open exceeded_length_folder?" buttons {"No", "Yes"} default button "Yes" with icon 0)
				if button_pressed = "Yes" then
					open alias exceeded_length_folder
				end if
				set bad_flag to bad_flag + 1
			end tell
		else
			set ok_flag to ok_flag + 1
		end if
	end repeat
	tell application "Finder" to display dialog "Total of files processed: " & (number_of_files as string) & return & "Total of OK files: " & ok_flag & return & "Total of files exceeding 27 chars " & bad_flag with icon 1
end open

:slight_smile:

Great,
This is exactly what I was looking for.
Thanks a lot for your help

Glad to have helped a fellow scripter!:slight_smile: