Remove spaces unless after a comma.

Hi,

I’m attempting to create an applescript that will remove spaces and other special characters, except if that space comes after a comma. I’m attempting to take a list of keywords and make it a list of hashtags.

Some examples.

My name is Bob becomes MynameisBob
My-name-is-Bob becomes MynameisBob
My_name_is_Bob becomes MynameisBob
My@name$is*Bob becomes MynameisBob

But
My, name, is, Bob, remains My, name, is, Bob,
My, name is, Bob, becomes My, nameis, Bob,
My name is, Bob, becomes Mynameis, Bob,

What I have so far…



set thefile to "street, New York, Winston-Salem, USA" as Unicode text
findAndReplace(", ", " #", thefile)

on findAndReplace(tofind, toreplace, TheString)
	set ditd to text item delimiters
	set text item delimiters to tofind
	set textItems to text items of TheString
	set text item delimiters to toreplace
	if (class of TheString is string) then
		set res to textItems as string
	else -- if (class of TheString is Unicode text) then
		set res to textItems as Unicode text
	end if
	set text item delimiters to ditd
	return "#" & res
end findAndReplace

The result is: “#street #New York #Winston-Salem #USA

I need New York to read NewYork
I need Winston-Salem to read WinstonSalem

Ideas? Suggestions?

Thanks.

Model: MacBook
Browser: Safari 537.36
Operating System: Mac OS X (10.8)

Hi,

this is probably not the fastest solution (I guess someone will provide an sed one-liner)
It splits the string into components separated by comma-space, removes all non-alphanumeric characters from each component, adds the # character and joins the components with separating space characters


set theString to "street, New York, Winston-Salem, USA"
set {TID, text item delimiters} to {text item delimiters, ", "}
set components to text items of theString
repeat with i from 1 to (count components)
	set aComponent to item i of components
	set item i of components to "#" & (do shell script "echo " & quoted form of aComponent & " | sed  's/[^[:alnum:]]//g'")
end repeat
set text item delimiters to space
set theString to components as text
set text item delimiters to TID
theString --> #street #NewYork #WinstonSalem #USA

This one may help :

set thefile to "street, New York, Winston-Salem, USA" as Unicode text
set thefile to findAndReplace(", ", ",#", thefile)
set thefile to findAndReplace({" ", "-"}, "", thefile) # EDITED
set thefile to "#" & findAndReplace(",#", " #", thefile)

on findAndReplace(tofind, toreplace, TheString)
	set ditd to text item delimiters
	set text item delimiters to tofind
	set textItems to text items of TheString
	set text item delimiters to toreplace
	set res to textItems as text
	set text item delimiters to ditd
	return res
end findAndReplace

Yvan KOENIG (VALLAURIS, France) dimanche 20 avril 2014 18:01:58

Odd behaviour

When I click on [Open this scriplet in your editor:] I no longer get the script but Safari switch to Top Site.
What’s playing the fool ?

OK, both of those work when run as Applescript. (On the second one, I had to add another line to get rid of the dash).

From what I’ve heard, the easiest way to convert to a service is using Run Applescript within Automator. But with both of these, when I try to convert into a service, they no longer work. Any ideas why that would be? Or is it possible to convert to a service without using Automator?

Oops, the script is now edited above thanks to the ability to use a list of delimiters so there is no need for an added line.

Go to :
http://wafflesoftware.net/thisservice/
and download “ThisService 3” which allow us to encapsulate scripts in services.

On my side, my choice is to give scripts a shortcut thanks to FastScripts.
http://www.red-sweater.com/fastscripts/
Free as long as we don’t use more than 10 shortcuts.

My problem with Top Sites is solved. The culprit was a Safari’s extension.

Yvan KOENIG (VALLAURIS, France) lundi 21 avril 2014 18:11:13

Still problems… :frowning:

Both of these examples work… as long as I hardcode the data into the script. As soon as I attempt to pass data to it, either through a dialog (applescript) or via selected text (automator), it stops working. Sometimes it won’t compile; other times it works but not correctly.

I guess it’s something simple, but I’ve got no idea what that might be.

I guess that you wrongly treated the record returned by display dialog.

This version behaves flawlessly.

set thefile to display dialog "Type the string to treat" default answer "street, New York, Winston-Salem, USA"
set thefile to text returned of thefile

set thefile to findAndReplace(", ", ",#", thefile)
set thefile to findAndReplace({" ", "-"}, "", thefile)
set thefile to "#" & findAndReplace(",#", " #", thefile)

on findAndReplace(tofind, toreplace, TheString)
	set ditd to text item delimiters
	set text item delimiters to tofind
	set textItems to text items of TheString
	set text item delimiters to toreplace
	set res to textItems as text
	set text item delimiters to ditd
	return res
end findAndReplace

Yvan KOENIG (VALLAURIS, France) mardi 22 avril 2014 18:12:50

. and be aware that an Automator action could return a list of strings instead of a single string

Thanks Stefan & Yvan, you both were a great help. I have an Applescript working, so I’m skipping Automator this time.