how to read a file

Ok, I’m NOT a coder per se, I do TV production. I’m OK with Applescript but things are NOT working with my XOJO app so I am trying to have XOJO read the text file (2 lines of text) instead of Applescript. Applkescripts always work when I click a button, but NOT when the app boots up and I need for something to happen.

Looking at the docs sample TextInputStream (below) makes no sense to me. The file is located in the Mac Users HOME folder. Easy to access with Applescript. Can someone help me get text from line 1 and 2 from the text file. Thanks so much from this beginner.

Dim f As FolderItem = GetOpenFolderItem("text") // as defined in File Type Sets Editor If f <> Nil Then If f.Exists Then // Be aware that TextInputStream.Open coud raise an exception Dim t As TextInputStream Try t = TextInputStream.Open(f) t.Encoding = Encodings.MacRoman TextArea1.Text = t.ReadAll Catch e As IOException t.Close MsgBox("Error accessing file.") End Try End If End If

SpecialFolder.UserHome.child(“nomefile.txt”)
For find file in user folder

dim f as folderitem
f = SpecialFolder.UserHome.child("nomefile.txt")
if f <> nil and f.exist then
   Dim t As TextInputStream
   Try
       t = TextInputStream.Open(f)
       t.Encoding = Encodings.MacRoman
       TextArea1.Text = t.ReadAll
   Catch e As IOException
       t.Close
       MsgBox("Error accessing file.")
       End Try
end if

if U want to read a line per line use t.readline and while not t.eof for make a loop
In try …

dim string_read as string
 t = TextInputStream.Open(f)
 t.Encoding = Encodings.MacRoman
while not t.eof
    string_read = t.readline
    .....
    .....
wend

Ciao da Italia.

thanks massi, I’ll put this code in my app and see if it works. thanks

Of anything, here we all help

many errors, can’t figure out how to fix it. I just need line 1 from the text file

Edit (Paul): Corrected image URL per Post Formatting Tips .

You can’t combine the two different bits of code Massimiliano gave you like that. Plus it’s “Exists”, not “Exist” and you don’t have a TextField named TextField1.

You really only need to slightly tweak the example for TextInputStream to access the file from the correct location and then to assign the first two lines to variables.

Dim line1, line2 As String
Dim f As FolderItem = SpecialFolder.UserHome.Child("_CBN Search passwords.txt") // your file location
If f <> Nil Then
  If f.Exists Then
    // Be aware that TextInputStream.Open could raise an exception
    Dim t As TextInputStream
    Try
      t = TextInputStream.Open(f)
      t.Encoding = Encodings.UTF8
      line1 = t.ReadLine // Read the first line
      line2 = t.ReadLine // Read the second line
    Catch e As IOException
      t.Close
      MsgBox("Error accessing file.")
    End Try
  End If
End If

never take code from the forum and expect it to work first time.
have you tried anything yourself?

f.exists not file.exist … the error even tells you this.

t as Textinputstream is dim 'd inside an IF statement… so it becomes nil when that ends.
Move it to the first line of the code so that it is available everywhere

You dont have a textfield1 on your window.

edit: Paul got there first

thank you paul!!!

yippeeee, it works. Now I have to stare at the code for 4 hours until I understand what all is going on. Thank you Paul so much.

You will find many such examples in the Examples Folder delivered with Xojo. If you do not “have the time” to study the Docs, maybe this is a place where you find the inspiration you need? :slight_smile:

sasha, I have the time or I make the time, but I find if someone gives me the code, I learn faster from that. I really do try to learn it myself by watching tutorials or opening examples but the syntax trips me up and I never get the right result.

I would say, as shown by what happened in this thread, that this statement is false.
You were given example code from the documentation, and when it didn’t work you couldn’t see the errors.

We’re all very happy to help, but the errors you had with the example code from the documentation are considered very basic things. I know you’re coming from AppleScript, but I would start with http://www.xojo.com/resources/learn.php It’s a nice “textbook” that teaches the basics of Object Oriented Programming while teaching Xojo at the same time. It’s a 2-for-1 starter book.

I really do appreciate that you try things before coming here and demanding example code (this happens a lot!) so keep up the great work! If you check out that textbook and the Xojo manual you’ll find in the Documentation folder you may find things start to click. It’s very rewarding to be able to figure things out yourself, so once you get the hang of Xojo I think you’ll love it.

Different people learn differently. We have nearly 66 hours of training video (that’s over 200 videos) available to subscribers at http://xojo.bkeeney.com/XojoTraining/. We’ve streamed over 10,000 hours of streaming video to Xojo developers around the world. Most projects come with source code you can use in your own projects.

I’ve had people comment that it was like peering over someone’s shoulder and getting a peek at how someone with a lot of experience does it. Might be your cup of tea or it might not but it’s an option.

thanks Tim & Bob

That’s exactly why I recommended to take a look into those examples. Anyway, don’t get us wrong please, you are always welcome to ask here first. :slight_smile:

try some stuff
read the docs
try some more stuff
read the examples
try some more stuff
then ask

thats my preferred order (and tell us what stuff you’ve tried so far)

Norm, awesome