Workspace shared variables - Answer to exercise
Last updated at 2:48 pm UTC on 20 September 2019
There are different solutions, let's see first Morphic way, based on clicking object, then then the Workspace way, based on typing and evaluating code in the Workspace.
Solution 1. Through the Window menu
In the title bar of the Workspace window choose the window menu. This is, click on the blue icon with white triagle laying on the top right corner of the Workspace window.
Solution 2. Morphic based drill down to the bindings dictionary
- clicking on the red halo –> you get the halos of the system window which wraps the workspace object
- choosing 'debug' -> 'explore'
- then click on 'model' –> the model
- you see an instance variable 'bindings'; it is a dictionary.
- that dictionary holds the variable 'myvar'
Solution 3. Through a script
The issue is about identifying the Workspace object containing the variables in which we are interested. For this to work the workspace needs a label as there might be other workspaces on the desktop.
Assuming the label (title) you gave to the workspace is 'myWorkspace' then the following script gives you the bindinges.
windows := Project current world submorphs select: [:sm | sm isKindOf: SystemWindow].
(windows detect: [:w | w label beginsWith: 'myWorkspace']) model inspectBindings
Solution 4. Scripting again, but using hasfocus
The advantage respect to the previous solution is that this one does not care about the name of the Workspace. There can be 10 Workspaces and all called 'Workspace'. It looks at the one who has the focus: the one where you are typing/executing code.
"This is a test variable"
myHelloVar := 'Hello World ... I love classics.'.
"Find all PluggableSystemWindows whose model is 'Workspace' "
v1 := PluggableSystemWindow allInstances select: [ :x | x model class = Workspace] .
"Of those P.S.W. find the one who has focus (in which we are typing).
The submorph at position 1 is a 'PluggableTextMorphPlus', it can answer 'hasKeyboardFocus' message ".
v2 := v1 detect: [ :x | (x submorphs at: 1) hasFocus ] .
"Finally list all variables in the selected Workspace."
(v2 submorphs at: 1) model bindings keys.
Solution 5: Script with using Project current ans hasFocus
currentWorkspaceWindow := ((Project current world submorphs select: [:m | (m isKindOf: SystemWindow)
and: [m model isKindOf: Workspace]])
select: [:ws | ws isLookingFocused]) first.
currentWorkspaceWindow model bindings "a dictionary of workspace shared variables including currentWorkspaceWindow itself"
currentWorkspaceWindow label "the label of the workspace window"
- Exercise 1. The reader may try to print in Transcript all the variables and their values of the current workspace.
- Exercise 2. Create a list of all project names with all workspace titles of workspaces which have shared variables. (Answer)
- Exercise 3. You know that you have a workspace with a particular title somewhere in the image and you want to open the project which has that workspace. (Answer)
If you use the window menu (blue button with white triangle) you can make your workspace inclosable. This is useful if you want to avoid loosing an important workspace by mistake and its variables. Then your workspace shared variables behave like globals. After each start of the image you still have them.
Referring to solution 5 this may be queried by
List the content of all workspaces of all projects in an image.