Escaping single quote in file name/path


Thanks to KniazidisR, I’ve been able to write a nifty little script to batch-manage a bunch of EyeTV files before delivering them to Handbrake for encoding.

Being your run-of-the-mill eager-beaver, I’ve added to the process the treatment via a Python script of the .txt files extracted from the EyeTV package. Everything works fine except when the name of the text file contains a single quote when all hell breaks loose --the do shell script pass the name of the file as an argument, so… yeah.

After looking right and left, I’ve found what I call the “on searchReplace” method to deal with this problem but I can"t seems to make it work, even for just one file as follows :

on searchReplace(thisText, searchTerm, replacement)
	set AppleScript's text item delimiters to searchTerm
	set thisText to thisText's text items
	set AppleScript's text item delimiters to replacement
	set thisText to "" & thisText
	set AppleScript's text item delimiters to ""
	return thisText
end searchReplace
tell application "Finder"
	set search_T to "'"
	set rep to "\\'" -- 2 "\" because only one throws an error
	set selected to selection as alias
	set textName to selected as text
	set res to searchReplace(textName, search_T, rep)
end tell

Whenever I try to run this, I’ve got an Finder error on the searchReplace(textName, search_T, rep) part of he last line with code number -1708.

Bonus question, is there a way to tell AppleScript to not stop on an error in a repeat when something goes wrong, to just get to the next item of the list and keep going on the loop ?

OK, thanks to a guy on StackOverflow, I’ve got a way to bypass my problem (see below).

set pythonScriptPath to POSIX path of (path to documents folder) & "Python/"
set descriptionFiles to (every file of current_folder whose name extension is "txt")
repeat with textFile in descriptionFiles
    -- run a Python script that clears the xml tags and reformat the text of the file 
    do shell script "python3" & space & quoted form of pythonScriptPath & space & quoted form of POSIX path of (textFile as text)
end repeat

Effectively, it seems the best way to escape characters is to let the system do it on it’s own… which doesn’t tell me why my searchReplace won’t work.
And doesn’t answer the bonus question…

First answer: set res to my searchReplace(textName, search_T, rep)

Second answer. Try / On error / end try

1- Great, thanks
2- I’ll try that