Clean text but ignore apostrophe

I have this:


set theText to "\"O' Brien\""

set x to trim(theText)
on trim(theText)
	return (do shell script "echo \"" & theText & "\" | xargs")
end trim


I am not experienced in the Terminal. How can I get this to ignore the apostrophe?

Hi,

What exactly you want get in the result: “O Brien” or “"O Brien"” ?
The most effective (faster) way is using AppleScript’s text item delimiters instead of do shell script:


set theText to "\"O' Brien\""
set AppleScript's text item delimiters to {"'", quote} -- apostrophe and quote
set x to text items of theText
set AppleScript's text item delimiters to ""
set x to x as text
--> "O Brien"

set theText to "\"O' Brien\""
set AppleScript's text item delimiters to {"'"} -- only apostrophe
set x to text items of theText
set AppleScript's text item delimiters to ""
set x to x as text
--> "\"O Brien\""

You can write the code above using handler:

set theText to "\"O' Brien\""
set x to my replaceCharacters:{"'", quote} ofText:theText withCharacter:""

on replaceCharacters:myList ofText:theText withCharacter:myChar
	set AppleScript's text item delimiters to myList
	set theText to text items of theText
	set AppleScript's text item delimiters to myChar
	return theText as text
end replaceCharacters:ofText:withCharacter:

or


set theText to "\"O' Brien\""
set x to my replaceCharacters:{"'"} ofText:theText withCharacter:""

on replaceCharacters:myList ofText:theText withCharacter:myChar
	set AppleScript's text item delimiters to myList
	set theText to text items of theText
	set AppleScript's text item delimiters to myChar
	return theText as text
end replaceCharacters:ofText:withCharacter:

Hi. What constitutes ‘clean’ is a little unclear, but xargs doesn’t look like the right shell command for this use. The below will remove punctuation and condense spaces. If you’re trying to preserve the apostrophe, then strike [:punct:].

set theText to "O'          Brien"
do shell script "echo " & theText's quoted form & " |   tr -s [:punct:] " & space's quoted form

Hi. Again.

The do shell script variant given by Mark Anthony contains, in principle, the correct idea. But it has 2 important omissions: 1) the original text is “"O’ Brien"”, not “O’ Brien”, 2) OP asks to clean only apostrophes, not any punctuation marks.

If the OP still prefers the do shell script version, then it would be correct to write it more precisely:


set theText to "\"O' Brien\""
set x to do shell script "echo " & theText's quoted form & " | tr -s \"'\" " & space's quoted form
--> "\"O Brien\""
set x to run script x --> "O Brien"

or, directly:


set theText to "\"O' Brien\""
set x to run script (do shell script "echo " & theText's quoted form & " | tr -s \"'\" ' '")
--> "O Brien"

Please refrain from being snide on the forum. There isn’t enough specificity in the question to avoid making assumptions as to what outcome is desired or even if what was requested—ignoring vs deleting—was actually intended.

I don’t even know what to say. I respect you enough as an intelligent person, so I’m sorry that my post seemed like a mockery to you. I’ll delete it if you like.

Thanks you for your replies and sorry for not giving a full explanation to the question. Bit stressed out at the time!!

set theText to “"O’ Brien"”

what I need from this is to return a string – O’ Brian

Your answers, thank you, return O Brian but I need the apostrophe included. Hope that more sense.


set theText to "\"O' Brien\""
set x to run script theText

or,


set theText to "\"O' Brien\""
set x to do shell script "echo " & theText

NOTE: the first script is about 3 times faster than second script