Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kipry

macrumors newbie
Original poster
Jun 12, 2015
3
0
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
 
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
}

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.
 
Last edited:
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 ?
 

Attachments

  • m.txt
    17 bytes · Views: 304
Hi, the file is txt, sorry. the script say the readFilePath is not defined.

Apologies - that was a typo. I've edited my original post.

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

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.

 
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.