Need help with a find and replace script

i’m trying to write a find and replace script and need some help getting it started.

this is an example of the naming convention of the filename i need to modify:
XXXXXX.020.M.XXXX

how can i get the script to delete the “.020.M” leaving only the "X"s and a dot between the first six Xs and the last four Xs.

also, keep in mind that the “.020.M” is never the same, example:
XXXXXX.020.M.XXXX, XXXXXX.021.F.XXXX, XXXXXX.022.R.XXXX, etc.

i think the script would need to somehow count the characters, is that possible?

thanx, jmarsh

This might work as long as the character count is consistent in the file names.

set current_name to "XXXXXX.020.M.XXXX"
set new_name to (text 1 thru 7 of current_name) & (text -4 thru end of current_name)

– Rob

thanx rob,
the code works as far as replacing the name.

i forgot to tell you that the file will have a .pdf at the end of the name, example:
XXXXXX.020.M.XXXX.pdf

i want the same as before but to leave the .pdf at the end of the file.
any suggestions?

thanx again, jmarsh

Try this:

set current_name to "XXXXXX.020.M.XXXX.pdf"
set new_name to (text 1 thru 7 of current_name) & (text -8 thru end of current_name)

– Rob

OK, this is what i got so far.

tell application “Finder”
activate
set name of selection to (text 1 thru 7 of item_name) & (text -4 thru end of item_name)
end tell

i want this script to search a folder and replace every item_name it the folder with the new item_name (XXXXXX.XXXX)

is this possible?

Maybe this is close to what you need (untested). I advise you to duplicate some files to test the script.

set target_fol to (choose folder with prompt "Choose a folder whose files need to be renamed.")

tell application "Finder"
	set items_ to files of target_fol
	repeat with item_ in items_
		try
			set new_name_ to name of item_
			set name of item_ to ((text 1 thru 7 of name_) & (text -8 thru end of name_))
		on error e
			display dialog e buttons {"OK"} default button 1
		end try
	end repeat
end tell

– Rob

everything seems to work right except i get an error message saying “Variable name_ is not defined”

My mistake. This should make the error go away.

set target_fol to (choose folder with prompt "Choose a folder whose files need to be renamed.")

tell application "Finder"
	set items_ to files of target_fol
	repeat with item_ in items_
		try
			set name_ to name of item_
			set name of item_ to ((text 1 thru 7 of name_) & (text -8 thru end of name_))
		on error e
			display dialog e buttons {"OK"} default button 1
		end try
	end repeat
end tell

– Rob

worked great!!!
thanx for all the help.

if you want to take it all the way for me,
how can i make this script so i can attatch it to a folder and it run automatically on whatever file is added to the folder.

thanx again for the quick response and solution.
excellent, jmarsh

This will work only on files (not folders) that arrive in the attached folder. If you run into cases where it fails, come back and we’ll try to fix it.

on adding folder items to this_folder after receiving added_items
	repeat with item_ in added_items
		if (folder of (info for item_)) is false then
			tell application "Finder"
				try
					set name_ to name of item_
					set name of item_ to ((text 1 thru 7 of name_) & (text -8 thru end of name_))
				end try
			end tell
		end if
	end repeat
end adding folder items to

– Rob