read a window file ".txt" with OSX

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

read a window file ".txt" with OSX

Post by jmburnod » Sat Dec 11, 2010 11:03 am

Hi All,

I have a window text file type ".txt"

If a open it with textedit i have what i want (same result with copy/paste from textedit to rev 4.0)
LevelName: Session
LevelName: Block
LevelName: Trial
LevelName: SubTrial

If i read it by script and i put it into a fld

I get this

L e v e l N a m e : S e s s i o n

L e v e l N a m e : B l o c k

L e v e l N a m e : T r i a l

L e v e l N a m e : S u b T r i a l


What i forget ?

Thank

Jean-Marc
https://alternatic.ch

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: read a window file ".txt" with OSX

Post by bn » Sat Dec 11, 2010 2:33 pm

Hi Jean-Marc,

could you post a zipped version of the file. If it is really a txt file you should be able to read it by script.

regards

Bernd

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: read a window file ".txt" with OSX

Post by jmburnod » Sat Dec 11, 2010 6:03 pm

Hi Bernd,

Here is a little stack and a text file exemple

Best

Jean-Marc
Attachments
ReadWindowfile.zip
(22.84 KiB) Downloaded 211 times
https://alternatic.ch

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: read a window file ".txt" with OSX

Post by bn » Sat Dec 11, 2010 9:48 pm

Hi Jean-Marc,

the problem is that the .txt file is encoded in UTF8. That is where the problem arises. The spaces are placeholders for ASCII 0.

I always get confused with Livecode's unidecode/uniencode.

I did a replace function that shows first the original text where I replace ASCII 0 with "•" on the Mac.
Then the function tries to take out the "funny" characters.
All this is returned by the function and your script puts that into field "LeT"
You would have to enlarge the field.

Code: Select all

function ReadUnFi pF --•• lire un fichier
   put pF into PathFi
   put empty into rReadUnFi
   put url ("binfile:" & pf) into rReadUnFi
   repeat with i = 1 to length (rReadUnFi)
      if charToNum(char i of rReadUnFi) = 0 then
         put "•" after tCollect
      else
         put char i of rReadUnFi after tCollect
      end if
   end repeat
   
   put cr & cr after tCollect
   repeat with i = 1 to length (rReadUnFi)
      put charToNum(char i of rReadUnFi) into taChar 
         if (taChar > 32 and taChar < 254)  or taChar is among the items of "10"  then
            put char i of rReadUnFi after tCollect
         end if
   end repeat
   return tCollect
end ReadUnFi
What you could do is: either get the text as pure ASCII. That would be the easiest. Or you could try to do a Unicode conversion. Or someone can look into the unicode conversion.

Kind regards

Bernd

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: read a window file ".txt" with OSX

Post by jmburnod » Sun Dec 12, 2010 11:10 am

Hi Bernd,

Thank.
That is better but it delete all spaces and i need them

Original text:
LevelName: Session

ReadUnFi() return
LevelName:Session

Original text:
textcor: bravo, c'est la bonne rÈponse
ReadUnFi() return
textcor:bravo,c'estlabonnerÈponse

All the best

Jean-Marc
https://alternatic.ch

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: read a window file ".txt" with OSX

Post by bn » Sun Dec 12, 2010 11:49 am

Hi Jean-Marc,

sorry about that
replace

Code: Select all

if (taChar > 32 and taChar < 254)  or taChar is among the items of "10"  then
with

Code: Select all

if (taChar > 31 and taChar < 254)  or taChar is among the items of "10"  then
ASCII 32 is the space and I did not pay attention to that.

if you have other special charcters that are ASCII 31 or below just add them to the exception list
Lets say you want ASCII 11 in your result then you would say

Code: Select all

if (taChar > 31 and taChar < 254)  or taChar is among the items of "10,11"  then
for the high ASCII characters = the accented characters you could to a conversion with the isoToMac() function

Kind regards

Bernd

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: read a window file ".txt" with OSX

Post by jmburnod » Sun Dec 12, 2010 8:24 pm

hi Bernd,

It work fine now
See the comment about isotomac before or after the repeat

Code: Select all

function ReadUnFi pF --•• lire un fichier
   put pF into PathFi
   put empty into rReadUnFi
   put url ("binfile:" & pf) into rReadUnFi  
  -- put url ("binfile:" & pf) into  buf  
   --   put isoToMac(buf) into rReadUnFi --•• if i do isotomac() before the repeat i have a "ÿ" (ascii 216) in char 1 of the text 
  put empty into tCollect
   repeat with i = 1 to length (rReadUnFi)
      put charToNum(char i of rReadUnFi) into taChar
      if (taChar > 31 and taChar < 254)  or taChar is among the items of "10"  then
         put char i of rReadUnFi after tCollect
      end if
   end repeat
   put isoToMac(tCollect) into bufTemp --•• if i do isotomac() after the repeat it work fine
   put bufTemp into tCollect
   return tCollect
end ReadUnFi
All the best

Jean-Marc
https://alternatic.ch

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: read a window file ".txt" with OSX

Post by bn » Sun Dec 12, 2010 10:43 pm

Hi Jean-Marc,
It is not surprising that you have funny characters if you do the isoToMac before 'cleaning' up. There is an ASCII 255 and an ACII 254 the first two characters of the text. That is why I only let ASCII 32 to 253 pass.
So you found the right place to do the conversion.

Kind regards

Bernd

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: read a window file ".txt" with OSX

Post by jmburnod » Thu Dec 30, 2010 1:00 pm

Hi Bernd And All,

I have done a little stack using a function inspired by Bernd.
still remains some behavior strange

See the attachments

Best regards

Jean-Marc
Attachments
TestConvertToMac.zip
(3.93 KiB) Downloaded 204 times
https://alternatic.ch

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: read a window file ".txt" with OSX

Post by bn » Thu Dec 30, 2010 11:19 pm

Hi Jean-Marc,

I looked at the files you have in your folder and TextWrangler tells me it is UTF-16. So I tried a different approach with unidecode/uniencode.

For your test files it works, but just don't ask me why. :)
I find this unicode stuff very confusing.

If you replace all of the code in your stack with this:

Code: Select all

on tConvertFromPCToMac
   global gPathMonFol
   put the defaultfolder into ODF
   set the defaultfolder to gPathMonFol
   answer file "Ouvrir"
   put it into LePath
   put url ("binfile:" & LePath) into rReadUnFi
   put uniencode(unidecode (rReadUnFi,"UTF-16"),"ANSI") into tAnsi
   set the unicodeText of field "LeTexte" to tAnsi
   set the defaultfolder to ODF
end tConvertFromPCToMac
It gives you the correct transliteration (if that is the word)

I just don't know why the last time TextWrangler inidcated it was UTF-8 ? Did you do anything to the files?

Anyways, I think Marc Schonewille is the one on this forum who knows most about unicode.

But if this works for you with different text files then I would just take it as it is.

Kind regards

Bernd

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: read a window file ".txt" with OSX

Post by jmburnod » Fri Dec 31, 2010 10:19 am

Hi Bernd,

I tested your new script and it work for me
I just don't know why the last time TextWrangler inidcated it was UTF-8 ? Did you do anything to the files?
Sorry for this :oops: . I think i have opened and copied the text from the original file and pasted in a new file textedit.

The original files are created by E-Prime (i think)

Thank and happy new year

Jean-Marc
https://alternatic.ch

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: read a window file ".txt" with OSX

Post by bn » Fri Dec 31, 2010 2:22 pm

HI Jean-Marc,
I just don't know why the last time TextWrangler inidcated it was UTF-8 ? Did you do anything to the files?

Sorry for this . I think i have opened and copied the text from the original file and pasted in a new file textedit.
I was just wondering if your source was changing the unicode. Since this is not the case you should have a consistent textformat and you should be OK.

Happy New Year

Bernd

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: read a window file ".txt" with OSX

Post by jmburnod » Fri Dec 31, 2010 6:07 pm

Hi Bernd,

Thank one more for your help
Your script work fine except i have char tab (9) before some lines

I delete it with a replacetext() and that is perfect ... for this time

Happy new year

Jean-Marc
https://alternatic.ch

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Re: read a window file ".txt" with OSX

Post by bn » Fri Dec 31, 2010 6:19 pm

Hi Jean-Marc,

could you send me one or preferably more example files off list?

Or you could post them here?

Kind regards

Bernd

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: read a window file ".txt" with OSX

Post by jmburnod » Fri Dec 31, 2010 6:30 pm

Hi Bernd,

I'm confuse. There is chars Tab in the original file

Code: Select all

could you send me one or preferably more example files off list
Yes of course.

Jean-Marc
Attachments
testConvert.txt.zip
(2.92 KiB) Downloaded 212 times
https://alternatic.ch

Post Reply