small help needed with names

This script works but if the filename contains a space, then it fails. I understand, it has something to do with how bash accepts names (or file paths). Can someone fix it ?


tell application "Finder"
	set F to selection
	set r to F as string
	set r to POSIX path of r
	set namer to text 1 thru -5 of r
	set newName to namer & "s.mkv"
	set originalName to namer & ".mkv"
	set subtitleName to namer & ".srt"
	do shell script "/usr/local/bin/mkvmerge " & "-o " & newName & " " & originalName & " " & subtitleName
end tell

Hi,

quoted form of is your friend. The shell treats space characters as parameter delimiters


tell application "Finder" to set r to POSIX path of (item 1 of (get selection) as text)
set namer to text 1 thru -5 of r
set newName to quoted form of (namer & "s.mkv")
set originalName to quoted form of (namer & ".mkv")
set subtitleName to quoted form of (namer & ".srt")
do shell script "/usr/local/bin/mkvmerge -o " & newName & " " & originalName & " " & subtitleName

Notes: item 1 of selection is better than selection as string. If more than one item is selected, the string coercion will fail.
And always avoid lines in application tell blocks which actually don’t target the application like shell scripts :wink:

many thanks for the help and advice