use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions


property originalText : "IMG_" -- possible example values
property replacementText : "SUPER-PHOTO_"

tell application "NeoFinder"
	set myList to selected items -- get a list of all selected items
end tell


if myList = missing value then -- that happens if nothing is selected in NeoFinder. But we need a selection!
	display dialog "Bitte selektieren Sie zuerst einige Dateien im NeoFinder!" buttons {"OK"} default button "OK" with icon caution
	return 0
end if

set numItems to count myList

-- tell the user what the script does and allow to cancel
set theResponse to display dialog "Dieses Script kann Teile des Namens der " & numItems & " selektierten Dateien ersetzen." buttons {"OK", "Abbrechen"} default button "Abbrechen" with icon caution
if button returned of theResponse = "Abbrechen" then
	return 0
end if

-- get the name of the first selected item, so the user remembers what this is all about
tell application "NeoFinder"
	tell first item in myList
		set firstItemName to name
	end tell
end tell


-- now ask for the original name text that should be replaced
set theResponse to display dialog "Welcher Teil des Namens soll ersetzt werden?" & return & "(" & firstItemName & ")" default answer "" with icon note buttons {"Abbrechen", "Weiter"} default button "Weiter"
if button returned of theResponse = "Abbrechen" then
	return 0
end if
set originalText to text returned of theResponse
if originalText = "" then
	return 0
end if

-- then ask for the new text to replace that with
set theResponse to display dialog "Welcher neue Text soll '" & originalText & "' in den Namen ersetzen?" default answer "" with icon note buttons {"Abbrechen", "Weiter"} default button "Weiter"
if button returned of theResponse = "Abbrechen" then
	return 0
end if
set replacementText to text returned of theResponse
if replacementText = "" then
	return 0
end if



--  and last, do the actual replacement of the string parts
tell application "NeoFinder"
	repeat with theItem in myList
		tell theItem
			set currentName to name
			set newName to my findAndReplaceInText(currentName, originalText, replacementText)
			if newName ≠ currentName then
				log {newName}
				set name to newName
			end if
		end tell
	end repeat
end tell



-- useful function to replace text in a string
on findAndReplaceInText(theText, theSearchString, theReplacementString)
	set AppleScript's text item delimiters to theSearchString
	set theTextItems to every text item of theText
	set AppleScript's text item delimiters to theReplacementString
	set theText to theTextItems as string
	set AppleScript's text item delimiters to ""
	return theText
end findAndReplaceInText
