loop through words with a /

I am looping through some lines of text and then by each word of the line with:

repeat with theWord in (words of theCurrentLine)
						doADialog("the note:" & theWord)

end repeat

My line of text is: 2/1 3/2 1 2/2 2 5 2/10

I need the word to include the slash and word after as one word, i.e. 2/1 as one word, 2/10 as a word.

Thanks for any help

Todd

Have you tried using quotes. Hence 2/3 would be “2/3”. I’m not sure what you are trying to accomplish.

Model: 20" iMac
AppleScript: 2.3
Browser: Safari 531.21.10
Operating System: Mac OS X (10.6)

I am converting music tab, so quotes will not be in the line. More or less it will come from a file downloaded from internet or something like that.

Todd

Hi,

the problem is, AppleScript treats a slash as a word delimiter.
This could be a workaround


set dummyDelimiter to "yxasqw" -- a character sting which definitely doesn't appear in the text

set t to "2/1 3/2 1 2/2  2 5 2/10"

set {TID, text item delimiters} to {text item delimiters, "/"}
set t to text items of t
set text item delimiters to dummyDelimiter
set t to t as text
set wordList to words of t

repeat with oneWord in wordList
	set text item delimiters to dummyDelimiter
	set w to text items of oneWord
	set text item delimiters to "/"
	set contents of oneWord to w as text
end repeat
set text item delimiters to TID
wordList

My solution to what I think you’re doing:

set varline to characters of "2/1 3/2 1 2/2  2 5 2/10" as list
set varnewline to {}
set varnewword to ""
repeat with varlet in varline
	set varlet to varlet as string
	if varlet is not " " then
		set varnewword to varnewword & varlet
	else
		if varnewword is not "" then--stops a "" item on double spaces
			set varnewline to varnewline & varnewword as list
			set varnewword to ""
		end if
	end if
end repeat
set varnewline to varnewline & varnewword as list
return varnewline

Thank you stefan and richard for the responses. I played a bit with both and am going with stefans because it allowed a few other characters like a tilde and a dash to be included in the “word”. Great help for us struggling scripters! I am amazed at how quick you guys can solved a problem! Sometimes it just takes “new” eyes.

Todd

I just from this decided to make up my own independent replacement handler people might want to use instead of the text item way.

on subreplace(varstring, varold, varnew, varreps)
	set varline to characters of varstring as list
	set varnewline to {}
	set varnewword to ""
	repeat with varlet in varline
		set varlet to varlet as string
		if varlet is not in varold then
			set varnewword to varnewword & varlet
		else
			if varnewword is not "" then
				if varnew is null then
					set varnewline to varnewline & varnewword as list
					set varnewword to ""
				else
					set varnewline to varnewline & varnewword & varnew as list
					set varnewword to ""
				end if
			else
				if varreps is true and varnew is not null then
					set varnewline to varnewline & varnew as list
				end if
			end if
		end if
	end repeat
	set varnewline to varnewline & varnewword as list
	return varnewline
end subreplace

Parameters:
varstring -your string [string]
varold -items to replace (“” does not work) [string or list of strings]
varnew -string to replace with, specify ‘null’ to return no replacement value, specifying “” uses “” as replacement items [string or null]
varreps -whether you want back to back replacement items to be treated individually or not [bool] e.g. subreplace(“2/1 3/2 1 2/2 2 && & 5 2/10”, {" ", “&”}, “hi”, true) returns {“2/1”, “hi”, “3/2”, “hi”, “1”, “hi”, “2/2”, “hi”, “hi”, “hi”, “hi”, “2”, “hi”, “hi”, “hi”, “hi”, “hi”, “hi”, “hi”, “5”, “hi”, “2/10”} but false returns {“2/1”, “hi”, “3/2”, “hi”, “1”, “hi”, “2/2”, “hi”, “2”, “hi”, “5”, “hi”, “2/10”}

Return value: a list of strings, therefore use ‘as string’ for the string, I didn’t want to default this incase someone had a use for the list.

if the “word” delimiter is always one or two space characters, this is sufficient


set theCurrentLine to "2/1 3/2 1 2/2  2 5 2/10"

set {TID, text item delimiters} to {text item delimiters, space}
repeat with theWord in (get text items of theCurrentLine)
	if contents of theWord is not "" then doADialog("the note:" & theWord)
end repeat
set text item delimiters to TID

on doADialog(d)
	display dialog d
end doADialog

my apologies in my previous post I said stefan’s let me use other characters. When in fact i t was richards. Now he improved it ten fold. Thanks again.

Todd

:slight_smile:
Just about to add informative and necessary errors.

PS How can I make {ygyhj:“uygy”} into “{ygyhj:"uygy"}”

That new handler is the stuff! As I am sure you know, the requirements increase as you script more. I just noticed I do want to preserve the spaces so they line up as they did originally, i.e. the 2/1 1 etc will be aligned over the lyrics of the song. Brilliant! Almost like you knew before me what I needed. Thank you so much again.

Todd

It was really more for everyone, the applescript text item way is such a pain and has no flexibility, I suddenly saw the solution here, hopefully everyone starts using it.

New one:

on subreplace(varstring, varold, varnew, varreps)
	try
		if varstring is in {null, missing value} then error
		set varstring to varstring as string
	on error
		error "subreplace: String parameter should not or cannot be coerced to type string."
	end try
	set varnewold to {}
	repeat with varitem in varold
		if varitem is in {null, missing value, ""} then
			if varitem as string is "" then set varitem to "\"\""
			error "subreplace: Removal parameter item (" & varitem & ") should not or cannot be coerced to type string"
		end if
		try
			set varnewold to varnewold & (varitem as list as string)
		on error
			try
				error "o" & varitem & "o"
			on error varerr
				error ("subreplace: Removal parameter item (" & characters 12 thru -25 of varerr as string) & ") should not or cannot be coerced to type string"
			end try
		end try
	end repeat
	set varold to varnewold
	if varnew is not in {null, missing value} then
		try
			set varnew to varnew as string
		on error
			error "subreplace: Replacement parameter should not or cannot be coerced to type string."
		end try
	end if
	try
		set varreps to varreps as boolean
	on error
		error "subreplace: Repeated parameter should not or cannot be coerced to type boolean"
	end try
	set varline to characters of varstring as list
	set varnewline to {}
	set varnewword to ""
	repeat with varlet in varline
		set varlet to varlet as string
		if varlet is not in varold then
			set varnewword to varnewword & varlet
		else
			if varnewword is not "" then
				if varnew is in {null, missing value} then
					set varnewline to varnewline & varnewword as list
					set varnewword to ""
				else
					set varnewline to varnewline & varnewword & varnew as list
					set varnewword to ""
				end if
			else
				if varreps is true and varnew is not in {null, missing value} then
					set varnewline to varnewline & varnew as list
				end if
			end if
		end if
	end repeat
	set varnewline to varnewline & varnewword as list
	return varnewline
end subreplace

Parameters:
varstring -your string [string]
varold -items to replace (“” does not work) [string or list of strings (others will try to be coerced to string)]
varnew -string to replace with, specify null to return no replacement value, specifying “” uses “” as replacement items [string or null/missing value]
varreps -whether you want back to back replacement items to be treated individually or not [boolean (inc. 0 and 1)] e.g. subreplace(“2/1 3/2 1 2/2 2 && & 5 2/10”, {" ", “&”}, “hi”, true) returns {“2/1”, “hi”, “3/2”, “hi”, “1”, “hi”, “2/2”, “hi”, “hi”, “hi”, “hi”, “2”, “hi”, “hi”, “hi”, “hi”, “hi”, “hi”, “hi”, “5”, “hi”, “2/10”} but false returns {“2/1”, “hi”, “3/2”, “hi”, “1”, “hi”, “2/2”, “hi”, “2”, “hi”, “5”, “hi”, “2/10”}

Return value: a list of strings, therefore use ‘as string’ for the string, I didn’t want to default this incase someone had a use for the list.

Very necessary edit, please use this:

subreplace("212211", "11", "", 1)
on subreplace(varstring, varold, varnew, varreps)
	try
		if varstring is in {null, missing value} then error
		set varstring to varstring as string
	on error
		error "subreplace: String parameter should not or cannot be coerced to type string."
	end try
	set varold to varold as list
	set varnewold to {}
	repeat with varitem in varold
		if varitem is in {null, missing value, ""} then
			if varitem as string is "" then set varitem to "\"\""
			error "subreplace: Removal parameter item (" & varitem & ") should not or cannot be coerced to type string"
		end if
		try
			set varnewold to varnewold & (varitem as list as string)
		on error
			try
				error "o" & varitem & "o"
			on error varerr
				error ("subreplace: Removal parameter item (" & characters 12 thru -25 of varerr as string) & ") should not or cannot be coerced to type string"
			end try
		end try
	end repeat
	set varold to varnewold
	if varnew is not in {null, missing value} then
		try
			set varnew to varnew as string
		on error
			error "subreplace: Replacement parameter should not or cannot be coerced to type string."
		end try
	end if
	try
		set varreps to varreps as boolean
	on error
		error "subreplace: Repeated parameter should not or cannot be coerced to type boolean"
	end try
	set varline to characters of varstring as list
	set varnewline to {}
	set varnewword to ""
	repeat with varlet in varline
		set varlet to varlet as string
		if varlet is not in varold then
			set varnewword to varnewword & varlet
		else
			if varnewword is not "" then
				if varnew is in {null, missing value} then
					set varnewline to varnewline & varnewword as list
					set varnewword to ""
				else
					set varnewline to varnewline & varnewword & varnew as list
					set varnewword to ""
				end if
			else
				if varreps is true and varnew is not in {null, missing value} then
					set varnewline to varnewline & varnew as list
				end if
			end if
		end if
	end repeat
	if varnewword is not "" then set varnewline to varnewline & varnewword as list
	return varnewline
end subreplace

Parameters:
varstring -your string [string]
varold -items to replace (“” does not work) [string or list of strings (others will try to be coerced to string)]
varnew -string to replace with, specify null to return no replacement value, specifying “” uses “” as replacement items [string or null/missing value]
varreps -whether you want back to back replacement items to be treated individually or not [boolean (inc. 0 and 1)] e.g. subreplace(“2/1 3/2 1 2/2 2 && & 5 2/10”, {" ", “&”}, “hi”, true) returns {“2/1”, “hi”, “3/2”, “hi”, “1”, “hi”, “2/2”, “hi”, “hi”, “hi”, “hi”, “2”, “hi”, “hi”, “hi”, “hi”, “hi”, “hi”, “hi”, “5”, “hi”, “2/10”} but false returns {“2/1”, “hi”, “3/2”, “hi”, “1”, “hi”, “2/2”, “hi”, “2”, “hi”, “5”, “hi”, “2/10”}

Return value: a list of strings, therefore use ‘as string’ for the string, I didn’t want to default this incase someone had a use for the list.

Requires rb-appscript.