Broken Finder Scripts in Mavericks

I use the Finder scripts for renaming and replacing text in filenames extensively, but after a Mavericks install, they’re all broken.

Does anyone have a fix for these basic scripts? I’m currently in the middle of trying to rename a folder with hundreds of files in them, and it’s becoming a drag, to say the least.

Hello.

You are not stating what kind of finder rename script it is, that is broken. Anyways I recently wrote a rename script in post #5 in this thread.

Maybe you can customize that a little? It would of course be helpful if you stated what the file names you want to rename looks like, and what you do want them to look like.

Hi … sorry, getting ahead of myself.

The Finder scripts are the standard Apple Finder scripts (Add to File Names.scpt, Replace Text in Item Names.scpt, and Trim File Names.scpt).

As an example, the script below to prefix or suffix doesn’t work as intended “ it only adds text the end of the filename, but, interestingly, it appends the text from the button at the end (either the text Prefix or Suffix), like this (ThisIsMyFile.txt becomes ThisIsMyFile.txtPrefix or ThisIsMyFile.txtSuffix), which is odd.

I’m currently using Automator to do most of this, but wondered why my Applescript has gone awry after Mavericks.

try
	tell application "Finder" to set the source_folder to (folder of the front window) as alias
on error -- no open folder windows
	set the source_folder to path to desktop folder as alias
end try

set the prefix_or_suffix to ""
repeat
	display dialog "Enter the prefix or suffix to use:" default answer the prefix_or_suffix buttons {"Cancel", "Prefix", "Suffix"}
	copy the result as list to {the prefix_or_suffix, the button_pressed}
	if the prefix_or_suffix is not "" then exit repeat
end repeat
set the item_list to list folder source_folder without invisibles
set source_folder to source_folder as string
repeat with i from 1 to number of items in the item_list
	set this_item to item i of the item_list
	set this_item to (source_folder & this_item) as alias
	set this_info to info for this_item
	set the current_name to the name of this_info
	if folder of this_info is false and ¬
		alias of this_info is false then
		if the button_pressed is "Prefix" then
			set the new_file_name to the (the prefix_or_suffix & the current_name) as string
		else
			set the new_file_name to the (the current_name & the prefix_or_suffix) as string
		end if
		my set_item_name(this_item, the new_file_name)
	end if
end repeat
beep 2

on set_item_name(this_item, new_item_name)
	tell application "Finder"
		--activate
		set the parent_container_path to (the container of this_item) as text
		if not (exists item (the parent_container_path & new_item_name)) then
			try
				set the name of this_item to new_item_name
			on error the error_message number the error_number
				if the error_number is -59 then
					set the error_message to "This name contains improper characters, such as a colon (:)."
				else --the suggested name is too long
					set the error_message to error_message -- "The name is more than 31 characters long."
				end if
				--beep
				tell me to display dialog the error_message default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
				copy the result as list to {new_item_name, button_pressed}
				if the button_pressed is "Skip" then return 0
				my set_item_name(this_item, new_item_name)
			end try
		else --the name already exists
			--beep
			tell me to display dialog "This name is already taken, please rename." default answer new_item_name buttons {"Cancel", "Skip", "OK"} default button 3
			copy the result as list to {new_item_name, button_pressed}
			if the button_pressed is "Skip" then return 0
			my set_item_name(this_item, new_item_name)
		end if
	end tell
end set_item_name

Hello

I doubdt that your problem is specific to Mavericks.

The culprit is this block of instructions :

 if the button_pressed is "Prefix" then
           set the new_file_name to the (the prefix_or_suffix & the current_name) as string
       else
           set the new_file_name to the (the current_name & the prefix_or_suffix) as string
       end if

when you ask it to add the word suffix, it does exactly what you want it to do.

If current_name is trucmuche.txt, new_file_name is, as designed, set to the current_name & the prefix_or_suffix which is trucmuche.txt.suffix

It’s your duty to take care of a possible name extension and edit the code to treat this case.

Yvan KOENIG (VALLAURIS, France) jeudi 7 novembre 2013 10:00:17

I see Yvan. That does make sense, and I can make allowances for adding the end of the entire filename, extension included. That doesn’t explain how they managed to work exactly as described and expected previously (ie, they used the text that I input into the dialog as the prefix/suffix). I’t may not be Mavericks itself that’s specifically to blame, but it’s certainly related to its installation.

McUsrll: the filenames are all UTF-8 plaintext files of the nature ThisIsMyFile.txt. One group was missing an extension, the other was to be converted from .txt to .html, but they were all standard text files. The scripts are the standard Apple-supplied Finder scripts.

Now I’ve used the Automator method, and it does everything the separate AppleScripts do in-one, I’ll stick with that from now on. However, I will keep an eye on the scripts and AppleScript in general, as I’m sure this will affect other scripts that I use.

McUsrII, thank you very much.

I won’t be able to run this just yet as I’m mid-project, but I shall check it out ASAP and get back with the results.

K@

Hello.

I see that you managed to get hold of a copy before I removed it. :slight_smile:

Well, here is if not the real deal, a better one, as it lets you choose to rename the end of the name, or change name extention. Suffix is meant for the end of the name…

I’ve called it name - changer, since it lets you add to the front or end of the name, and only change the last suffix.

-it seems to me that nothing gets put into the name extension part of a filename in finder, but I have taken measures for it to be filled, when display file extension is false. (Or whatever the proper name of that property is.)

I have tested this script when the property extension hidden of a file is false, I haven’t tested it when extension hidden is true.

I’ll do that tomorrow, or late this evening, and come back with any changes. ( and some further “polishing”/decoration).

property scripttitle : "Finder Name Changer"
-- Upgraded to Mavericks 2013.11.8
main()

on main()
	set ctr to 0
	tell application id "MACS"
		try
			set the source_folder to (folder of the front window) as alias
		on error -- no open folder windows
			set the source_folder to path to desktop folder as alias
			return
		end try
		
		
		set the prefix_or_suffix to ""
		
		repeat
			set {button_pressed, prefix_or_suffix} to ¬
				{button returned, text returned} of ¬
				(display dialog ¬
					"Enter the prefix or suffix to use:" default answer the prefix_or_suffix ¬
					buttons {"Cancel", "Prefix", "Suffix"} ¬
					cancel button 1 default button 3 with icon note with title my scripttitle)
			
			if button_pressed is "Suffix" and prefix_or_suffix is not "" then
				set button_pressed to button returned of (display dialog ¬
					"Do you want to change the end of the filename or the name extension?" buttons ¬
					{"Cancel", "Suffix", "Extension"} cancel button 1 default button 3 ¬
					with icon note with title my scripttitle)
			end if
			if the prefix_or_suffix is not "" then exit repeat
		end repeat
		
		script o
			property item_list : {}
		end script
		
		set o's item_list to list folder source_folder without invisibles
		set source_folder to source_folder as string
		
		tell application id "MACS"
			repeat with i from 1 to (length of o's item_list)
				set this_item to item i of o's item_list
				set this_item to (source_folder & this_item) as alias
				
				tell (get properties of this_item)
					set current_name to name of it
					if class of it is not folder and ¬
						class of it is not alias then
						if the button_pressed is "Prefix" then
							set new_file_name to prefix_or_suffix & current_name
						else
							set sl to length of its name extension
							
							if sl > 0 then
								set current_ext to its name extension
								set current_name to text 1 thru (-1 * (sl + 2)) of current_name
							else
								tell me to set sl to (offset of "." in ((reverse of (characters of current_name)) as text))
								if sl > 0 then
									set current_ext to text -(sl - 1) thru -1 of current_name
									set current_name to text 1 thru (-1 * (sl + 1)) of current_name
								end if
							end if
							
							if the button_pressed is "Extension" then
								set the new_file_name to current_name & "." & prefix_or_suffix
							else -- "Suffix"
								if sl > 0 then
									set the new_file_name to current_name & prefix_or_suffix & "." & current_ext
								else
									set new_file_name to current_name & prefix_or_suffix
								end if
							end if
						end if
						try
							local tmpctr
							set tmpctr to (my set_item_name(this_item, new_file_name))
							set ctr to ctr + tmpctr
						on error
							if ctr > 0 then
								local nm
								set nm to name of it
								tell me to display notification "Renamed " & ctr & ¬
									" Files before User Aborted Renaming " & nm with title my scripttitle
							else
								tell me to display notification "User Aborted. " with title my scripttitle
							end if
							error number -128
						end try
						
					end if
				end tell
			end repeat
			display notification "Renamed " & ctr & " files. " with title my scripttitle
			
		end tell
	end tell
	beep 2
	
end main

on set_item_name(this_item, new_item_name)
	local ret_val -- 1 if we renamed the file, 0 if not, "for the record".
	set ret_val to 0
	tell application id "MACS"
		
		set the parent_container_path to (the container of this_item) as text
		if not (exists item (the parent_container_path & new_item_name)) then
			try
				set the name of this_item to new_item_name
				set ret_val to 1
			on error the error_message number the error_number
				if the error_number is -59 then
					set the error_message to "This name contains improper characters, such as a colon (:)."
				else --the suggested name is too long
					set the error_message to error_message -- "The name is more than 31 characters long."
				end if
				beep
				set {button_pressed, new_item_name} ¬
					to {button returned, text returned} of ¬
					(display dialog the error_message default answer new_item_name ¬
						buttons {"Cancel", "Skip", "OK"} cancel button 1 default button 3 ¬
						with icon caution with title my scripttitle)
				
				if the button_pressed is not "Skip" then ¬
					set ret_val to (my set_item_name(this_item, new_item_name))
				
			end try
		else --the name already exists
			beep
			set {button_pressed, new_item_name} ¬
				to {button returned, text returned} of ¬
				(display dialog ¬
					"This name is already taken, please rename." default answer new_item_name ¬
					buttons {"Cancel", "Skip", "OK"} ¬
					cancel button 1 default button 3 with icon caution with title my scripttitle)
			
			if the button_pressed is not "Skip" then ¬
				set ret_val to (my set_item_name(this_item, new_item_name))
		end if
	end tell
	return ret_val
end set_item_name


Hello.

It is now as thoroughly tested, as I have imagination for, at the moment. Which means, it works with both files that has filextensions, that is, a file extension, known to the Finder/launchd system, like “.txt”, and file extensions, that are not taken as file extensions, like “.new”.

I tried to test it with the preference show all extensions set, and unset, without noticing any difference!

The reason that the script broke in the first place, is that there has been assumed that that the button, and the text was returned from the dialogs in a certain order, which now has been turned around.

I haven’t straightened up this, the properties of the returned result of the dialog, should really be pulled out properly, by using the properties, and not just copied into a list, so that the script won’t break again in the future.

I leave it here as an example of how not to do it. :wink:

Edit

I’ll optimize it tomorrow, for speed, and robustness with regrards to properties returned from the dialogs.

Hello.

The script in post #7 Should work with Mavericks now, and hadn’t it been for the usage of notification centre to display the number of files renamed, then it should be working for earlier versions as well.

I have scripted the dialogs the way they ought to be scripted, I have also tried to speed things up a little.
But this script is not fast, on the other hand, it’s a script that does what it should do well, with good error handling.
It is considerably faster than the version I posted yesterday, with just a few optimizations, like using the creator code instead of the filename, for finder, and a script object to keep the list of files in the folder.

Hello.

I have now updated the return of the count of set_item_name, and cleaned up its execution path, with regards to returning within branches. The updated script is in post #7.