«SCD/TXT File Merger GUI (select or drag a folder)» by prko

on 02 Mar'26 07:29 in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
(
var mergeFiles, window, selectButton, dragArea, displayText, extField, extFieldInitialString, mergeButton, selectedFolder;

mergeFiles = { |currentDir, extension|
	var outputFileName, sourceFiles;
	
	if (currentDir.isNil or: { extension.isNil or: { extension.isEmpty } }) {
		"Error: No folder or extension provided.".warn;
	} {
		outputFileName = "_merged_." ++ extension;
		
		sourceFiles = (currentDir +/+ "*." ++ extension).pathMatch.select { |path|
			path.basename != outputFileName
		}.sort;
		
		if (sourceFiles.isEmpty) {
			("No ." ++ extension ++ " files found in: " ++ currentDir).postln;
		} {
			("Number of ." ++ extension ++ " files found: " ++ sourceFiles.size).postln;
			
			File.use(currentDir +/+ outputFileName, "w", { |outFile|
				sourceFiles.do { |path|
					var fileName = path.basename;
					try {
						outFile.write("\n/* " ++ fileName ++ " */\n\n" ++ File.readAllString(path) ++ "\n\n");
						("Appended: " ++ fileName).postln;
					} {
						("Warning: Could not read " ++ fileName).warn;
					};
				};
				("Success! All files merged into: " ++ outputFileName).postln;
			});
		};
	};
};

window = Window("File Merger",
	Rect.aboutPoint(Window.screenBounds.center, 200, 50)).front;

// 1a. Select a folder
selectButton = Button()
.states_([["1a. Select a folder"]])
.action_ {
	FileDialog({ |path|
		selectedFolder = path[0];
		displayText.string = "Selected Folder: " ++ selectedFolder;
	}, fileMode: 2);
};

// 1b. Drag a folder
dragArea = DragSink()
.string_("1b. Drag a folder")
.align_(\center)
.background_(Color.gray(0.8))
.receiveDragHandler_ {
	var path = View.currentDrag;
	if (path.isKindOf(String)
		and: { File.exists(path) }
		and: { File.type(path) == \directory }
	) {
		selectedFolder = path;
		displayText.string = "Selected Folder: " ++ selectedFolder;
	} {
		"Invalid folder dragged.".warn;
	};
};

// Middle: display selected folder
displayText = StaticText().align_(\center).string_("Selected Folder: None");

// 2. Extension input
extFieldInitialString = "2. File Extension (txt or scd) to Merge";
extField = TextField()
.string_(extFieldInitialString)
.stringColor_(Color.gray)
.focusGainedAction_ {
	if (extField.string == extFieldInitialString) {
		extField.string_("").stringColor_(Color.black);
	};
}
.focusLostAction_ {
	if (extField.string.trim.isEmpty) {
		extField.string_(extFieldInitialString).stringColor_(Color.gray);
	};
};

// 3. Merge
mergeButton = Button()
.states_([["3. Merge"]])
.action_ {
	var extension = extField.string.trim;
	if (extension == extFieldInitialString) { extension = "" };
	mergeFiles.(selectedFolder, extension);
};

window.layout = VLayout(
	HLayout(selectButton, dragArea),
	displayText,
	HLayout(extField, mergeButton)
);

window.onClose = { "Window closed.".postln };
)
raw 2872 chars (focus & ctrl+a+c to copy)
reception
comments