Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.7K Views
|
11 Replies
|
4 Total Likes
View groups...
Share
Share this post:

How to make these codes run automatically when the notebook is opened?

Posted 2 years ago

Hello, I am curious about that is there a way to make codes run automatically when the notebook is opened. For example:

DialogInput[
  Grid[{{"InputFile", 
     FileNameSetter[Dynamic[a], 
      "Open", {"file" -> {"*.docx"}}]}, {"OutputFile", 
     FileNameSetter[Dynamic[b], "Directory"]}, {DefaultButton[]}}]];

How to make these run automatically when the notebook is opened? Thanks

POSTED BY: Zhenyu Zeng
11 Replies
Posted 2 years ago

Not sure. I have attached the notebook I used for testing. What happens when you open it?

Attachments:
POSTED BY: Rohit Namjoshi
Posted 2 years ago

Yes. It runs automatically but it has a reminder. enter image description here

POSTED BY: Zhenyu Zeng
Posted 2 years ago

Take a look at this post on MSE for some ideas.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

Hello. Thanks for your reply. I check this post, but I see this is about how to change settings. If another one open the notebook from a new PC, it can't be run automatically.

POSTED BY: Zhenyu Zeng
Posted 2 years ago
  1. Create a new notebook
  2. Evaluate

    SetOptions[EvaluationNotebook[],
    InitializationCellEvaluation -> True, 
    InitializationCellWarning -> False]
    
  3. Save the notebook
  4. Open the notebook in a text editor
  5. Those options are saved in the notebook, not on your system

$FrontEnd options are saved on your system. Don't know if there is a workaround for that.

What exactly are you trying to achieve? Why do you need code to be evaluated when the notebook is opened?

POSTED BY: Rohit Namjoshi
Posted 2 years ago

Hello, How to open the note book in a text editor? I tested these:

SetOptions[EvaluationNotebook[], InitializationCellEvaluation -> True,
  InitializationCellWarning -> False]

hhh = 31

But when I opened this notebook a second time, it didn't run automatically. What I want to do is to make an interface by which another one can choose and then get the results. As Mathematica can't make codes to an executable programme, so I want to make it run automatically when someone open it so as an executable programme.

POSTED BY: Zhenyu Zeng
Posted 2 years ago

Replace hhh = 31 with

Plot[Sin[x], {x, 0, Pi}]

Mark the cell as an Initialization cell (Control 8 on a Mac) or from the menu Cell / Cell Properties / Initalization Cell.

Save the notebook, close it, reopen it. You should see a dialog asking if initialization cells should be evaluates. Answer Yes.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

Thanks. I did as you said: enter image description here

But I opened it, it did nothing. I clicked it and press Ctrl + Enter and it asked me : enter image description here

Did I forget to set something?

POSTED BY: Zhenyu Zeng
Posted 2 years ago

How to open the note book in a text editor?

What editor do you use? Sublime, vim, Emacs, TextEdit, Notepad (on Windows), ... ? Any of them can open the notebook as a text file.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

May you tell me why to open the notebook in a text editor? For what?

POSTED BY: Zhenyu Zeng
Posted 2 years ago

To answer your earlier concern that the options would be saved on your system so if you shared the notebook with someone else they would not take effect. Please read my answer above with the 5 steps to demonstrate that the options are saved in the notebook, not on your system.

POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard