2
$\begingroup$

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.

enter image description here

$\endgroup$
2
  • $\begingroup$ Define a function that does that? $\endgroup$ – MarcoB Dec 29 '20 at 1:47
  • $\begingroup$ @MarcoB : How to define and use such a function? $\endgroup$ – Iosif Pinelis Dec 29 '20 at 4:34
1
$\begingroup$

Try:

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

enter image description here

$\endgroup$
1
  • $\begingroup$ 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]}) $\endgroup$ – Iosif Pinelis Jan 1 at 18:02
3
$\begingroup$

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.

$\endgroup$
7
  • $\begingroup$ 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. $\endgroup$ – Iosif Pinelis Dec 30 '20 at 14:33
  • $\begingroup$ 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. $\endgroup$ – Daniel Huber Dec 30 '20 at 17:03
  • $\begingroup$ 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. $\endgroup$ – Iosif Pinelis Dec 30 '20 at 21:31
  • $\begingroup$ 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. $\endgroup$ – Daniel Huber Dec 31 '20 at 8:54
  • $\begingroup$ 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? $\endgroup$ – Iosif Pinelis Dec 31 '20 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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