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

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

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

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.

  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?

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.

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.

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.

Thanks. I did as you said:

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

Did I forget to set something?

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

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

RunOnOpen.nb (1.79 KB)

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.

Yes. It runs automatically but it has a reminder.