Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In order to record animations more easily while using Mathematica on Linux, I would need to know how to get the position of three clicks.

The following picture (main part generated by Panel[Graphics@Disk[], ImageSize -> 200]) shows the idea:

enter image description here

Dynamic@MousePositon[] shows the coordinates of the mouse within the Mathematica window but how to save the position of the clicks? (knowing that EventHandler works only in the Output cell).

Then the idea is to get posi={xi,yi} and to record the screen (using byzanz) automatically:

time = 5; delay = 2; path = "test";
toGIF = ToString@StringForm[
  "byzanz-record --duration=`` --x=`` --y=`` --width=`` --height=`` --delay=``",
  time, First@pos1, Last@pos1, Subtract[#, #2] & @@ (First /@ {pos2, pos1}),
  Subtract[#, #2] & @@ (Last /@ {pos3, pos2}), delay] <> path <> ".gif"

Then Run@toGIF would produce the GIF.

share|improve this question
up vote 4 down vote accepted
pos = {};
 NotebookEventActions :> {
   "MouseClicked" :> If[CurrentValue["AltKey"], 
                        AppendTo[pos, MousePosition[]]], 
    PassEventsDown -> True}]


So, while pressing Alt you can click to store your positions.

share|improve this answer

Just to provide a full answer (and thanks Kuba's answer) in case someone wants to screencast while using Mathematica and byzanz (Gnome only):

recordGif::Usage = "Record Mathematica animations; Syntax: recordGif[time,delay]";
recordGif[time_, delay_] :=

  DynamicModule[{pathToGif, pathToSh, toGIF, posGIF},
    Button["Run", Print["Starting in " <> ToString@delay <> " seconds for " <> ToString@time <> " seconds."];
      Run@toGIF[posGIF, pathToGif]],
    Button["Export to a shell script",
      {Export[pathToSh, toGIF[posGIF, pathToGif], "Lines"];
       Run["chmod +x " <> pathToSh];
       Print["Exported to " <> pathToSh];}],
    Dynamic@posGIF}, ItemSize -> 22],
  Initialization :>
    posGIF = {};
    pathToGif = "~/mmagif.gif"; pathToSh = "~/mmash";
    SetOptions[EvaluationNotebook[], NotebookEventActions :>
      {"MouseClicked" :> If[CurrentValue["ControlKey"], 
       AppendTo[posGIF, MousePosition[]]], PassEventsDown -> True}];
    toGIF[posGIF_, path_] := (ToString@StringForm[
      "byzanz-record --duration=`` --x=`` --y=`` --width=`` --height=`` --delay=`` ", 
      time, First@posGIF[[1]], Last@posGIF[[1]], 
      Abs@Subtract[#, #2] & @@ (First /@ {posGIF[[2]], 

      Abs@Subtract[#, #2] & @@ (Last /@ {posGIF[[3]], 
          posGIF[[2]]}), delay] <> path <> " &");

Evaluating recordGif[5,5] as follow shows:

enter image description here

By clicking while pressing Ctrl on the 3 points shown in the question image and by clicking on the Run button you get:

enter image description here

and by clicking on the button Export to a shell script you get in mmash:

byzanz-record --duration=5 --x=253 --y=220 --width=493 --height=324 --delay=5 ~/test.gif &

which you can simply run a terminal: ./mmash.

share|improve this answer
+1 there is somewhere on meta.mathematica a topic about capturing methods. you may want to link this answer to it :) – Kuba May 5 '14 at 19:43
I will do that tomorrow :) If you have any idea how to make the variable posGIF local and not global please feel free to tell me :D I lost patience with that thing :P – Öskå May 5 '14 at 19:47
Move those 3 first lines posGIF = {}; pathToGif =...; SetOptions[... at the beginning of Initialization. Why? posGIF appears in body wrapped with Dynamic, those symbols are forcing definitions from Initialization to evaluate before the rest. – Kuba May 5 '14 at 19:57
Thanks :) That seems quite simple now :) – Öskå May 5 '14 at 20:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.