i have previously done this but deleted it now forgotten

I have some filnemas that have the following ending
-tif

I have got a trim text method from apple that looks trims the text. But when i try and use it it wont work.

I has in the past but dont know why it doesn’t now.

Here is the code


property theFolder : ""
--choose the folder through prompt
set theFolder to (choose folder with prompt "Choose the folder to look for invalid file endings")



set folderList to list folder theFolder without invisibles
set folderCount to (count folderList)

repeat with i from 1 to folderCount
	set theFileName to item i of folderList as string
	
	if theFileName ends with "-TIF" then
		
		set newFileName to my trim_line(theFileName, "-TIF", 1)
		set newFileName to newFileName & ".tif"
		set theFile to ((theFolder as string) & theFileName) as file specification
		tell application "Finder"
			set name of theFile to newFileName
		end tell
end

on trim_line(this_text, trim_chars, trim_indicator)
	-- 0 = beginning, 1 = end, 2 = both
	set x to the length of the trim_chars
	-- TRIM BEGINNING
	if the trim_indicator is in {0, 2} then
		repeat while this_text begins with the trim_chars
			try
				set this_text to characters (x + 1) thru -1 of this_text as string
			on error
				-- the text contains nothing but the trim characters
				return ""
			end try
		end repeat
	end if
	-- TRIM ENDING
	if the trim_indicator is in {1, 2} then
		repeat while this_text ends with the trim_chars
			try
				set this_text to characters 1 thru -(x + 1) of this_text as string
			on error
				-- the text contains nothing but the trim characters
				return ""
			end try
		end repeat
	end if
	return this_text
end trim_line



It should make this filename
hello-tif
into
hello.tif

Any ideas?

Also this works, and uses exactly the same code just different if:


if theFileName ends with "-AdB.tif" then
		
		set newFileName to trim_line(theFileName, "-AdB.tif", 1)
		set newFileName to newFileName & "-ADB.tif"
		set theFile to ((theFolder as string) & theFileName) as file specification
		tell application "Finder"
			set name of theFile to newFileName
		end tell
	end if

Does this work?

property theFolder : ""
--choose the folder through prompt 
set theFolder to (choose folder with prompt "Choose the folder to look for invalid file endings")
tell application "Finder"
	set files_ to files of theFolder whose name ends with "-tif"
	repeat with file_ in files_
		set oldName to name of file_
		try
			set name of file_ to (((characters 1 thru -5 of oldName) & ".tif") as text)
		on error e
			display dialog e
		end try
	end repeat
end tell

If it works, it could probably stand some refinement (error checking and stuff like that).

– Rob

seems to.

Also i have just realised that the files that i was checkong ended in -tif.tif because of hide the filenames or something

thanks.

I have stayed with old version as i have some error and progress checking

Cheers for the help!

I thought that the old version didn’t work. :shock:

– Rob

it didnt initially but i found the error and that was that the filenames which ended in -tif had their extension hidden. When i sorted that it worked.

Thats all really.

Sorry for the confusion