How do I remove characters from a string?

Hi,

I’m trying to create a handler that goes through a list and removes trailing spaces from each item. My code is:

to removeTrailingSpaces(theList)
	repeat with theItem in theList
		if the length of theItem > 0 then
			if (the character 1 of theItem is " ") then set theItem to characters 2 thru end of theItem as string
			if (the last character of theItem is " ") then set theItem to characters 1 thru ((end of theItem) - 1) as string
		end if
	end repeat
	return theList
end removeTrailingSpaces

My first problem is that doing “characters 2 thru end…” returns a comma-delimited list. So the string " Hello " returns “H,e,l,l,o”.

My other problem is that I don’t know how to put theItem back into theList. Should I just create a new list and populate that instead?

Thanks! :slight_smile:

:-Joe

Model: MacBook
AppleScript: 2.0.1
Browser: Firefox 3.0.9
Operating System: Mac OS X (10.5)

Hi again :slight_smile:

I’ve managed to sort the first problem, so it comes back as a proper string:

to removeTrailingSpaces(theList)
	repeat with theItem in theList
		if the length of theItem > 0 then
			if (the character 1 of theItem is " ") then set theItem to text 2 thru end of theItem as text
			if (the last character of theItem is " ") then set theItem to text 1 thru ((end of theItem) - 1) as text
			log (theItem)
		end if
	end repeat
	return theList
end removeTrailingSpaces

But I still don’t know how to set theItem back into the same list, without creating an entirely new list (which wouldn’t be so bad I guess). Any ideas?

Thanks :smiley:

As far as I know, you’d have to clear the first list to put new stuff back in it, which isn’t possible. This http://macscripter.net/viewtopic.php?id=24525 gives you information to delete items from a list but I don’t think it’s possible to do what you’re trying to do. I’d go with a new list…

Yeah, I went with a new list in the end. There’s nothing wrong with that anyway. :slight_smile:

Thanks!

I guess in theory it would use a bit more RAM but because we’re talking about a list of maybe a 100 words or even 10000, it would still be a few kb more…

Hi

There’s probably a lot better way of doing what your trying here maybe look into text item delimeters,
however this works to an extent, but your gonna get errors depending where the space is and in which word.
So use it as only an example! (of maybe what not to do)
You don’t have to create a new list, it totally depends on what your trying to do. the size of the list whether you need to keep the list in order etc…

There’s some proper “applescript list legends” on this forum if one see’s your post i’m sure they’ll step up.

set theList to {"hello ", "world"}
removeTrailingSpaces(theList)

to removeTrailingSpaces(theList)
	repeat with theItem in theList
		if the length of theItem > 0 then
			if (the character 1 of theItem is " ") then set newItem to text 2 thru end of theItem as text
			if (the last character of theItem is " ") then set newItem to (text 1 thru ((length of theItem) - 1)) of theItem as string
		end if
	end repeat
	set item 1 of theList to newItem as string
	return theList
end removeTrailingSpaces

Hi,
interesting function, is it possible to remove “:” from an alias or a string ?

i’ve try this

on run
	set myfiles to choose file with prompt "Please choose an image from the image sequence:" without showing package contents and invisibles
	open {result}
end run

-- drag & drop operation

on open myfiles
	tell application "Finder" to set theSequence to POSIX path of myfiles
	set parentDirectory to POSIX file (do shell script "dirname " & quoted form of POSIX path of (myfiles)) as alias
	log parentDirectory
	set theList to {parentDirectory}
	removeTrailingSpaces(theList)
	if the length of theItem > 0 then
		if (the character 1 of theItem is ":") then set newItem to text 2 thru end of theItem as text
		if (the last character of theItem is ":") then set newItem to (text 1 thru ((length of theItem) - 1)) of theItem as string
	end if
	
	set item 1 of theList to newItem as string
	return theList
	
	log theList
end open


i’ve remove the “to” function because it crash the script but now i don’t know how to make it work
thanks

This is interesting! Please take a look at my post. I want to remove lines not ending or starting with specific text. Is it possible to make this into what I want to achieve?

https://macscripter.net/viewtopic.php?pid=205476#p205476