replacing a group of spaces into a comma

Hi all,

I’ve got a rather specific problem.

I have a variable string that looks like this (I’m trying to parse something from terminal:

itemone – itemtwo itemthree itemfour itemfive itemsix itemseven

note: the spaces can be a different length between items, so what I need is an applescript that takes the above variable string and replaces each group of spaces into a comma so the result will look something like this:

itemone,–,itemtwo,itemthree,itemfour,itemfive,itemsix,itemseven,

From there, I can use standard delimiters to grab the items I need.

Many thanks in advance, I’ve been trying to find a solution to this for the last few days!

  • Mel

Hi,

try this


set oldText to "itemone            --      itemtwo           itemthree    itemfour            itemfive     itemsix     itemseven     " --> the spaces are ignored in the applescript block
set {TID, text item delimiters} to {text item delimiters, space}
set oldList to text items of oldText
set newList to {}
repeat with i in oldList
	if contents of i is not "" then set end of newList to contents of i
end repeat
set text item delimiters to ","
set newText to newList as text
set text item delimiters to TID
newText

note: it doesn’t work if one of the items contains a space character

Hello

The OP ask for the replacement of ‘groups of spaces’

I understand that as:
replace groups but don’t replace isolated space characters.

This is why I use:



--\[SCRIPT spaces2comma]
(*
Enregistrer le script en tant que Script, Application ou Progiciel : spaces2comma.xxx
déplacer le fichier créé dans le dossier
<VolumeDeDémarrage>:Users:<votreCompte>:Library:Scripts:Applications:
Il vous faudra peut-être créer le dossier Applications.

Copiez vos données dans le Presse-papiers.

menu Scripts > spaces2comma

Chaque groupe de caractères espace sera remplacé par un caractère virgule.
Le résultat sera disponible dans le presse-papiers.

+++++++++

Save the script as a Script, Application or Application Bundle: spaces2comma.xxx

Move the newly created file into the folder:
<startup Volume>:Users:<yourAccount>:Library:Scripts:Applications:
Maybe you would have to create the folder Applications by yourself.

Copy your block of datas to the Clipboard.

menu Scripts > spaces2comma

Every group of space characters will be replaced by a comma character.
The result will be available in the clipboard.

Yvan KOENIG (Vallauris, FRANCE)
4 février 2009 
*)

--=====

on run
	try
		set theText to (the clipboard as text)
	on error
		if my parleAnglais() then
			error "No valid text data in the Clipboard !"
		else
			error "Pas de données texte dans le presse-papiers !"
		end if
	end try
	set twoSpaces to space & space
	set threeSpaces to twoSpaces & space
	repeat while theText contains threeSpaces
		set theText to my remplace(theText, threeSpaces, twoSpaces)
	end repeat
	set the clipboard to my remplace(theText, twoSpaces, ",")
	
end run
--=====

on remplace(t, d1, d2)
	local l
	set AppleScript's text item delimiters to d1
	set l to text items of t
	set AppleScript's text item delimiters to d2
	set t to l as text
	set AppleScript's text item delimiters to ""
	return t
end remplace

--=====

on parleAnglais()
	local z
	try
		tell application theApp to set z to localized string "Cancel"
	on error
		set z to "Cancel"
	end try
	return (z is not "Annuler")
end parleAnglais

--=====

--[/SCRIPT]

Yvan KOENIG (from FRANCE dimanche 14 juin 2009 19:15:39)

Thanks for the help guys!

  • Mel