3 Replies Latest reply: Apr 25, 2012 4:59 AM by Frinton Fogey
Frinton Fogey Level 1 Level 1 (0 points)

How do I search for and select a repeating phrase in a file read via Applescript. The file contains details of phone calls submitted to a charity and I want to be able to analyse the types of call taken. The phrase is 'Type of enquiry' and I want to extract the description that follows. I am a beginner at Applescript and have been working my way through Applescript 1-2-3 but cannot find an obvious solution although I have manged to open and read the file! All suggestions gratefully received.


iMac, Mac OS X (10.7.1)
  • 1. Re: How do I search for and select a repeating phrase in a file.
    Camelot Level 8 Level 8 (45,790 points)

    In all honesty while this can be done in AppleScript, it's not easy, at least compared with some other options, including a shell command.

     

    For example, if the data you want is on the line following the 'Type of enquiry' line then a one-line grep command in Terminal.app will do that:

    grep -A 1 'Type of enquiry' /path/to/file.txt

    You'll need to save the data as a plain text file, if it isn't already, and provide the path to that file where I've used '/path/to/file.txt'.

     

    There are various ways of formatting this data, too, depending on what you need, but you didn't cover that in your post.

  • 2. Re: How do I search for and select a repeating phrase in a file.
    twtwtw Level 5 Level 5 (4,690 points)

    In applescript you can use something like the following:

     

    set {oldTID, my text item delimiters} to {my text item delimiters, "Type of enquiry"}

    set dataBits to text items of textToParse

    set my text item delimiters to oldTID

     

    the variable dataBits, then, will be a list where every list item is a string that starts with the text following that phrase. 

     

    You could do regular expressions searches as well, but you'd need to download and install the Satimage osax.

  • 3. Re: How do I search for and select a repeating phrase in a file.
    Frinton Fogey Level 1 Level 1 (0 points)

    Thanks for the sugesstions, the Applescript solution was helpful but didn't really give the result I was looking for whereas the grep command produced exactly what I wanted, so much to learn, so little time!