• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Find and Replace Text
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Find and Replace Text


  • Subject: Re: Find and Replace Text
  • From: "Marc K. Myers" <email@hidden>
  • Date: Wed, 24 Apr 2002 19:37:26 -0400
  • Organization: [very little]

> Date: Wed, 24 Apr 2002 12:44:06 -0400
> From: "Eric Phillips" <email@hidden>
> To: <email@hidden>
> Subject: Find and Replace Text
>
> I have almost no experience in AppleScript doing string manipulations.
> I have done very rudimentary string manipulations when changing some
> file paths. Any help in how to wade through a text file to do find and
> replace would be greatly appreciated.
>
> I have some text that I used OCR to get an electronic version. I want
> to get it into excel but it needs modification to get it into the
> correct columns. The text itself can have commas between words, I would
> like to keep that format intact. What I want to change is when there is
> a 'Word,SpaceNumber'. I would like the number in the next cell so I
> would like to change the ',Space' to a 'Tab'. If I import the file as
> comma delimited then where the words are separated by commas gets all
> messed up. My knowledge of using AppleScript to do all that string
> manipulation is quite limited. I was thinking about using something like
> MS Word and script it to do find and replace. If I do it this way is
> there a way of searching for ', space number' no matter what the number
> is and have it replace with 'tab number' or do I need to have nine find
> and replace statements to handle each different integer, nothing will
> have a leading 0? I would prefer to do this in AppleScript so that I
> don't hav!
> e to open another application and I can learn more AppleScript. If
> someone has any suggestions on how to get started doing this in
> AppleScript I would appreciate the help.
>
> Eric

This should do the trick:

property srchStrings : {"1", "2", "3", "4", "5", "6", "7", "8", "9"}

set theFile to (choose file with prompt "Pick a text file:" of type {"TEXT"})
set fileID to (open for access theFile)
try
set theText to read fileID
close access fileID
on error m number n
try
close access fileID
end try
error m number n
end try
repeat with aString in srchStrings
set theText to textReplace(theText, (" " & aString), tab)
end repeat

on textReplace(theText, srchStrng, replStrng)
tell (a reference to AppleScript's text item delimiters)
set {od, contents} to {contents, {srchStrng}}
try
set {textList, contents} to {(text items of theText), {replStrng}}
set {newText, contents} to {(textList as text), od}
return item 1 of result
on error errMsg number errNbr
set contents to od
error errMsg number errNbr
end try
end tell
end textReplace

Marc K. Myers <email@hidden>
http://AppleScriptsToGo.com
4020 W.220th St.
Fairview Park, OH 44126
(440) 331-1074

[4/24/02 7:36:55 PM]
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: Large Quark Doc Comparisons
  • Next by Date: dd: Palm Note Pad (Desktop 4.0)
  • Previous by thread: Re: Find and Replace Text
  • Next by thread: Re: Find and Replace Text
  • Index(es):
    • Date
    • Thread