Page 1 of 1

Solved*reading an rtf file

Posted: Thu Aug 11, 2011 10:20 pm
by JamesWebster
Putting this into my card didn't function

Code: Select all

local Intro
On Preopencard
   put "/Tippen/Intro.rtf" into Intro
   disable fld id 1024
   Open file specialFolderPath("desktop") & Intro for read
   read from file specialFolderPath("desktop") & Intro until EOF
   set the rtftext of fld id 1024 to url(specialFolderPath("desktop") & Intro)
   close file specialFolderPath("desktop") & Intro
end Preopencard
While I want the content of the rtf file in the field, the only thing I get is its filepath, any advice?

Re: reading an rtf file

Posted: Thu Aug 11, 2011 10:55 pm
by bn
Hi James,

welcome to the Forum.

You are actually mixing two ways to read data into your field:
one where you first open the file, then read and close the file. After reading until EOF the data is in the variable it

Code: Select all

On Preopencard
   local Intro -- initialization optional
   put "/Tippen/Intro.rtf" into Intro
   disable fld id 1024
   Open file specialFolderPath("desktop") & Intro for read
   read from file specialFolderPath("desktop") & Intro until EOF
   set the rtftext of fld id 1024 to it
   close file specialFolderPath("desktop") & Intro
end Preopencard
the other URL form is easier/shorter for reading a whole file, here you put it directly into the rtfText of the field

Code: Select all

On Preopencard
    put "/Tippen/Intro.rtf" into Intro
    disable fld id 1024
    set the rtftext of fld id 1024 to url ("file:" & (specialFolderPath("desktop") & Intro))
end Preopencard
both scripts tested and they work.

with the url form it is important to put "url ("file:" & (specialFolderPath("desktop") & Intro))" into parenthesis. Otherwise here wrong: url "file:" & (specialFolderPath("desktop") & Intro) it does not work.

You initialized a script local variable at the top of the script but only used one handler. The script local variable is persistent between calls and accessible to every handler that is below its initialization. Not needed here. If you want to initialize the variable you can do it as I indicated in the first script. Except if you turned on explicit variable in the preferences: then you have to declare the variables. But still in this case no need to declare the variable outside of the handler.



Kind regards

Bernd

Re: reading an rtf file

Posted: Thu Aug 11, 2011 11:12 pm
by JamesWebster
thank you very much - I guess I've got a lot to learn going from VB to Livecode