Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

I have problems making Button evaluate an input like this one:

Button["Load", Get["/Users/simonlausen/Desktop/Input/ex1.mx"]]

I have tried a bunch of things, but haven't found any elegant solution. What to do?

share|improve this question

marked as duplicate by Sjoerd C. de Vries, Artes, m_goldberg, rm -rf Apr 3 '13 at 3:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
Button has a time-out of 5 seconds. Add the options Method-> "Queued" to remove the time-out. (default is "Preemptive"). It's documented. –  andre Apr 1 '13 at 22:36
    
Method -> "Queued" is almost always the answer to questions about buttons that don't work as expected when the button action requiers Mathematica to do something that takes some time. I have posted a list of some of the previous similar question here. This question might be considered a duplicate on any of these. –  m_goldberg Apr 1 '13 at 23:09

2 Answers 2

One can see that the Get operation does work, but simple output is suppressed:

Export["testfile.m", "Print[2+2]; Sin[1]", "String"]

Button["Load", << "testfile.m"]

When the button is clicked 4 is printed, but Sin[1] is not.

This is the same as: Button["Go", Sin[1]] which also does not print when the button is clicked.

If Get is used directly:

<< "testfile.m"

4

Sin[1]

At the moment I cannot recall how to get the plain output printed to the evaluation Notebook.

EDIT: It appears there is no simple way:

Can Button[] generate output without using Print[]?

share|improve this answer

I think the answer is that the main link has to evaluate the Get call. Reusing Mr.Wizards test:

Export["testfile.m", "Print[2+2]; f[x_]:=Sin[x]", "String"]
Button["Load", Get["testfile.m"], Method -> "Queued"]

works for me. For an explanation what I mean with main link you could check the chat log. I don't know whether it is good practice to include links of this here.

http://chat.stackexchange.com/transcript/message/8608385#8608385

share|improve this answer
    
Method -> "Queued" does not help with the example I gave. It may help when loading a package takes longer than the time-out, but I'm not sure if that is the problem the OP is having or if it is more akin to this. –  Mr.Wizard Apr 1 '13 at 23:15
1  
That chat discussion should be part of a canonical answer! Don't we have a question that requires explicitly this? –  István Zachar Apr 2 '13 at 14:08

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