• 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: Filter a text string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Filter a text string


  • Subject: Re: Filter a text string
  • From: Christopher Stone <email@hidden>
  • Date: Tue, 11 Oct 2011 15:19:20 -0500

On Oct 11, 2011, at 10:19, Bert Groeneveld wrote:
set myString to "The <letter>a</letter> quick <photo>12</photo> brown fox jumps <photo>150</photo> over the <mode>x</mode> lazy dog."

Is there a smart way to filter the above example text string so that it reads fine: "The quick brown fox jumps over the lazy dog."
______________________________________________________________________

Hey Bert,

I didn't read your post very carefully and wrote the script below, which unfortunately doesn't meet your need.  Since I wrote it I'll post it anyway.  :)

I wasn't able to get 'textutil' to work with the strings command, but perhaps Mark or one of the other Unix-heads can elucidate.

On Oct 11, 2011, at 11:03, Thomas Fischer wrote:
if you have (or are willing to get) the Satimge.osax (it's free) then
{snip}
will do the job.
I don't know how fast their regexp engine is, though.

Very fast.  I prefer it to shelling out to Sed, Perl, Python, or Ruby for that very reason.

I think the Satimage.osax is one of the most useful tools for parsing text in Applescript, and it has numerous other features as well.

Using Perl instead of the Satimage.osax:

------------------------------------------------------------------------------------------------
set str to "The <letter>a</letter> quick <photo>12</photo> brown fox jumps <photo>150</photo> over the <mode>x</mode> lazy dog.
The <letter>a</letter> quick <photo>12</photo> brown fox jumps <photo>150</photo> over the <mode>x</mode> lazy dog."
set str to quoted form of str

set perlRepl to do shell script "perl -0777 -ne 's/ *<(.+?)>.+?<\\/\\1> */ /g; print' <<< " & str
------------------------------------------------------------------------------------------------

--
Best Regards,
Chris

------------------------------------------------------------------------------------------------
# Using the Unix 'textutil'
------------------------------------------------------------------------------------------------
on write_to_file_overwriting(someText, targetFile)
  try
    set resultNumber to open for access targetFile with write permission
    set eof of resultNumber to 0
    write someText to resultNumber
    close access resultNumber
  on error errMsg number errNum
    try
      close access resultNumber
    end try
    error errMsg
  end try
end write_to_file_overwriting
------------------------------------------------------------------------------------------------
try

  

  set tempFile to "" & (path to temporary items from user domain) & "junk.html"
  set _posixPath to quoted form of (POSIX path of tempFile)
  set str to "The <letter>a</letter> quick <photo>12</photo> brown fox jumps <photo>150</photo> over the <mode>x</mode> lazy dog."

  

  write_to_file_overwriting(str, tempFile)

  

  set cmd to "tr '\\r' '\\n' " & _posixPath & ";
textutil -stdout -cat txt " & _posixPath

  

  do shell script cmd

  

on error errMsg number errNum
  set {cr, sep} to {return, "------------------------------------------"}
  set e to sep & cr & "Error: " & errMsg & cr & sep & cr & "Error Number: " & errNum & cr & sep
  beep
  display dialog e
end try
------------------------------------------------------------------------------------------------

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Filter a text string
      • From: Bert Groeneveld <email@hidden>
References: 
 >Filter a text string (From: Bert Groeneveld <email@hidden>)

  • Prev by Date: Re: Reading IPTC and EXIF information from photos
  • Next by Date: Safari Status Bar
  • Previous by thread: Re: Filter a text string
  • Next by thread: Re: Filter a text string
  • Index(es):
    • Date
    • Thread