Applescript Problem validate

Discussion in 'Mac Programming' started by kipry, Jun 12, 2015.

  1. kipry macrumors newbie

    Joined:
    Jun 12, 2015
    #1
    Hello , I am new to the community and look forward to learning a lot here , I have a question with a simple script. The problem is, in the file m.rtf, I have this
    {
    text3,21
    text3,4
    }

    The problem is that it found the string "text3.2" in the file and tells me that is right , how I can do to find exactly the string original "text3,2" ?

    Code:
    set Sstring to "text3,2"
    set file 1 to read alias ((path to library folder as text) & "m.rtf")
    
    if Sstring is in file1 then
    
    say "yes"
    
    else
    say "no"
    end if
     
  2. superscape, Jun 12, 2015
    Last edited: Jun 12, 2015

    superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #2
    Hi,

    Are you sure it's an rtf file? I don't think you should be able to read an rtf file like that. Could you post a copy of the file?

    If it's a txt file then you can do it like this;
    Code:
    --let the user select the file
    set theFilePath to choose file
    
    --set the search string
    set Sstring to "text3,21"
    
    --read the input file
    set theFileContents to read theFilePath
    
    --theResult will be true or false depending on whether we found the string
    set theResult to false
    
    --get a list of paragraphs
    set theParaList to every paragraph of theFileContents
    
    --go through each paragraph
    repeat with thePara in theParaList
    
        if thePara as text is Sstring then
    
            --if the contents of the paragraph match the search string then
           --set the result to true and stop the repeat loop
    
            set theResult to true
    
            exit repeat
    
        end if
    
    end repeat
    
    
    display dialog theResult
    
    ...if it turns out that it is actually an rtf file then you can convert it to a txt using textutil on the command line and then continue as shown above. If you struggle with that, let me know.

    Regards
    r.
     
  3. kipry thread starter macrumors newbie

    Joined:
    Jun 12, 2015
    #3
    Hi, the file is txt, sorry. the script say the readFilePath is not defined.

    Where I can put one else in the script if not in that file skip to the next file ?
     

    Attached Files:

    • m.txt
      File size:
      17 bytes
      Views:
      18
  4. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #4
    Apologies - that was a typo. I've edited my original post.

    You'd need to have a list of files to check and loop through it. You do a loop as follows:

    Code:
    --set up a list of file paths
    set theFileList to {"Macintosh HD:Users:Admin:Desktop:foo.txt", "Macintosh HD:Users:Admin:Desktop:bar.txt", "Macintosh HD:Users:Admin:Desktop:blah.txt"}
    
    --loop through the list
    repeat with theFile in theFileList
       display dialogtheFile
    end repeat
    
    ...I'll leave it as a fun exercise for you to work out how to incorporate the loop into the earlier script. ;-)

    It's worth noting that I've given examples in AppleScript because that's what yo asked for, however this sort of thing *might* be better of as part of a shell script.

     
  5. kipry thread starter macrumors newbie

    Joined:
    Jun 12, 2015
    #5
    i have this, but is not right, it say me that is in the 20 file and not is.

    Code:
    -let the user select the file
    set theFile1 to "Library/folder/v16.txt"
    set theFile2 to "Library/folder/v18.txt"
    set theFile3 to "Library/folder/v20.txt"
    
    set Sstring to "text3,21"
    --read the input file
    set theFileContents to read theFile1
    
    --theResult will be true or false depending on whether we found the string
    set theResult to false
    
    --get a list of paragraphs
    set theParaList to every paragraph of theFileContents
    
    --go through each paragraph
    repeat with thePara in theParaList
       
        if thePara as text is Sstring then
            set theResult to say "is in the 16 file"
            --if the contents of the paragraph match the search string then
            --set the result to true and stop the repeat loop
           
        else if thePara as text is Sstring then
           
            set theFileContents to read theFile2
            set theResult to false
            set theParaList to every paragraph of theFileContents
           
            set theResult to say "is in the 18 file"
        else
            set theFileContents to read theFile3
            set theResult to false
            set theParaList to every paragraph of theFileContents
           
            set theResult to say "is in the 20 file"
           
            exit repeat
           
        end if
       
    end repeat
    
    THANKS
     
  6. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #6
    Have a read through your code and think about what's happening in your repeat loop... ;-) if you're still struggling, let me know.
     

Share This Page