Skip to content
In development: Molkit Script and the AI chat are not available to accounts yet. These pages describe work in progress and may change.

Your first script

In this tutorial you build a figure from an empty canvas: three molecules, each with its 3D shape, captioned and laid out, with a title. Then you build a second, short figure: hydrogen chloride giving its proton to water, with curved arrows between parts of the molecules.

Each run adds to what is already on the canvas, and a name can be used only once in a document. If you run draw water a second time, the run stops with E-TAKEN (“water already exists”). So when a step runs the same molecules again, it tells you to clear the canvas first: click the canvas, press Ctrl+A, then press Delete. You can also press Ctrl+Z on the canvas to take back the whole last run.

  1. Open the console. Press Ctrl+` (Cmd+` on a Mac), or click Molkit Script in the tool palette. Clear the example in the box: select all of it and delete it.

  2. Draw one molecule. Type this line and press Ctrl+Enter:

    draw water view: lewis

    Water appears as a Lewis structure, named water, and the log says what the line did. view: lewis asks for the Lewis view; bondline is the other view that draw makes.

  3. Make its shape. derive makes other views of a molecule. Replace the script with these two lines and run them:

    draw ammonia view: lewis
    derive ammonia views: [vsepr]

    Ammonia appears next to the water from step 2, and its VSEPR copy, named ammonia-vsepr, appears to its right.

  4. Do the same for several molecules with a loop. Replace the script with this one, clear the canvas, and run it:

    foreach m in [water, ammonia, methane] {
    draw m view: lewis caption: title
    derive m views: [vsepr]
    }

    m takes each name in turn, and caption: title writes each molecule’s name under it. Water is planar, so its Lewis drawing already shows its VSEPR shape: Molkit makes no copy, and the log says so.

  5. Keep each pair together in a set. A set holds a molecule and its views, and moves as one block. Replace the script with this one, clear the canvas, and run it:

    foreach m in [water, ammonia, methane] {
    draw m view: lewis into: set "{m.title}"
    derive m views: [vsepr] into: set "{m.title}"
    }
    arrange [set "water", set "ammonia", set "methane"] as: column

    "{m.title}" builds each set’s name from the molecule’s title, and arrange stacks the three sets in a column.

  6. Add a title. Placement options put a new object relative to others, and all means the box around everything. Add this line at the end of the script, clear the canvas, and run the whole script again:

    write "Lone pairs and shape" size: 14 pt style: bold above: all align: left
  7. Point at a part of a molecule. A dot path names a piece of a molecule: water.O is its oxygen, water.bond(O, H1) is a bond, and water.O.lone-pair1 is a lone pair. Replace the script with this one, clear the canvas, and run it:

    draw hydrogen-chloride view: lewis
    draw water view: lewis right-of: hydrogen-chloride gap: 1.5 bond
    rotate water by: 45 deg
    describe water

    Water goes to the right of HCl, one and a half bond lengths away, and is turned so that one of its lone pairs faces the HCl.

    The describe line lists the parts of water and where each lone pair sits now: O (lone-pair1 top, lone-pair2 left), H1, H2. So lone-pair2 is the pair that faces the H of HCl. Before the turn, the pairs sat top-right and top-left: a lone pair keeps its number when the molecule turns.

  8. Push the electrons with curved arrows. An arrow from a part to a part is a curved mechanism arrow, attached at both ends. Replace the describe water line with two arrows, clear the canvas, and run the script:

    draw hydrogen-chloride view: lewis
    draw water view: lewis right-of: hydrogen-chloride gap: 1.5 bond
    rotate water by: 45 deg
    arrow from: water.O.lone-pair2 to: hydrogen-chloride.H
    arrow from: hydrogen-chloride.bond(H, Cl) to: hydrogen-chloride.Cl

    The first arrow starts at the lone pair of water and ends at the H that the pair bonds to. The second starts at the H–Cl bond and ends at the Cl, which keeps that pair. An arrow starts where the electrons are (a lone pair or a bond) and ends where they go (an atom or a bond).

    Hydrogen chloride on the left and water on the right, drawn as Lewis structures. A curved arrow runs from a lone pair on the oxygen of water to the hydrogen of HCl, and a short hooked arrow runs from the H–Cl bond to the chlorine.
    What the script draws: water’s lone pair goes to the H of HCl, and the H–Cl bond’s pair goes to the Cl.

    The arrows move with the molecules when you drag them. If an arrow bulges the wrong way, add bow: left or bow: right; bow: bigger or bow: smaller changes its size. The rules are on the language page under Curved arrows.

  9. Check before you run. Make a typo on purpose: type draw water veiw: lewis and click Check. Nothing runs. The problem is underlined at the word, with a fix: “Did you mean: view:?”. Correct the typo and click Check again; the log says “no problems”.

  10. See what is there. Run this line:

    describe

    The log lists every object by name, with what it is and where it is, then the document’s settings and what you have selected.

  11. Save the script. Save… writes a .mkscript file, and Open… (or dropping the file on the console) brings it back. The same script makes the same figure in any document.