read a window file ".txt" with OSX
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
read a window file ".txt" with OSX
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
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
Re: read a window file ".txt" with OSX
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
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
Re: read a window file ".txt" with OSX
Hi Bernd,
Here is a little stack and a text file exemple
Best
Jean-Marc
Here is a little stack and a text file exemple
Best
Jean-Marc
- Attachments
-
- ReadWindowfile.zip
- (22.84 KiB) Downloaded 210 times
https://alternatic.ch
Re: read a window file ".txt" with OSX
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.
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
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
Kind regards
Bernd
Re: read a window file ".txt" with OSX
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
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
Re: read a window file ".txt" with OSX
Hi Jean-Marc,
sorry about that
replace
with
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
for the high ASCII characters = the accented characters you could to a conversion with the isoToMac() function
Kind regards
Bernd
sorry about that
replace
Code: Select all
if (taChar > 32 and taChar < 254) or taChar is among the items of "10" then
Code: Select all
if (taChar > 31 and taChar < 254) or taChar is among the items of "10" then
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
Kind regards
Bernd
Re: read a window file ".txt" with OSX
hi Bernd,
It work fine now
See the comment about isotomac before or after the repeat
All the best
Jean-Marc
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
Jean-Marc
https://alternatic.ch
Re: read a window file ".txt" with OSX
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
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
Re: read a window file ".txt" with OSX
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
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 203 times
https://alternatic.ch
Re: read a window file ".txt" with OSX
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: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
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
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
Re: read a window file ".txt" with OSX
Hi Bernd,
I tested your new script and it work for me
. 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
I tested your new script and it work for me
Sorry for thisI just don't know why the last time TextWrangler inidcated it was UTF-8 ? Did you do anything to the files?

The original files are created by E-Prime (i think)
Thank and happy new year
Jean-Marc
https://alternatic.ch
Re: read a window file ".txt" with OSX
HI Jean-Marc,
Happy New Year
Bernd
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.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.
Happy New Year
Bernd
Re: read a window file ".txt" with OSX
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
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
Re: read a window file ".txt" with OSX
Hi Jean-Marc,
could you send me one or preferably more example files off list?
Or you could post them here?
Kind regards
Bernd
could you send me one or preferably more example files off list?
Or you could post them here?
Kind regards
Bernd
Re: read a window file ".txt" with OSX
Hi Bernd,
I'm confuse. There is chars Tab in the original file
Yes of course.
Jean-Marc
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
Jean-Marc
- Attachments
-
- testConvert.txt.zip
- (2.92 KiB) Downloaded 211 times
https://alternatic.ch