FAQ: How do I access a Morph from a Workspace?
Last updated at 4:35 am UTC on 8 October 2017
From the mailing list:
Once you drag a widget out and place it somewhere, is there a way to assign it to a variable so that you can then send it messages? I realize you can create things with openInWorld, and that you can do a lot of things via halos and menus, but I also want to be able to send messages via a line of code to things already dragged out.
You can do this easily in a workspace.
From its window menu, check "create textual references for dropped morphs". Then drop a Morph on the Workspace. It will slide back to where it was, leaving its name (bound to the Morph in the Workspace's private dictionary).
Note: Squeak 5.1 gives an error when doing this but see this post for a fix: http://forum.world.st/Error-creating-textual-references-to-dropped-morphs-td4914257.html
From there you can write code in the Workspace to use it. Keep in mind the textual reference only works in the workspace where the Morph was dropped. So, for example, if your Morph produced the textual reference 'roundRect1399793' you can simply type something like:
roundRect1399793 flashBounds in the workspace to reference it (and make it flash visibly on the screen).
There is also the clipboard; you can copy a Morph to the clipboard and refer to it as
Or you can do something like (if you know there's only one of your kind of Morph about):
MyMorphClass allInstances first
Another option is to open an inspector on the morph by the debug menu and work in the evaluation pane of the inspector.
If a Morph is created by Smalltalk code, then you might give it a name.
(RectangleMorph new name: 'r3') openInWorld
Later you may access it and set for example an attribute
(World submorphNamed: 'r3') color: Color red.
Remark: The #submorphNamed: message works only for direct submorphs of World.
Question: is there a general way to find a morph by name? I tried out something like
|mymorph| World allMorphsDo: [:m| m knownName = 'Yabba' ifTrue: [ mymorph _ m ]]
But this seems awfully complicated for such a basic thing.
Morphs understand the message 'subMorphNamed:' so for the above you could do
myMorph _ World subMorphNamed: 'Yabba',