# Session and commands

Read the selection and content from scripts, change slides, and keep every change in the undo history.

Session commands take the same editing path as the interface: each call is one undoable edit and the interface updates at once. See [Open and save](/en/pptx/files) for getting the session.

## Call commands

```ts
const added = session.commands.addSlide({ after: 0, title: 'Second slide', body: 'Key points' })
```

Each command takes one options object and returns whether it applied. When it does not apply, for example because the slide or shape does not exist, it returns `false` and the deck is unchanged.

Combine several steps into one undo step with `chain()`; if any step does not apply, none of them is kept:

```ts
session.chain()
  .addSlide({ after: 1, title: 'Third slide' })
  .addSlide({ after: 2, title: 'Fourth slide' })
  .run()

await session.undo()   // undoes the whole chain
await session.redo()
```

## Find slides and shapes

Commands address slides by part name (such as `/ppt/slides/slide3.xml`) and shapes by id, which stay valid when slides move.

The shapes the user selected come from `selection()`:

```ts
for (const shape of session.selection()) {
  session.commands.setShapeFill({ slide: shape.slide, shapes: [shape.shape], color: '#2F6FED' })
}
```

To walk every slide, read the current content with the MIT package `@quirekit/office-kit-pptx`:

```ts
import { getShapeId, getShapeName, getShapeText, getSlidePartName, getSlides, getSlideShapes, loadPresentation } from '@quirekit/office-kit-pptx'

const deck = await loadPresentation(await session.snapshot())
for (const slide of getSlides(deck)) {
  const part = getSlidePartName(slide)
  for (const shape of getSlideShapes(slide)) console.log(part, getShapeId(shape), getShapeName(shape), getShapeText(shape))
}
```

## Units

Positions and sizes are in EMU: 1 inch = 914400, 1 cm = 360000, 1 pt = 12700. A 16:9 slide is 12192000 wide and 6858000 high by default. Font sizes are in points and colors are written `#RRGGBB`.

## Common commands

| Use | Commands |
| --- | --- |
| Slides | `addSlide`, `duplicateSlide`, `moveSlide`, `deleteSlide`, `setSlideHidden`, `setSlideNotes`, `setSlideLayout`, `setSlideBackground` |
| Text | `setShapeText`, `replaceShapeText`, `setTextFormat`, `setTextAlignment`, `setTextAnchor`, `setParagraphBullets`, `setLineSpacing` |
| Insert | `addTextBox`, `addShape`, `addPicture`, `addTable`, `addChart`, `addMedia`, `addEquation`, `addFreeform` |
| Shapes | `setShapeBounds`, `moveShapes`, `setShapeFill`, `setShapeOutline`, `rotateShapes`, `flipShapes`, `deleteShape`, `duplicateShapes` |
| Arrange | `arrangeShapes`, `alignShapes`, `distributeShapes`, `groupShapes`, `ungroupShapes` |
| Tables and charts | `setTableCellText`, `setTableStyle`, `setChartData` |
| Transitions and animations | `setSlideTransition`, `addShapeAnimation`, `updateAnimation`, `removeAnimation`, `moveAnimation` |
| Links | `setShapeLink` |

The types list every command and its options: type `session.commands.` in your editor to see them with their descriptions.

```ts
session.commands.addTextBox({
  slide: '/ppt/slides/slide1.xml',
  text: 'Q3 revenue: 210 million',
  x: 914400, y: 4572000, width: 6096000, height: 685800,
})
```
