I am writting with the hope of getting some help when using "SystemOpen".

Here is a simple example:

dir = "C:\\test2\\";
batFile  = StringJoin[dir, "test", ".bat"];    
txtFile = StringJoin[dir, "test", ".txt"]

  , StringJoin["echo %date% %time%>", dir, "log.txt"]
  , "Text"

Button["Press to Run"
  , Export[txtFile, DateString[]]
  ; SystemOpen[ batFile  ]
  ; Pause[5]
  ; test = Import[txtFile, "Text"]
  ; Export[
      , StringJoin[test, "\n", DateString[]]
  • First line -> we define the working directory.

  • Second line -> We create a .bat file in the selected directory. When running it will print the current time in a log.txt file in the same directory.

  • Third line -> We create a button.

    When we press the button:

    • it exports the initial time in a "test.txt" file.
    • Then it should run the .bat file and save the same time.
    • Then it should pause for 5 seconds.
    • Finally, it will read the initial time in "test.txt" and
    • append a new line with the new current time.

We should see that the log.txt file has the initial time. On the other hand, it has the ending one!!!

Mathematica clearly does all the operations and leaves SystemOpen for the end (I tried other ideas such as loops or similar, always to find that SystemOpen executes at the end!!!).

Is this a bug? can it be solved? I really want to use SystemOpen as I know that it works like charm except for this issue.

Thanks to george2079 (SystemOpen), he realized that this happens when using a Button.

For example:

    Export[txtFile, DateString[]]
  ; SystemOpen[batFile]
  ; Pause[5]
  ; test = Import[txtFile, "Text"]
  ; Export[
      , StringJoin[test, "\n", DateString[]]
  , {i, 1, 1}

will work fine. My program has several menus and buttons and I need that button. Does someone know how to bypass this problem using "systemopen"?

Thanks in advance.

  • 3
    $\begingroup$ You don't have the probleme if you add the option Method-> "Queued" to the Button. (the default is "Preemptive"). $\endgroup$
    – andre314
    Commented May 25, 2017 at 17:44
  • $\begingroup$ You seem to think Export will append content to an already existing file. It doesn't. It replaces the content of a preexisting file with the new content. $\endgroup$
    – m_goldberg
    Commented May 25, 2017 at 17:50
  • 1
    $\begingroup$ @andre that does fix things, but does it actually make sense? $\endgroup$
    – george2079
    Commented May 25, 2017 at 18:56
  • $\begingroup$ @george2079 I can' t say if that really make sense. I have just tested Method -> "Queued" to see if that had an effect, and it turned out that the problem disappeared. The ideas I had in mind were A) Method ->"Preemptive" is thought to execute a piece code quickly (Timeout= 5 second) so it don't care of what's happening when it launches a system-process (though even with Method-> ""Queued" it is done asynchronously ). $\endgroup$
    – andre314
    Commented May 25, 2017 at 20:44
  • $\begingroup$ As I supposed that theses processes timing are unpredictable, it is possible that the system process start with a delay (maybe when Mathmetica has ended a piece of code) B) Many questions comes from that the OPs are not aware of the option Method->"Queued" Maybe pure speculation.I'm not a expert at all. $\endgroup$
    – andre314
    Commented May 25, 2017 at 20:45

1 Answer 1


You need Method -> "Queued" for a Button, the reason is the following:

  • communication through a preemptive link blocks the FrontEnd

    No dynamic updates, not any UI interaction is possible till it is finished.

  • SystemOpen needs FrontEnd to finish

    At the end it is the FrontEnd which does the final step, take a look:

    << GeneralUtilities`
    PrintDefinitions @ SystemOpen

    It boils down to the line with:

    MathLink`CalFrontEnd @ FrontEnd`SystemOpen

    And this step is delayed till your code finishes.

Further reading:

Evaluation order of Button[]'s action

John Fultz about "Preemptive" method

Working with Dynamic: Synchronization with EventActions ( and other preemptive evaluations)

Working with ScheduledTasks inside DynamicModules

Button with progress bar

AdvancedDynamicFunctionality / Synchronous versus Asynchronous Dynamic Evaluations


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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