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