Scripting 4.2. Removing objects

In §4.1 we saw that objects could be removed by selecting them first and then calling the Remove command. A faster way is the removeObject function, which can also remove unselected objects:

`sound = Create Sound as pure tone: “sine377”,
` `... 1, 0, 1, 44100, 377, 0.2, 0.01, 0.01 ; remember the ID of the Sound
` `Play ; the Sound is selected, so it plays
` `spectrum = To Spectrum: “yes” ; remember the ID of the Spectrum
` `Draw: 0, 5000, 20, 80, “yes” ; the Spectrum is selected, so it is drawn
` `# Remove the created Spectrum and Sound:
` `removeObject: sound, spectrum ; remove one selected and one unselected object
`

The removeObject function keeps the objects selected that were selected before (except of course the ones it throws away). This allows you to easily throw away objects as soon as you no longer need them:

`sound = Create Sound as pure tone: “sine377”,
` `... 1, 0, 1, 44100, 377, 0.2, 0.01, 0.01 ; remember the ID of the Sound
` `Play ; the Sound is selected, so it plays
` `spectrum = To Spectrum: “yes”
` `removeObject: sound ; we no longer need the Sound, so we remove it
` `Draw: 0, 5000, 20, 80, “yes” ; the Spectrum is still selected, so it is drawn
` `removeObject: spectrum ; remove the last object created by the script
`

## Selecting and removing all objects from the list (don’t)

A very strange command, which you should not normally use, is `select all`:

`select all
` `Remove
`

This selects all objects in the list and then removes them. Please try not to use this, because it will remove even the objects that your script did not create! After all, you don’t want the users of your script to lose the objects they created! So please try to remove in your script only the objects that your script created, even if the script is for your own use (because if it is a nice script, others will want to use it).

### Links to this page

- Scripting
- Scripting 4. Object selection