Python >>> Transcript Translation
Alex Tweedly
alex at tweedly.net
Sat Dec 18 19:26:16 EST 2004
At 18:14 18/12/2004 -0500, Troy Rollins wrote:
>On Dec 18, 2004, at 2:35 PM, Roger.E.Eller at sealedair.com wrote:
>
>>Can anyone out there who is familiar with Python please translate the
>>script below to Transcript? This script is supposed to be able to create a
>>Hard-Drive image that works with the PearPC Macintosh Emulator. I want a
>>native Rev script that can create this multi-gigabyte file.
>
>But... that script is importing and using a separate object (script
>instantiation) called "sys" which is actually doing all the work.
Well, not really "all the work". The "sys" import is only used to extract
the command line arguments.
>Even with access to that, I don't have any idea if Transcript can actually
>achieve *all* the same things Python can... Python is tied into OSX at the
>system level.
Python uses standard system libraries - not all of which Rev lets you
access; but in this case, there shouldn't be any problem.
Let's get the arguments from an input field, instead of a command line
argument ...
>#!/usr/bin/python
># Copyright (c) 2004 Marco Lange.
>
>
>import sys
>
>
>GRANULARITY = 516096
>
>
>imagename = sys.argv[1]
>imagesize = int(sys.argv[2])
>if imagesize % GRANULARITY != 0:
> imagesize = ((imagesize / GRANULARITY) + 1) * GRANULARITY
>
>
>print "Using image size:", imagesize
Simply gets a file name and a value; takes the value, and rounds (up) to a
multiple of granularity.
local GRANULARITY = 516096
put field "inputField" into myVar
if myVar mod GRANULARITY <> 0 then
put GRANULARITY * (trunc(myVar/Granularity)+1) into myVar
end if
>imagefile = open(imagename, "wb+")
>imagefile.seek(imagesize-1)
>imagefile.write("\x00")
>imagefile.close
Open the file named earlier for binary write (overwriting any existing
content), seeks to the byte position (from the value above), writes one
null byte and closes the file. (i.e. creates a large, empty file)
ask file "File to write" .....
put it into myFileName
open file myFileName for binary write
seek to myVar in file myFileName
write numtochar(0) to file myFileName
close file myFileName
NB - not tested, but allowing for any typos, that should do it.
There's perhaps an "off by one" error - because Python numbers bytes from
zero, and Rev from 1 - so the seek should maybe be to "myVar-1".
-- Alex.
More information about the use-livecode
mailing list