Best way to create and name a text file and enter date and arbitrary text inside it?

I frequently create readme.txt files in directories at work. Inside them, I run an AutoHotkey shortcut to enter the day's date and then I type in my last name. Something like this:

17 July 2018 Lastname:

I'm looking for a way to do all this stuff with one click in Opus, so I can then just enter my note in the opened text file — very streamlined. The steps I want to automate are:

  1. Create text file named readme.txt in directory of active lister.
  2. Open readme.txt in text editor associated with .txt file extension.
  3. Enter date and arbitrary string (my last name) and colon. Probably skip Autohotkey here because I want my initial button press or hotkey to make all this happen. Or perhaps I can have Opus send a hotkey to AutoHotkey at this point?
  4. Put input focus after a space after the colon.

Any suggestions?

Thanks!

This will do most of that, opening the file in your default text editor:

Function type: MS-DOS Batch Function

cd {sourcepath}
echo {date|dd MMMM yyyy} Lastname:>>readme.txt
start "" readme.txt

If the text editor you're using has a way to tell it to put the cursor at the end of the line via the command-line when opening the file, that'd be the best way to do things. I don't know if many editors allow that, though.

You could do similar by using JScript instead and sending a keypress to the editor after it opens, but you may need to play around with the amount of time to wait before sending the keypress. 100ms seemed to work for me, but if the keypress is missed or goes to the wrong place, try increasing it.

Stuff you might want to change is at the top of the script, in the three highlighted lines.

Function type: Script Function, Script Type: JScript

function OnClick(clickData)
{
	var lastname = "Lastname";
	var filename = "readme.txt"
	var delayMS = 100;

	var sourcepath = DOpus.FSUtil.Resolve(clickData.func.sourcetab.path);
	var filepath = sourcepath + "\\" + filename;

	var dateNow = DOpus.Create.Date();

	var fso = new ActiveXObject("Scripting.FileSystemObject");
	var textFile = fso.CreateTextFile(filepath, true);
	textFile.WriteLine(dateNow.Format("D#dd MMMM yyyy") + " " + lastname + ":");
	textFile.Close();

	var cmd = clickData.func.command;
	cmd.ClearFiles();
	cmd.RunCommand(filepath);

	DOpus.Delay(delayMS);

	var wsh = new ActiveXObject("WScript.Shell");
	wsh.SendKeys("{End}");
}

One difference between the two: The simple first one will append the line to the file if it already exists, which is a bit safer. The JScript second one will obliterate any existing file. You could modify the script to check if the file already exists first if you want to avoid that, using DOpus.FSUtil.Exists.

1 Like

Leo, are you the hardest-working guy in the business, or what? Thanks for your big, caring, custom-tailored response!

It wasn't until I'd posted this and walked away from it that I started to wonder about whether to test for an existing file of that name — and weather to bail out or append to it. If it already exists, I'd like to put a blank line at its end and append this stuff after that blank line.

I'll play around with your JScript solution.

The JScript solution works. I had some trouble with the wait timing with Notepad and Notepad++, but it was my error.

The problem was that if I tested the script using {> Run (F5)} from the Command Editor box, my text editor would lose focus and not receive the {End} input.

Ah, I ran into that as well but forgot to mention it. Sorry about that. Yeah, the SendKeys part only works when run as a normal button, not from the editor.

No worries. I took your JScript and made it open the existing readme.txt file if it already exists.

I may later make it append a new entry onto an existing readme.txt file, but I don't know where to start. If DuckDuckGo doesn't help me there, I may create a post here about it. But it's a JScript thing — not an Opus thing.

1 Like

You can use fso.OpenTextFile(...) to open an existing text file and append to the end.