Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Replacing carriage return in text variable

886 views
Skip to first unread message

Mosby

unread,
Dec 6, 2008, 9:35:10 AM12/6/08
to
I don't know why, but the following script will not work. I have a
script that reads a text file and puts the contents into a variable.
This text has carriage returns at the end of each line. I need to
remove the carriage returns. The following is a hacked script but I
cannot make it work. Any suggestions? I would even be fine with
using DO SHELL SCRIPT but I know less about Unix that I know about
Applescript.

set t to "a,b,c
d,e,f
g,h,i"
set s to return
set r to "x"
set change_t to replaceText(s, r, t)
display dialog change_t

on replaceText(find, replace, subject)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set subject to text items of subject

set text item delimiters of AppleScript to replace
set subject to "" & subject
set text item delimiters of AppleScript to prevTIDs

return subject
end replaceText

Jolly Roger

unread,
Dec 6, 2008, 12:08:06 PM12/6/08
to
In article
<90fe1978-1561-468b...@n10g2000yqm.googlegroups.com>,
Mosby <dril...@aol.com> wrote:

Feel free to use this search and replace handler I've used for years:

-- begin script
-- usage example:
set theFile to choose file with prompt "Choose any text file:"
set theString to read file theFile
return SearchReplace(theString, ".", "<PERIOD>")

on SearchReplace(sourceStr, searchString, replaceString)
-- replace <searchString> with <replaceString> in <sourceStr>
set searchStr to (searchString as text)
set replaceStr to (replaceString as text)
set sourceStr to (sourceStr as text)
set saveDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to (searchString)
set theList to (every text item of sourceStr)
set AppleScript's text item delimiters to (replaceString)
set theString to theList as string
set AppleScript's text item delimiters to saveDelims
return theString
end SearchReplace
-- end script

--
Send responses to the relevant news group rather than email to me.
E-mail sent to this address may be devoured by my very hungry SPAM
filter. Due to Google's refusal to prevent spammers from posting
messages through their servers, I often ignore posts from Google
Groups. Use a real news client if you want me to see your posts.

JR

Mosby

unread,
Dec 6, 2008, 3:15:24 PM12/6/08
to
I still have the same problem. I want to replace carriage returns
with "" (null) but I cannot get it to do the replacement. It works
fine the way you have it set up, but when I change the search string
to "\r" and the replacement string to "" it does not perform the
replacement. However, I found that it did work using and other
replacement string besides "" (null). Why would that be? Thanks for
the help, BTW.
Message has been deleted

Jolly Roger

unread,
Dec 6, 2008, 3:39:02 PM12/6/08
to
In article <061220081429101031%dave@N_O_T_T_H_I_Sbalderstone.ca>,
Dave Balderstone <dave@N_O_T_T_H_I_Sbalderstone.ca> wrote:

> In article
> <dcdef6e4-468e-4523...@x14g2000yqk.googlegroups.com>,

> Don't use "\r". Use "return" without the quotes.

Exactly.

The AppleScript keyword "return" (without quotes) is the same as:

ascii character 13

If you wanted or needed to, you could set a variable to it like this:

set cr to ascii character 13
-or-
set cr to return

Message has been deleted

Jolly Roger

unread,
Dec 6, 2008, 5:04:17 PM12/6/08
to
In article <061220081539294623%dave@N_O_T_T_H_I_Sbalderstone.ca>,
Dave Balderstone <dave@N_O_T_T_H_I_Sbalderstone.ca> wrote:

> In article <jollyroger-5204E...@news.individual.net>,


> Jolly Roger <jolly...@pobox.com> wrote:
>
> > In article <061220081429101031%dave@N_O_T_T_H_I_Sbalderstone.ca>,
> > Dave Balderstone <dave@N_O_T_T_H_I_Sbalderstone.ca> wrote:
> >
> > > In article
> > > <dcdef6e4-468e-4523...@x14g2000yqk.googlegroups.com>,
> > > Mosby <dril...@aol.com> wrote:
> > >
> > > > I still have the same problem. I want to replace carriage returns
> > > > with "" (null) but I cannot get it to do the replacement. It works
> > > > fine the way you have it set up, but when I change the search string
> > > > to "\r" and the replacement string to "" it does not perform the
> > > > replacement. However, I found that it did work using and other
> > > > replacement string besides "" (null). Why would that be? Thanks for
> > > > the help, BTW.
> > >
> > > Don't use "\r". Use "return" without the quotes.
> >
> > Exactly.
> >
> > The AppleScript keyword "return" (without quotes) is the same as:
> >
> > ascii character 13
> >
> > If you wanted or needed to, you could set a variable to it like this:
> >
> > set cr to ascii character 13
> > -or-
> > set cr to return
>

> You can actually set "return" to anything you want.
>
> set return to "anything you want"
> return
>
> -->"anything you want"

Wow neat. Never thought to try that. : )

Message has been deleted
Message has been deleted
Message has been deleted

Mosby

unread,
Dec 6, 2008, 6:26:23 PM12/6/08
to
First of all thanks for all the help. But I still have not got this
to work. I made the changes to the script and still no luck. The
problem seems to be the "" (null) in the replacement string. When I
place anything between the quotes it will replace the carriage
returns.

Here is a sample data file:

"B0""Williams""George""W""477044773""S""9136544031""George W.
Williams""666 Dover Lane""Gunnison ""TX""67020""301""14772"""""
"C0""6910A""Anderson""James""R""M""0839269""N""""N"
"D0""01""6910A""F""CB900""Red Cross/ Oran
""NOCD""467689234""0777771""900/400""Anderson""James""R""77771969""GW""Y""N""01""""X"
"D1""01""6910A""726 E. Acheson""S""Corpus""TX""99999"""""
"E5""6910A"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"E6""6910A""""455555773""Wilson""Jack""W""301"""""""""""""
"F5""01""6910A""""29""""188338""09990""8100"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"X0""6910A"""""""
"Y0"


Maybe I'm going about this all wrong. The data is enclosed in quotes
with a return at the end of the line. I don't need the return or the
quotes. I would like to have a long list of items. I am going to use
that list to verify the data in the file. When I generate my list of
items I end up with an item that has a (quote,return,quote,C0). This
is the end of the first line. I will try to show the item below....

"
"C0

If I could get rid of the returns I would be done. Is there another
method to generate my list of data?


Message has been deleted
Message has been deleted
0 new messages