Quickly invoking a certain set of commands

Say I want to be able, in an arbitrary notebook, to quickly invoke a cell containing the commands

NotebookFileName[]
DateString[]


What are good ways to get to be able to do this?

I have tried to create a palette for this purpose (see the image of it below), but, when I click on the palette, nothing happens. I guess I created a wrong kind of pallette.

• Define a function that does that? Dec 29, 2020 at 1:47
• @MarcoB : How to define and use such a function? Dec 29, 2020 at 4:34

Try:

CreatePalette[
Button["Filename and Date",
str1 = NotebookFileName[SelectedNotebook[]]; str2 = DateString[];
NotebookWrite[
InputNotebook[], {Cell[str1, "Content"], Cell[str2, "Content"]}]
]
]


• Thank you very much; upvoted both of your answers. (I have only increased the font size: {Cell[str1, "Content", FontSize -> 15], Cell[str2, "Content", FontSize -> 15]}) Jan 1, 2021 at 18:02

The following code will create a palette and print the file name and date:

CreatePalette[{Button["Filename and Date",
NotebookWrite[
InputNotebook[], {NotebookFileName[SelectedNotebook[]],
DateString[]}]]}]


But note, that this will only work if the input notebook has a name. A new notebook needs to be saved before it acquires a name. Otherwise you will get an error message.

• Thank you very much! Just one little thing: Can I get the results formatted nicely, say without the extra spaces in the pathname? Sorry for my lack of proficiency in Mathematica. Dec 30, 2020 at 14:33
• Sorry, I do not see an extra space? Is it in your path or file name? However, NotebookFileName[SelectedNotebook[]] is an ordinary string and can be formatted accordingly. Dec 30, 2020 at 17:03
• This is what I get after pressing the palette button, with spaces: u.pcloud.link/publink/… There are no spaces in the pathname. Sorry, I don't know how to modify your code to get rid of the spaces and maybe get a better formatting otherwise. Dec 30, 2020 at 21:31
• I see one space between 2020 and 16. Is this what you mean? Concerning formatting, you could say: str=NotebookFileName[SelectedNotebook[]]; or str=NotebookFileName[SelectedNotebook[]] and then format str as you wish. Dec 31, 2020 at 8:54
• Thank you for your response, but I am sorry for my total ineptitude in Mathematica. I have tried this: u.pcloud.link/publink/… but only got this after pressing the button: u.pcloud.link/publink/… . Could you please just show me the entire modified code that would produce the pathname and date in plain text format? Dec 31, 2020 at 17:27