// title: SCD/TXT File Merger GUI (select or drag a folder) // author: prko // description: // code: ( 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 }; )