Check out a free preview of the full Electron Fundamentals, v2 course:
The "Creating a New File" Lesson is part of the full, Electron Fundamentals, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Steve demonstrates how to save a named new file to a local file system.

Get Unlimited Access Now

Transcript from the "Creating a New File" Lesson

[00:00:00]
>> Steve Kinney: Now let's try it again, where we make a brand new file, fresh.
>> Steve Kinney: I got an error, you can see my save button wasn't showing up, which means I've got a small error here, let's see.
>> Steve Kinney: setRepresentedFilename, it's not working out for me, I know why. setRepresentedFilename, when a brand new file opened, was undefined.

[00:00:32] Which is not okay for that, so what we'll do is we'll just give ourselves a guard there, we'll say, if filePath, then do it, right? Because if this is undefined, that is not an acceptable value. The same way that fs.writeFileSync and giving it null there wasn't okay, it's also not okay here.

[00:00:57]
>> Steve Kinney: But that's just a change to the render, so we should be able to just refresh, and we'll hit Save File.
>> Steve Kinney: And it looks like nothing happened, but if I go to my desktop, there's that file on the desktop. But again, I kind of showed the problem, it looks like nothing happened, right?

[00:01:24] That's not great, what would be great is, now we have a file, right? We can show it here, we just don't have that code path. So you have a few options here, all of the saving the file happened on the file system right? But, I mean, having the files that happened in the main process, but, we could communicate all that stuff back.

[00:01:48] I'm going to cheat, what I'm going to do is, I've saved a file, I have my open file, it does everything right. So as soon as I save the file, you know what I'm going to do? Just open the file that saved, and everything will just work, famous last words.

[00:02:05]
>> Steve Kinney: So what we'll do is, we will save that, and we'll say openFile with our new file, which is why I kind of broke it out like an hour ago.
>> Steve Kinney: We'll say.
>> Steve Kinney: All right, so we can see that it's edited, let's see what we've got here.

[00:02:41]
>> Steve Kinney: setDocumentEdited.
>> Steve Kinney: Next problem is in my renderer, before we had the issue with setRepresentedFilename. In this case, now it's blowing up on setDocumentEdited because that's expecting true or false. It's undefined because I passed it in as a variable, but when I call it down here, we didn't pass anything in.

[00:03:14] So, if we just open a new file that's not edited.
>> Steve Kinney: Boom, I've got a file name, I've got a represented file, we know whether or not it's been edited, since the last save.
>> Steve Kinney: Back and forth, right? We can open files from the file system. We can update the UI, both in our actual browser-based UI, as well as the native UI.

[00:03:48] Based on that file, we can write files from the file system, so on and so forth.