[Squeakland] getting code from a .txt file into a script

Ned Konz ned at squeakland.org
Tue Mar 23 08:50:32 PST 2004


On Tuesday 23 March 2004 4:13 am, Gary Frederick wrote:
> box
>    self forward: 100.
>    self turn: 90.
>    self forward: 100.
>    self turn: 90.
>    self forward: 100.
>    self turn: 90.
>    self forward: 100.
>    self turn: 90
>
> I want to be able to read it into a script in a project like the script
> project in the Worlds of Squeak.
>
> This is a class of kids that are playing with robots. They have been
> working with programming LEGO robots and with a simulator. I am
> interested in taking their programs and translating them into a script
> they can use in Squeak... if I can get the .txt files into scripts.

Are you using the eToy environment? I assume that's what you want.

You can use text scripting instead of tiles. The check box at the left top of 
the editor changes between the two modes.

However, if you edit the text in text mode you can't see it or edit it in 
(classic) tiles any more.

Here's a quick way to see the text format:

* turn on the "debugHaloHandle" preference
* go to the "drive a car" project in the Worlds of Squeak.
* bring up the halos on the car.
* click on the grey (debug) halo.
* click on "browse player class".

You will see a browser with the Player157 class in it.
Get the context menu on the name of the class (Player157) and choose 
"fileOut".

Now you will have a Player157.st file on disk.

Here's what it looks like (NOTE that you MUST use bare CR characters (Mac text 
format) if you are editing or building such scripts, though CR/LF may also 
work in a pinch):

--------------- 8< --------------
'From Squeak3.7alpha of 11 September 2003 [latest update: #5816] on 23 March 
2004 at 7:44:49 am'!
Player subclass: #Player157
	instanceVariableNames: 'isTrue'
	classVariableNames: ''
	poolDictionaries: ''
	category: 'Morphic-UserObjects'!

!Player157 methodsFor: 'access'!
getIsTrue
	^ isTrue! !

!Player157 methodsFor: 'access'!
setIsTrue: t1 
	isTrue := t1! !


!Player157 methodsFor: 'scripts'!
drive
	self forward: 7.
	self turn: Wheel getHeading // 5! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

Player157 class
	instanceVariableNames: 'cardPlayer111 unscriptedPlayer1 unscriptedPlayer2'!

!Player157 class methodsFor: 'reference'!
refCardPlayer111
	^ cardPlayer111! !

!Player157 class methodsFor: 'reference'!
refUnscriptedPlayer1
	^ unscriptedPlayer1! !

!Player157 class methodsFor: 'reference'!
refUnscriptedPlayer2
	^ unscriptedPlayer2! !
--------------- 8< --------------


So if you could translate your text files into this format you may be able to 
use them directly (depending on the vocabulary).

What's the format of your text files? Can you send me one?

You could do this:

* make a blank project with a Sketch in it that represents the robot.

* Make a new script in that Sketch that will prompt for a file and read your 
text format into methods (it would be best, probably, to put the reader 
method in another class (perhaps Player) so that you can make multiple Sketch 
classes that can read such files).

Let's say that your scripts have the method names in column 1.

The enclosed change set (which you would file into your new project) will let 
you make a script that will read such files.

> I'll put some scripts into an image and let them each have a copy of the
> image if I don't find out how to go from a .txt file to a script. That
> solution is better than nothing and I can do it by tomorrow :-)

You should distribute projects instead of images.

-- 
Ned Konz
http://bike-nomad.com/squeak/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ScriptEditorText.png
Type: image/png
Size: 2442 bytes
Desc: not available
Url : http://squeakland.org/pipermail/squeakland/attachments/20040323/ad3d1b3c/ScriptEditorText-0001.png
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ScriptEditor.png
Type: image/png
Size: 2387 bytes
Desc: not available
Url : http://squeakland.org/pipermail/squeakland/attachments/20040323/ad3d1b3c/ScriptEditor-0001.png
-------------- next part --------------
'From Squeak3.7alpha of 11 September 2003 [latest update: #5816] on 23 March 2004 at 8:49:14 am'!
"Change Set:		ReadTextFilesIntoPlayer-nk
Date:			23 March 2004
Author:			Ned Konz

This is a demo of converting text files into eToy textual scripts.

The Player method #getMethodsFromFile will prompt for a file name and then read methods from the selected file, if any.

The file format is simple: lines starting with a non-blank (space or tab) character start a new method.

Textual scriptors will be constructed for the new methods.

To add a button to read such scripts to your etoy objects, just add a (textual) script that calls getMethodsFromFile like this:

	readTextFile
		self getMethodsFromFile

"!


!Player methodsFor: 'reading robot commands' stamp: 'nk 3/23/2004 08:22'!
getMethodsFromFile
	"Prompt for a file and read methods from it."

	| file fileName |
	file _ FileList2 modalFileSelector.
	file ifNil: [ ^self ].
	fileName _ file name.
	file close.
	self readMethodsFrom: fileName! !

!Player methodsFor: 'reading robot commands' stamp: 'nk 3/23/2004 08:40'!
readMethodsFrom: aFileName 
	"Read methods from a file named aFileName. 
	Each method begins with a method name in the first column. 
	There may be arbitrary line endings in the file."
	| file methodTextStream methodText line methodName aMethodWithInterface aScriptEditor |
	methodTextStream := WriteStream
				on: (String new: 100).
	file := CrLfFileStream readOnlyFileNamed: aFileName.
	["skip leading blank lines"
	[line := file nextLine.
	line notNil
		and: [line isEmpty]] whileTrue.
	(line isNil
			or: [line first isSeparator not])
		ifTrue: [methodText := methodTextStream contents.
			methodTextStream reset.
			methodText isEmpty
				ifFalse: [
					self class removeSelectorSilently: methodName.
					aMethodWithInterface := self class permanentUserScriptFor: methodName player: self.
					aScriptEditor := aMethodWithInterface instantiatedScriptEditorForPlayer: self.
					aScriptEditor install.
					aScriptEditor showSourceInScriptor.
					aMethodWithInterface selector numArgs == 0
						ifTrue: [self class
								allSubInstancesDo: [:anInst | anInst scriptInstantiationForSelector: aMethodWithInterface selector]].
					"The above assures the presence of a ScriptInstantiation 
					for the new selector in all siblings"
					self class compile: methodText classified: 'scripts'.
					self updateAllViewersAndForceToShow: #scripts].
			line ifNil: [ ^self ].
			methodName := line asSymbol.].
	line
		ifNil: [^ self].	"end of file"

	methodTextStream nextPutAll: line withoutTrailingBlanks;
		 cr] repeat! !

-------------- next part --------------
A non-text attachment was scrubbed...
Name: getMethodsScriptor.png
Type: image/png
Size: 2478 bytes
Desc: not available
Url : http://squeakland.org/pipermail/squeakland/attachments/20040323/ad3d1b3c/getMethodsScriptor-0001.png


More information about the Squeakland mailing list