[squeak-dev] SketchMorph power tool: copy as code

Tim Johnson digit at sonic.net
Sat Mar 2 17:25:53 UTC 2019


Hi,

Here is a very simple change that allows one to activate the Morph menu -> "copy & paste" -> "copy text" to get textual (code) representation of the SketchMorph's form.  It simply (and rather inappropriately) implements #userString for a SketchMorph.  The resulting string can be pasted into a method or workspace or what-have-you in order to recreate the form.

I don't recommend using this in production, because:  looking at senders of #userString, I suspect this would have a negative impact on BookMorph, StackMorph, and perhaps some other places where #userString is used to access human-readable text in text-oriented Morphs.  So, perhaps this would be more appropriate as an additional item under the "painting" or "export" submenus.  Maybe it could even be generalized for all Morphs rather than just SketchMorphs.  I welcome decision-makers to make decisions about possibly using this code in some similar way, because I think it's useful and fun.

Below are two versions of it.  The second version allows the user to opt-out of using  #colorReduced .

-- snip -- 

'From Squeak5.2 of 22 January 2019 [latest update: #18231] on 2 March 2019 at 9:06:36 am'!

!SketchMorph methodsFor: 'menus' stamp: 'tcj 3/2/2019 08:58'!
userString
	| s | 
	"present textual representation of myself for Morph 'Copy text' menu item"
	s := String new writeStream.
	self form colorReduced storeOn: s.
	^ s close contents! !

-- snip --

'From Squeak5.2 of 22 January 2019 [latest update: #18231] on 2 March 2019 at 9:03:23 am'!

!SketchMorph methodsFor: 'menus' stamp: 'tcj 3/2/2019 09:02'!
userString
	| s | 
	"present textual representation of myself for Morph 'Copy text' menu item"
	s := String new writeStream.
	( ( self confirm: 'reduce colors?' )
		ifTrue: [ self form colorReduced ]
		ifFalse: [ self form ] ) storeOn: s.
	^ s close contents! !

-- snip --

Cheers,
Tim J



More information about the Squeak-dev mailing list