Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fhill2

macrumors newbie
Original poster
May 9, 2012
19
0
Is it possible to remove the character "," out of all items in a list of strings? I have tried text item delimiters, and I can't seem to get the looping correct.
Thanks!
 
These words don't resemble code in any language I know. We don't know what language you're using, what you've tried, what the input is, what the expected output is, etc. Give us more info and we'll try hard to help you.

-Lee
 
Using AppleScript's Text Item Delimiters : http://macscripter.net/viewtopic.php?id=24725

If you want an item from your list use something like :

Code:
item 5 of myList

Example :

Code:
set the_String_List to {"First String", "Second String", "Third String", "Fourth String", "Fifth String", "Sixth String"}
repeat with i from 1 to number of items in the_String_List
	set this_item to item i of the_String_List
	display dialog "Item : " & i & return & "String : " & this_item with title "List Items" giving up after 3
end repeat
 
The Terminal command "tr -d ," will remove commas from piped input. So cat file | tr -d , > newfile will remove all commas from file and output it to newfile. If your strings are separated by commas, you'd need to tokenize the list first and use something with a little more oomph than tr. Not sure if this is what you're after however.
 
My apologies for not being more specific in my first post!

I am building an integrated iTunes browser on the ipad using Lemur.
I am retrieving information from a [shell object] in Max using an osascript command (to run an applescript at a location on the computer).
When the device first opens. This information will be stored in a [coll object] for data management later (alphabetically sorting, loading the track, playlist display)


With the script:

tell application "Tunes"
name of every track in (get some playlist whose special kind is Music)
end tell

Creates a list ouputted to max in this format:
song1
song2
song3
song4
etc
This is to my advantage because it can be easily iterated without using any text/string/list processing in Max because a simple Max message will iterate with items separated by a comma.

However, if a song name contained a comma already, it will mess up my iteration. The output will look like:
song1
song2
so
ng3
song4
son,
g5,
etc

I wouldn't have the remove the commas if what appeared in Applescripts was sent correctly to Max, however it seems Max removes the 'quotation marks' of each item in the list.
A list of strings appear in Script Editor as:
"song1", "song2", "song3"
A list of strings appear in Max (directly after stdout of [shell] object) as:
song1, song2, song3,
So in Applescripts, I need the code which will iterate through a list, and remove the comma. This is my attempt on the scripting the process per item:

set mystring to "string with a , in it"
set TID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set pieces to text items of mystring
set mystringleft to text items 1 thru text item (whose name is ",")
set mystringright to text items (whose name is ",") thru count of text items
set result to mystringright & mystringleft

and obviously, doesn't compile :(

With regards to the Terminal command tr -d, I don't want to run 2 different processes to get my result. Sending the result of an Applescript to a [shell] object seems less efficient, unless it was my last resort or the information I desire could be retrieved from the iTunes .xml file in shell.
Thanksyou for the replies!
 
Text item delimiters are used when breaking strings apart into text items, and also when putting text items back together to make a string, so your snippet would be something like:

Code:
set mystring to "string with a , in it"
set TID to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set pieces to text items of mystring -- break string apart at commas
set AppleScript's text item delimiters to "" -- or whatever replaces the comma
set mystring to pieces as text -- put string back together using whatever
set AppleScript's text item delimiters to TID

return mystring
 
Code:
set mystring to "string with a , in it"
set mywords to words of mystring
--> {"string", "with", "a", "in", "it"}
mywords as string --> "stringwithainit"
-- That is squished together because no text "" is put between the items.
-- but if we set the AppleScript's text item delimiters to space...
set AppleScript's text item delimiters to space
set MW to mywords as string
set AppleScript's text item delimiters to "" -- ALWAYS SET THEM BACK
MW --> "string with a in it"
 
Is there a way to add a backslash before each comma and parentheses, to each string, in the list (get name of every track in playlist 1).
This would mean it will be escaped in Max and treated as a non-metacharacter.
I have tried 'quoted form of' but this only deals with quotation marks.
This script removes all metacharacters, so the strings sent to Max have no parenthese or commas. (which I would like included in the list of my track names)

tell application "iTunes" to set foo to (get name of every track in playlist 1)
repeat with i from 1 to number of items in foo
set the text item delimiters of AppleScript to space
set item i of foo to words of (item i of foo) as string
set the text item delimiters of AppleScript to space
end repeat
return foo

Thanks!
 
OK, to escape characters in the individual items of your name list, you can do something like:

Code:
tell application "iTunes" to set foo to (get name of tracks of playlist 1)
repeat with bar from 1 to (count foo)
	set item bar of foo to escapeString_(item bar of foo)
end repeat
return foo


on escapeString_(someText)
	set escapeCharacter to "\\" -- escape the escape character
	repeat with anItem in {",", "(", ")"} -- the items to escape
		if anItem is in someText then -- escape it/them
			set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, anItem}
			set {pieces, AppleScript's text item delimiters} to {text items of someText, escapeCharacter & anItem}
			set {someText, AppleScript's text item delimiters} to {pieces as text, tempTID}
		end if
	end repeat
	return someText
end escapeString_

You might also be able to just quote each name for your shell script, for example

Code:
tell application "iTunes" to set foo to (get name of tracks of playlist 1)
repeat with bar from 1 to (count foo)
	set item bar of foo to quoted form of (item bar of foo)
end repeat
return foo
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.