Help - Programming - Screengrab

Previous - Programming - Next

This section shows how to extend Squeak with a simple utility for grabbing a part of the screen in Squeak and saving it as a gif-file, which you then can load into Squeak.

To a large extent, programming in Squeak (and Smalltalk) consists in extending and reusing existing code. If you use Worldmenu-do... you find a line saying Utilities grabScreenAndSaveOnDisk.

Here is the code for it

Browser fullOnClass: Utilities. "Cmd-b"

If you find grabScreenAndSaveOnDisk (under graphical support) you'll find this

grabScreenAndSaveOnDisk "Utilities grabScreenAndSaveOnDisk"
| form fileName |
form _ Form fromUser.
form bits size = 0 ifTrue: [^ self beep].
fileName _ FileDirectory default nextNameFor: 'Squeak' extension: 'gif'.
Utilities informUser: 'Writing ' , fileName
during: [GIFReadWriter putForm: form onFileNamed: fileName].

If you try this, you find the parts you grab get saved as Squeak.1.gif, Squeak.2.gif etc. Indeed, if you do Cmd-b on the following this will happen in Squeak - it changes the cursor by which you now can grab. If you click directly it does nothing (as instructed in the line: form bits size = 0 ifTrue: [^ self beep].)

Try it:

Utilities grabScreenAndSaveOnDisk


(1) It would be nice to supply your own name for the gifs you make
(2) It would be nice to have this on screen with a button.

Well, here is how. Put your cursor in the editing pane of grabScreenAndSaveOnDisk, and copy and paste the following lines in it, and do Cmd-s to have the code accepted by Squeak (and compiled to bytecode)


" Utilities grabScreenAndSaveOnDiskAs
Derived from grabScreenAndSaveOnDisk, to enable the user to provide his own name for the GIF "

| form fileName username |
form _ Form fromUser.
form bits size = 0 ifTrue: [^ self beep].
username := FillInTheBlankMorph request: 'GIF is going to be called: ' initialAnswer: 'Squeak'.
fileName _ FileDirectory default nextNameFor: username extension: 'gif'.
Utilities informUser: 'Writing ' , fileName
during: [GIFReadWriter putForm: form onFileNamed: fileName].

This code will now be part of your Squeak image, be part of the Browsers etc. Indeed, it is just like any other code in Squeak. It is added to Squeak. And now you can do

Utilities grabScreenAndSaveOnDiskAs

and see it work.

Incidentally: What's the reason the name is as long as it is? In Smalltalk the convention is to write long descriptive terms, with each next word written as a capital letter. Spaces and underscores are not permitted here. If you want to have shorter names, use an extra variable for it and make that short.

If you compare the two pieces of code, you see that I have in fact added little:

- added a variable username
- added a line for it that pops up a dialogue. (Do Cmd-b on FillInTheBlankMorph to check it out.
The code I used is on the class side, as the phrase go: Under class in the Browser.)
- changed the line in fileName to use that username

The rest of the code remains the same. I have just added what I needed. The code will store grabbed screenparts by the name supplied with a number, or as before by 'Squeak', as the user pleases.

Next, we wanted to start this from the screen.

Pop up the Supplies Flap, and put a Button 'Press Me' on the screen. Now do this:

(1) Use the halo for its menu and the option change label to change its name to Grabber.
(2) Click on the greenish halo next to Button to pop up window that belongs to eToys and that contains code like this

^ self

(3) Replace self by Utilities grabScreenAndSaveOnDiskAs and do Cmd-s to have the code accepted.
(4) Press on the collapse window.

Task accomplished!

Now you have made a button which when pressed will let you grab arbitrary parts of your screen and save them on disk under the name you supply. Using a FileList  with open on the gifs name will put the gif on your screen. By the way: If you check out Worldmenu - new morph, you find a number of ready made alternatives, that can be used as explained in the files that follow this one.

By the way: This is a nice way to combine Paintings you made, or indeed anything else: You put them on the screen in a pleasing way and grab both of them.

Previous - Programming - Next