Saving to a file in Sugar

Skip to end of metadata
Go to start of metadata

Randy's request:

"I am starting work again on my idea from awhile back to gather information from students as they work on etoys projects and provide feedback to help their learning. I attached a script you wrote for me to record a number indicating certain activity. It asks to create a file if the file doesn't exist. The script works fine in the new etoys image as I expected and the file gets created and I can find it on a PC or Mac. When I run the project on the OLPC, it seems to work because it asks to create the file and continues on correctly after that without error messages. However, I can't locate the file image1.txt that it should have created. I tried looking in the journal, I used the terminal program to sift through the file structure and I tried open the Write program to search for the file - all to no avail. Do you have any suggestions as to how to find the file on the OLPC if it exists?"

bert's answer

I answered Randy directly and he did find the file finally in one of the obscure rainbow directories. But it's rather cumbersome and he's not very familiar with the unix shell. So I investigated what it would take to make the equivalent of "Filestream fileNamed:" using the Journal.

It's pretty much as ugly as I feared. Like, the file one gets from the Journal is read-only, so one has to copy. One has to create time stamps manually. Etc. :/ See attachment.

bert's attachment

| image1 fileName journal metaData existing utf8Name |

fileName := 'image1.txt'.
image1 := FileStream fileNamed: fileName.

SugarLauncher isRunningInSugar ifTrue: [
	journal := SugarDataStore new.
	metaData := {'title'->fileName. 'mime_type'->'text/plain'} as: Dictionary.
	existing := (journal find: metaData with: #()) first.
	existing isEmpty ifFalse: [
		metaData := existing first.
		utf8Name := journal getFilename: (metaData at: 'uid') asString.
		image1 nextPutAll: (FileStream readOnlyFileNamed: utf8Name utf8ToSqueak) contentsOfEntireFile.
		FileDirectory default deleteFileNamed: utf8Name utf8ToSqueak]].

image1 setToEnd.
image1 cr.
image1 nextPutAll: 'Hello'.
image1 tab.
image1 print: Time now.
image1 close.

SugarLauncher isRunningInSugar ifTrue: [
	utf8Name := (FileDirectory default fullNameFor: fileName) squeakToUtf8.
	metaData at: 'timestamp' put: DateAndTime now asUnixTime.
	metaData at: 'mtime' put: (DateAndTime now asString first: 19).
	existing isEmpty
		ifTrue: [journal create: metaData with: utf8Name with: false]
		ifFalse: [journal update: (metaData at: 'uid') with: metaData with: utf8Name with: false].
	FileDirectory default deleteFileNamed: fileName].

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Mar 09, 2009

    Scott Wallace says:

    When I tried Bert's script, it failed on the olpc because the version loaded on ...

    When I tried Bert's script, it failed on the olpc because the version loaded on my xo did not have the #asUnixTime method in it yet.  Bert wrote to say that a reasonable substitute would be to use "DateAndTime totalSeconds - 2177456400", so I substituted that, and modified the script to conform to Randy's original in function, and the resulting script, as sent in a project to Randy on 9 March 2009, is:

     acquire
        | image1 fileName journal metaData existing utf8Name |

        fileName := 'image1.txt'.
        image1 := FileStream fileNamed: fileName.

        SugarLauncher isRunningInSugar ifTrue: [
            journal := SugarDataStore new.
            metaData :=

    Unknown macro: {'title'->fileName. 'mime_type'->'text/plain'}

    as: Dictionary.
            existing := (journal find: metaData with: #()) first.
            existing isEmpty ifFalse: [
                metaData := existing first.
                utf8Name := journal getFilename: (metaData at: 'uid') asString.
                image1 nextPutAll: (FileStream readOnlyFileNamed: utf8Name utf8ToSqueak) contentsOfEntireFile.
                FileDirectory default deleteFileNamed: utf8Name utf8ToSqueak]].

        image1 setToEnd.
        image1 cr.
        image1 nextPutAll: self getNum asString.
        image1 tab.
        image1 nextPutAll: Time dateAndTimeNow asString.
        image1 close.

        SugarLauncher isRunningInSugar ifTrue: [
            utf8Name := (FileDirectory default fullNameFor: fileName) squeakToUtf8.
            metaData at: 'timestamp' put: (DateAndTime totalSeconds - 2177456400).
            metaData at: 'mtime' put: (DateAndTime now asString first: 19).
            existing isEmpty
                ifTrue: [journal create: metaData with: utf8Name with: false]
                ifFalse: [journal update: (metaData at: 'uid') with: metaData with: utf8Name with: false].
            FileDirectory default deleteFileNamed: fileName].
        
        self setNum: self getNum + 1

  2. Mar 16, 2009

    teefal says:

    (from Scott) Hi, Randy, Bert did come up with code that actually lets the scri...

    (from Scott)

    Hi, Randy,

    Bert did come up with code that actually lets the script create and write to a text file that is visible in the Journal! In the attached project, I've applied his technique to the method you sent a picture of earlier.

    An annoying feature of our current etoys systems is that the formatting is lost on textually-written scripts. For that reason, you'll see that in the attached project I've left the Scriptor with the original, formatted textual script, with meaningful variable names such as "fileName" and "journal", as unaccepted text in the Scriptor, so you can see it the way it ought to be; the same intended script will also be found in the the collapsed window entitled The "Acquire" Script. If you edit the code for such a script and want to preserve variable names, comments, fonts, and any other formatting changes, what you'll need to do is to maintain the master copy of the script in a separate text window, as I did in in this project, because after your code change is accepted, the code seen in the textual code pane loses all its formatting, you'll see t1, t2, t3 instead of image1, fileName, jornal, etc. This is a huge nuisance, and working with this project has led me to feel that this is a pretty high-priority thing to get fixed!

    So... please put the attached project on a usb stick, put the stick into your XO, find the project in the Journal, and open it.

    Then "run" it a bit, by either mousing over Norbert – who appears here as a yellow egg – or by clicking the "Norbert acquire" button. Then quit etoys, locate "image1.txt" in the Journal, and open it up to see what was written.