Need Additional Changes to the Script

Hi Guys,

I got this script from this forum which is useful for me. But I am unable to rename the file in the display dialog window.

property max_char : 10

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

I need little changes in this script.

  1. Script to run in folder and sub folders.
  2. To run for the files of particular extension eg: “txt” (for only Word Files)
  3. To rename the file in the display dialog window itself.

If anyone helps it saves me a day.

Thanks in advance

The posted script can’t achieve it’s goal because displayed name can’t be edited by a script.

The Finder’s dictionary states :

displayed name (text, [b]r/o[/b]) : the user-visible name of the item

r/o is the standard abbreviation for read-only.

You may try this one :

property max_char : 10

on open theItems
	repeat with thisItem in theItems
		tell application "Finder"
			set itemName to name of thisItem
			set extName to name extension of thisItem
			set charCounts to count itemName
			if extName is "" then
				set extCounts to 0
			else
				set extCounts to (count extName) + 1
			end if
			if charCounts - extCounts > max_char then
				repeat
					set maybe to text returned of (display dialog "This name is too long. Please shorten but don't change the name extension !" default answer itemName)
					if (extCounts = 0) or maybe ends with ("." & extName) then
						if not (((count maybe) - extCounts) > max_char) then exit repeat
					end if
					beep
				end repeat
				set name of thisItem to maybe
			end if
		end tell
	end repeat
end open

Yvan KOENIG (VALLAURIS, France) lundi 17 novembre 2014 16:21:31

the script is a droplet, it processes only items being dropped onto its icon.
Try this, you can skip files and provide a file extension to filter.
The file extension is removed from the file name and later added when the file is going to be renamed.


property max_char : 10
property fileExtension : ".txt" -- with the dot

on open theItems
	repeat with thisItem in theItems
		set willRename to false
		tell application "Finder" to set {name:itemName, name extension:extName} to thisItem
		set extensionLength to (count extName)
		if extensionLength > 0 then
			set extensionLength to extensionLength + 1
			set extName to "." & extName
		end if
		-- strip the extension from the file name
		set itemName to (text 1 thru -(extensionLength + 1) of itemName)
		repeat
			if fileExtension is extName and (count itemName) > max_char then
				set willRename to true
				set {text returned:newItemName, button returned:buttonReturned} to display dialog "This name is too long. Please shorten." default answer itemName buttons {"Cancel", "Skip", "Rename"} default button 3
				if buttonReturned is "Skip" then exit repeat
				set itemName to newItemName
			else
				if willRename then
					tell application "Finder" to set name of thisItem to itemName & extName
				end if
				exit repeat
			end if
		end repeat
	end repeat
end open

Hi,

One thing else about ‘displayed name’ is that the extension is returned only if the user has it set in Preferences to display extensions.

gl,
kel

Hi StefanK,

Thanks for all.

Your code works fine. But I need additional things on your script.

Count of file name should include extensions i.e., “.txt”

Should need for 2 or 3 extensions. i.e., “.txt” “.doc” etc.,

Droplet should work for files or Folders only. And not for sub folders.

Thanks

This should work for all extensions provided in property fileExtensions


property max_char : 10
property fileExtensions : {".txt", ".doc"} -- with the dot

on open theItems
	repeat with thisItem in theItems
		set willRename to false
		tell application "Finder" to set {name:itemName, name extension:extName} to thisItem
		set extensionLength to (count extName)
		if extensionLength > 0 then
			set extensionLength to extensionLength + 1
			set extName to "." & extName
		end if
		-- strip the extension from the file name
		set itemName to (text 1 thru -(extensionLength + 1) of itemName)
		repeat
			if fileExtensions contains extName and (count itemName) > max_char then
				set willRename to true
				set {text returned:newItemName, button returned:buttonReturned} to display dialog "This name is too long. Please shorten." default answer itemName buttons {"Cancel", "Skip", "Rename"} default button 3
				if buttonReturned is "Skip" then exit repeat
				set itemName to newItemName
			else
				if willRename then
					tell application "Finder" to set name of thisItem to itemName & extName
				end if
				exit repeat
			end if
		end repeat
	end repeat
end open

Hi Stefank,

As I mentioned in my previous post.

Count of file name should include extensions i.e., “.txt” (10). And the display dialog to tell total count. This will help me to rename to our standard.

Thanks

Sorry, I don’t understand.

In your first post you’re comparing max_char with number of chars in file name minus number of chars in file extension.
So it might be a good idea to leave out the extension to avoid accidentally being removed or renamed.
Later on there is no check if the extension is still there

Hi StefanK,

Once again thanks for you.

I got it.