How Do I ... : Write Smalltalk scripts : Add a script : Example: Write a script
Example: Write a script
Tip: Keep the script typeTip: Keep the script typeNo tip for this topicExample: Connect the scriptExample: Connect the scriptExample: Connect the scriptExample: Connect the script
Suppose you want DirectoryView to display the contents of a file directory. To do this, it needs a script that does the following:
Sees if the directory specified in the text field is empty
If the directory is empty, it does nothing
If the directory has files, it displays the file names in the multiple selection list
Adding a pool dictionary  
This script uses information in the pool dictionary CfsConstants of the Smalltalk class CfsFileDescriptor. So the script can use the information, change the definition for the DirectoryView class as follows:
1. Click on the Script Editor icon in the lower-right of a Composition Editor open on DirectoryView. A Script Editor opens.
2. In the class definition shown, add CfsConstants to the statement for poolDictionaries:. After you add CfsConstants, the definition for the class DirectoryView reads as follows:
AbtAppBldrView subclass: #DirectoryView
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: 'CfsConstants'
3. Click mouse button 2 on the lower pane. (For UNIX, use mouse button 3.) Then, select Save from the displayed pop-up menu.
Adding the script  
Next, add the script:
1. In the Script Editor, select Add from the Categories menu.
2. In the displayed prompter, type a name that will help you identify the script you're adding, such as Directory example, and select OK.
3. Click on New Method Template icon, the New Method Template icon in the upper-left of the editor. The following template appears in the lower pane:
messagePattern
"comment"
| temporaries |
statements
4. Replace the template with the following code:
getFilesFromDir: aDir to: aCollection
"Instance, public - Get file names from the specified directory
and list the names in the list box. Mode information comes
the CfsConstants pool dictionary. 'slash' defines a \ for
Windows. For UNIX, 'slash' defines a /."
| dd entry slash |
aCollection removeAll: aCollection.
(aDir == nil or: [aDir isEmpty]) ifTrue: [^self].
dd := CfsDirectoryDescriptor opendir: aDir
pattern: '*.*'
mode: FREG | FDIR | FSPECIAL.
(dd isCfsError) ifTrue: [^self].
entry := dd readdir.
slash := CfsDirectoryDescriptor pathSeparator asString.
[entry == nil] whileFalse: [
aCollection add: (aDir,slash,(entry dName)).
entry := dd readdir.].
(self subpartNamed: 'Multiple Select List1') items: aCollection.
5. Click mouse button 2 on the lower pane. (For UNIX, use mouse button 3.) Then, select Save from the displayed pop-up menu. The name of the script, getFilesFromDir:to:, appears in the upper-right pane.
If Undefined or a similar phrase is shown, ensure that you've typed in the code given above.
You can copy code from this help panel to the clipboard by selecting Copy from the Edit menu of the browser window. Then paste it into the Script Editor by selecting Paste from the pop-up menu of the lower pane. (After you delete all text except for the script, pop up the menu and select Save.)
Go to Example: Connect the script to see what connections you make to the script to run DirectoryView.
Last modified date: 08/09/2019