Create binary file from Applescript (in OS 8.6!)

I hope someone with a long memory can help with this problem. I am trying to write an AppleScript to run OS 8.6 that will write a binary file in WordPerfect format if the file does not already exists. The file is around 6K bytes long, and AppleScript under OS 8.6 gives an error message if I try to write a 6K byte line.

So I am trying something like this, to build up the file from separate blocks of data, but it does not create the correct file:

try
			open for access theFile with write permission
			write «data rdatFF57504310000000012C020100000000DB030012CD6B7F580000067B57507D313000001203DBD20A001AFF0000000F003C000F003CFF0000000F004B000F004B001A0AD2D3000014002EFF2E002CFF2C002E002E002C002C001400D3D101» to theFile
			close access theFile
			open for access theFile with write permission
			write «data 0018CD6B7F58000300011C2300000756657264616E61001801D1D20C001CCD6B7F580000000C00030000000C1C230756657264616E61001C0CD2D20D001CCD6B7F580000000C00030000000C1C230756657264616E61001C0DD2DB00000200DB313030» to theFile starting at eof
			close access theFile
		on error errNumber
			tell me to activate
			display dialog "Error: " & errNumber
			error number -128
		end try

I am obviously doing something very wrong, but I can’t guess what it might be. I will be grateful for any help.

Hi emendelson,

You should have posted the error message.

Anyway, I can’t remember back to OS 8.6, but maybe you couldn’t use the file reference (theFile) when writing but had to use the reference number. You get the reference number when you open for access:

set reference_number to (open for access theFile with write permission)

Then you use that reference number to reference the file instead of the variable theFile.

The reason you use the try/error block is so you can trap the error and close access the file. Otherwise, if there’s an error, the file will still be opened. If this happens, you will get errors when you try to open the file again (in OS 8.6). So, after this line:

on error errNumber

add:

close access reference_number

Also, the variable theFile has to be a file specification reference (a file that may not exist) if you want the file to be created if it doesn’t exist. You might be getting errors if the file was discarded and you used an alias reference.

Editted: also, take out these lines:

      close access theFile
       open for access theFile with write permission

You don’t need to close it and open it right after. Just use the same reference_number. After you write the first set of data, the file pointer is pointing at end of file, then you can just write again and it will write at the end of file.

I don’t think the limit was 6 kilobytes.

gl,

Besides everything kel1 said, the second and all subsequent data objects should also begin «data rdat. (or data something else with four letters), otherwise the first four bytes of data are taken to represent the data type.

Sorry - the error message when I tried to write all 6KB was “Way too long, Dude!” They had informal error messages in those days!

That error message did not appear when I broke the data into smaller blocks.

Thanks also for your corrections. I’ll experiment again. But I think I still don’t know how to append the second block of data to the first block.

Hi emendelson,

After you write the first piece of data, the file pointer points there until you close access the file. So don’t close access the file and reopen it again. Just write the second piece of data right after the first.

gl,

Many thanks for these responses - I’ve now got my code correctly writing the data to the file.

Now I discover that I need to do one more thing: read and write the resource fork of the document, which contains information that I need to reproduce.

I can’t find any Applescript command that reads or writes a resource fork to a file. Is there any way to write a resource fork? (I can read the data in OS X if necessary, but I am trying to write it in OS 8.6.)

Thanks for any help - if this is possible at all!

Fun to see someone is still scripting for the classic environment. I remember that the user could define the memory the application is allowed to use to fine tune the OS. Which was needed when working with heavy QuarkXPress pages and only the power of a PowerMac G3.

Back on topic again: I’ve used resource IO OSAX to be able to read resource forks from font files in Mac OS. I think it was from late night software, but I’m not 100% sure.

Hello.

Another possible workaround. that may work (under the assumption that a resource fork created on OS X is compatible with OS 8.5) is to move the file to OS X and use ditto to make an archieve, then restore the resourcefork from that archieve.

I just found it worth mentioning.

Thanks for these suggestions. I couldn’t find resource IO from Late Night Software, though that sounds like something that may be useful here.

Here’s what I’m trying to do: I’m writing a startup script for OS 8.6 running in the Sheepshaver emulator under OS X. I use this for running WordPerfect for the Mac, which of course doesn’t run under OS X. The startup script tests for the existence of a WordPerfect document that WordPerfect uses when it starts up in order to read some default settings.

If the document doesn’t exist, I want the Applescript to create the missing file. Thanks to the answers given above, I can create the data fork, but not the resource fork, which is essential (it contains a WordPerfect macro that needs to run when the file is read).

If I can’t create the resource fork, the easiest thing is to have a backup somewhere the user isn’t likely to delete it, and simply copy it, but it would be more elegant and efficient to write the file.

Thanks again for any help.

Hi emendelson,

ResEdit was a good app for working with resources. I think all versions used to work in 8.6. Here’s the documentation:

http://developer.apple.com/legacy/mac/library/documentation/mac/pdf/ResEditReference.pdf

gl,

Actually I use it all the time (or sometimes, anyway) in OS 8/9. But I don’t think I can use it through Applescript to create and write a resource.

Meanwhile, I’ve written to Late Night Software to ask if they might be willing to give away the OS 8/9 versions of their osax software, in the same way they give away some recent items, and will report if I hear anything back. Apparently, their Resource IO osax is exactly what I am looking for to use in this project.

Mark Aldritt at Late Night Software has very generously offered a download link to the last version of Script Tools, and he says it’s OK to post the link here:

http://www.latenightsw.com/archives/ScriptTools1.3.6.sit.hqx

I’m about to download it, and I hope I’m not the only person who will find it useful…

I’m very glad it worked out so well. I think/hope you have found the best solution for Mac OS.

Also thank you for sharing this link with us. Good luck with your project!

Actually, it turns out that Resource IO is not part of Script Tools, but was part of the LNS Scripting Additions that was sold separately and also packaged with Script Debugger, though not included in the demo versions. So I’m still searching…!

Hi emendelson,

You can try finding Satimage osax for os8.6. I don’t know if it had the resource tools, like ‘put resource’, back then and added it later. I couldn’t find it at the Smile site:

http://www.satimage.fr/software/en/smile/index.html

gl,

Mark Aldritt sent me a copy of the full set of LNS Scripting Additions, so I’ve got all the tools I could possibly need. I’ve asked if it’s OK to post them somewhere, and if he gives the OK, I’ll post for anyone else who might need them.