Process spaces in file correctly

Hi Everyone,

I have an applescript that is a drag and drop icon with Quicktime files. But it only works when there are no spaces in the name of the files. I would like it to convert spaces or even just remove them, but I can’t get it working properly. I thought the oldDelims flag had something to do with this, but I confess I am very new to AS still. Here is the part of my code where I think it should process/cut the spaces:

on getFileName(thefile)
	set {oldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {":"}}
	set mytextfilename to last text item of (thefile as text)
	set AppleScript's text item delimiters to oldDelims
	return mytextfilename
end getFileName

I would appreciate any help you can give me.


it can’t work, oldDelims is only a temporary variable o save the value.
To remove any space character in a string, use something like this

on getFileName(thefile)
	set mytextfilename to name of (info for thefile as alias)
	set {oldDelims, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
	set mytextfilename to text items of mytextfilename
	set AppleScript's text item delimiters to ""
	set mytextfilename to mytextfilename as Unicode text
	set AppleScript's text item delimiters to oldDelims
	return mytextfilename
end getFileName

But I guess, your script is using a shell script, so it would be easier
to escape the characters than removing it

Thank you very much for helping me on this one.I tried this, but it doesn’t solve the problem. But then I realized perhaps it is on the input file itself that is breaking the script if there are spaces? Is there a way I can remove spaces first and foremost. I think it would have to be in this part of my script:

on open these_items
	-- make something that checks for ffmpegx
	repeat with i from 1 to the count of these_items
		set this_item to (item i of these_items)
		set the item_info to info for this_item
		if folder of the item_info is true then
		else if (alias of the item_info is false) and ¬
			((the file type of the item_info is in the type_list) or ¬
				the name extension of the item_info is in the extension_list) then
		end if
	end repeat
end open

try this, I added quoted form and a separate composition of the destination file path to escape the space characters

-- this sub-routine processes files
on process_item(this_item)
	-- NOTE that the variable this_item is a file reference in alias format
	set {name:Nm, name extension:Ex} to info for this_item
	if Ex is missing value then set Ex to ""
	if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
	set destinationFile to quoted form of (POSIX path of (path to movies folder) & Nm & ".mp4")
	with timeout of 10800 seconds -- three hour per movie time limit
		do shell script "ffmpeg -i " & quoted form of POSIX path of this_item & " -vcodec mpeg4 -b 2000 -s 640x480 -acodec aac -ab 448 -ar 48000 -f mp4 " & destinationFile
	end timeout
end process_item

Wow, this is awesome Stefan! I have been trying to muddle my way through my basics for about a week on this. You not only resolved that issue but also the whole process is more smooth now in how it deals with the extension. I cannot thank you enough!

you’re welcome :slight_smile: