Problem reading a file in OS 10.4 vs 10.5

Hi,

I’ve got a (small?) problem with a script I’ve been working on. I have a data file I want to read in, and grab certain elements from; the trouble is that the file is of variable length and the item delimiters seem to change from day to day.

I’ve been able to get it running on the OSX 10.4 program, but when I try to work on it on 10.5.2, the data isn’t read in the same way, and I’m really not sure why. Finder identifies the file on both platforms as “Unix Executable” – the file itself is various values (stored in Hexadecimal) and text (stored as ASCII).

So, I read in the file, and I look at two characters within the document that provide me with the delimiters for the rest of the document:

set theContents to read (choose file with prompt “Choose the file you wish to parse”)
set Split1 to ASCII number (character 1827 of theContents)
set Split2 to ASCII number (character 1828 of theContents)

This part seems to be OK, but later on, I am trimming out things I don’t need, and at this point it seems the applescript text delimiters is truncating the data at the wrong spot – as I said, it’s running fine at the moment on OSX 10.4, but not 10.5, so I’m guessing it’s something to do with the way the file is read in.

Do I need to read the file in as “data” and then convert the hex data to a string to read it the old way?

Ugh, I’m not a real programmer and I’m just trying to get a silly script running to help us out with layouts at work.

Anyway, here’s the very rough code (please be kind – this is not polished)

– set up basic file delimiters
set originalDelimiters to AppleScript’s text item delimiters
set FirstCut to (ASCII character 12) & “Base Edition”
set SecondCut to (ASCII character 0) & “pCom”

– pick file to read
set theContents to read (choose file with prompt “Choose the ALS file you wish to parse”)
– set variable delimiters
set Split1 to ASCII number (character 1827 of theContents)
set Split2 to ASCII number (character 1828 of theContents)
set SplitText to (ASCII character (Split1)) & (ASCII character (Split2))

– parse out junk at end
set firstParse to theContents
set AppleScript’s text item delimiters to FirstCut
set trimtext to text item 2 of firstParse
set nexttext to text item 1 of firstParse

– parse out junk at beginning
set AppleScript’s text item delimiters to SecondCut
set trim2text to text item 1 of nexttext
set finaltext to text item 2 of nexttext

– split remaining good data into items
set AppleScript’s text item delimiters to SplitText
set theItems to text items 3 thru ((count of text items of nexttext) - 1) of nexttext

– figure out Y coordinates from hex numbers
set Y1A to ASCII number (character 59 of text item 1 of theItems)
set Y1B to ASCII number (character 60 of text item 1 of theItems)
set Y1C to ASCII number (character 61 of text item 1 of theItems)
set Y1D to ASCII number (character 62 of text item 1 of theItems)
set Y1Pos to ((Y1A * 256) + Y1B + (Y1C / 256) + (Y1D / 65536) - 28)

– figure out X coords etc
set X1A to ASCII number (character 63 of text item 1 of theItems)
set X1B to ASCII number (character 64 of text item 1 of theItems)
set X1C to ASCII number (character 65 of text item 1 of theItems)
set X1D to ASCII number (character 66 of text item 1 of theItems)
set X1Pos to ((X1A * 256) + X1B + (X1C / 256) + (X1D / 65536) - 916)

– eventually there will be other stuff here, etc.

So, this works fine (I have a few displays to verify the information) on the OSX 10.4 machine, but on OSX 10.5, I get “cannot get character 59 of …”

I have this sneaking suspicion I’m doing something very basic wrong here… but I can’t wrap my brain around it right now. I’ve spent too much time figuring out how the data was stored in the file and this problem is stumping me at the moment.

So, does anyone have any ideas or advice?

(and once again, I apologize for the way the code is written, this is just an experimental thing, honest!)

(link to file is down)

Hi, Bdemers.

Leopard’s AppleScript uses Unicode text internally, so after your file’s read, the data are coerced to that. Tiger and previous OSses use plain ASCII text internally. I don’t have Leopard myself, but after I coerce theContents to Unicode explicitly, I get the same error as you. The script appears to work exactly the same until it gets the text items of nexttext, using the delimiter SplitText. The discarded first, second, and last text items are the same whether the text is ASCII or Unicode, but (with your file) there are four extra ” and shorter ” text items in the area of interest when the text is Unicode. I don’t know why. But ASCII character 26 is a non-text character that might not translate into Unicode quite as expected. A work-round seems to be to get those text items as a continuous text rather than as a list of items.

Your script has a repeated error at the end where it tries to get a text item from a list. (The text items of a text are just items after they’re extracted into a list.)

This works for me (with your file) both when theContents is plain text and when it’s Unicode:

-- set up basic file delimiters
set originalDelimiters to AppleScript's text item delimiters
set FirstCut to (ASCII character 12) & "Base Edition"
set SecondCut to (ASCII character 0) & "pCom"

-- pick file to read
set theContents to (read (choose file with prompt "Choose the ALS file you wish to parse")) --as Unicode text
-- set variable delimiters
set SplitText to text 1827 thru 1828 of theContents

-- parse out junk at end
set firstParse to theContents
set AppleScript's text item delimiters to FirstCut
set trimtext to text item 2 of firstParse
set nexttext to text item 1 of firstParse

-- parse out junk at beginning
set AppleScript's text item delimiters to SecondCut
set trim2text to text item 1 of nexttext
set finaltext to text item 2 of nexttext

-- get remaining good data
set AppleScript's text item delimiters to SplitText
set goodData to text from text item 3 to text item -2 of nexttext

-- figure out Y coordinates from hex numbers
set Y1A to ASCII number (character 59 of goodData)
set Y1B to ASCII number (character 60 of goodData)
set Y1C to ASCII number (character 61 of goodData)
set Y1D to ASCII number (character 62 of goodData)
set Y1Pos to ((Y1A * 256) + Y1B + (Y1C / 256) + (Y1D / 65536) - 28)

-- figure out X coords etc
set X1A to ASCII number (character 63 of goodData)
set X1B to ASCII number (character 64 of goodData)
set X1C to ASCII number (character 65 of goodData)
set X1D to ASCII number (character 66 of goodData)
set X1Pos to ((X1A * 256) + X1B + (X1C / 256) + (X1D / 65536) - 916)

ASCII number and ASCII character are deprecated as from Leopard. I think they still work at the moment, but their days are numbered.

Thanks a lot, I struggled with this a fair bit last night, but I’ve got some inspiration to go back and work on it today.

That script is really far from complete so I apologize for the condition of it (and the error!) , but I’ll get back at it today. After getting my read error figured out I hope that’ll be the last hurdle I have.

You are all so helpful, thanks again. This forum has been of immense value!