Simple FAYT script that allows you to create a file or folder. This project is largely my attempt to port @Hurpin's indispensable Better "New Text Document" Script into FAYT
Creates a folder by default. Supports creating nested folders, and files of any type. E.g. including ".ext" creates a file of that type instead. If you wish to write contents to that file, insert a : (followed by the content).
The default activation key is //. After that you just enter the command in the filter bar that pops up. Requires version 13.19.2 to have // as the activation key. If you are on an earlier version, you can change the key in Preferences/Quick Keys.
14/11/25
- I have found that '.' actually works better as the activation key.
Examples:
Create file type
- Name.txt: content. Blah blah blah
Create folder
- Folder name
Create nested folders
- Folder1/Folder2/Folder3
FAYT_Create.opusscriptinstall (1.8 KB)
// J.Fourie
// Create File/Folder FAYT v1.3
// Folder by default, file if one dot, write content after colon. Supports nested folders.
// E.g. //MyFolder/SubFolder
// E.g. //Note.txt: Quick notes go here.
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, "");
};
}
var fsu = DOpus.FSUtil();
function OnInit(initData) {
initData.name = "FAYT:Create";
initData.version = "1.1";
initData.desc = "Quick folder creation by default, file if one dot. Supports :content. Now supports nested folders. Folder does NOT auto-open.";
initData.min_version = "13";
initData.default_enable = true;
}
function OnAddCommands(addCmdData) {
var c = addCmdData.AddCommand();
c.name = "FAYT:Create";
c.method = "OnFAYT";
c.hide = true;
var f = c.fayt;
f.enable = true;
f.key = "//"; // Activation key
f.realtime = true;
f.wantempty = true;
}
function OnFAYT(data) {
if (data.key !== "return") return;
var tab = data.tab;
if (!tab) return;
var cmdline = String(data.cmdline || "").trim();
// Remove leading // if present
if (cmdline.indexOf("//") === 0) {
cmdline = cmdline.slice(2);
}
cmdline = cmdline.trim();
if (!cmdline) return;
// Split name and content at the first colon
var idx = cmdline.indexOf(":");
var rawName = (idx >= 0 ? cmdline.slice(0, idx) : cmdline).trim();
var content = (idx >= 0 ? cmdline.slice(idx + 1) : "").trim();
// Clean path and allow nested folders
rawName = rawName.replace(/[\\:\*\?"<>\|]/g, "").replace(/\//g, "\\");
if (!rawName) rawName = "NewFile";
var parts = rawName.split(".");
var isFile = (parts.length === 2); // one dot only
var baseDir = String(tab.path);
var target = uniqueName(baseDir, rawName, isFile);
if (isFile) {
// Create parent folders if needed
var parent = fsu.NewPath(target).Parent;
if (parent && parent.IsValid && !fsu.Exists(parent)) {
var cmd = DOpus.Create().Command();
cmd.ClearFiles();
cmd.AddLine('CreateFolder NAME="' + String(parent) + '"');
cmd.Run();
}
// Create the file
var fh = fsu.OpenFile(target, "w");
if (fh && fh.error === 0) {
if (content) fh.Write(content);
fh.Close();
DOpus.Output("[Created File] " + target);
} else {
DOpus.Output("[ERROR] Failed to create file: " + target);
}
} else {
var cmd2 = DOpus.Create().Command();
cmd2.RunCommand('CreateFolder NAME="' + target + '"');
DOpus.Output("[Created Folder] " + target);
}
tab.CloseFAYT();
}
function uniqueName(basePath, relPath, isFile) {
var path = fsu.NewPath(basePath);
var name = relPath;
var base = name, ext = "";
var sep = name.lastIndexOf("\\");
var folder = (sep >= 0 ? name.slice(0, sep) : "");
name = (sep >= 0 ? name.slice(sep + 1) : name);
var dot = name.lastIndexOf(".");
if (isFile && dot > 0) {
base = name.slice(0, dot);
ext = name.slice(dot);
} else {
base = name;
}
var sub = fsu.NewPath(basePath);
if (folder) sub.Add(folder);
var full = sub + "\\" + base + ext;
for (var i = 2; fsu.Exists(full); i++) {
full = sub + "\\" + base + " (" + i + ")" + ext;
}
return full;
}