Copy & Save '.mid file' to 'folder' || Help.
-
I have a panel for drop/loading .mid files. On 'drop' I want to be able to copy & save a duplicate version of that file into the AppData folder.
Does anyone have a snippet you can kindly share?
-
C Chazrox marked this topic as a question
-
@Chazrox
File.copy()
;) -
@d-healey I need to understand something. Docs says that File.copy(var target) target == the NEW file? So that would be the new name? Then just throw that into a variable and File.move() to directory?
-
@Chazrox Pass in a File object.
For example
inline function copyMidiFile(original) { local filename = original.toString(original.Filename); local newFile = FileSystem.getFolder(FileSystem.AppData).getChildFile(filename); original.copy(newFile); }
-
@d-healey Thank you! I'll try this now.
-
@d-healey no errors but its not working..
can you spot it?original = FileSystem.fromAbsolutePath(full); // full is the full filepath name from the file dropped. if (original == undefined) { } else { local filename; filename = original.toString(original.Filename); local appDataDir; appDataDir = FileSystem.getFolder(FileSystem.AppData); local newFile; newFile = appDataDir.getChildFile(filename); original.copy(newFile); original.move(appDataDir); Console.print("Saved copy to AppData: " + newFile.toString(newFile.FullPath)); }
-
In what way is it not working?
// Why the empty if statement? if (original == undefined) { }
// Each of these can be one line local filename; filename = original.toString(original.Filename); local appDataDir; appDataDir = FileSystem.getFolder(FileSystem.AppData); local newFile; newFile = appDataDir.getChildFile(filename);
// Why are you copying then overwriting it with the original? original.copy(newFile); original.move(appDataDir);
-
Why the empty if statement?
I had something there but deleted it. I just removed it.
Why are you copying then overwriting it with the original?
I dont know what Im doing lol. I was just trying things because it wasnt working.
@d-healey said in Copy & Save '.mid file' to 'folder' || Example?:
In what way is it not working?
When I drop the .mid file on my panel, the rest of the script executes but this part of it doesnt. I dont see any files in AppData or any other directory that I test.
-
@Chazrox said in Copy & Save '.mid file' to 'folder' || Example?:
When I drop the .mid file on my panel, the rest of the script executes but this part of it doesnt. I dont see any files in AppData or any other directory that I test.
Can you show me where you are calling your function?
-
@d-healey complete mess at the moment but have a look.
inline function onpnlMIDIFilesFileDrop_B(obj) { // Maintain hover state for paint() local prevHover; prevHover = this.data.hover ? 1 : 0; this.data.hover = (obj.hover && !obj.drop) ? 1 : 0; if ((this.data.hover ? 1 : 0) != prevHover) this.repaint(); // Hover hint if (obj.hover && !obj.drop) { this.data.text = "Drop to load!"; this.data.dropped = false; this.changed(); return; } if (!obj.drop || obj.fileName == undefined) return; // Parse basename (no path, no extension) local full; full = obj.fileName; local slash; slash = Math.max(full.lastIndexOf("/"), full.lastIndexOf("\\")); local dot; dot = full.lastIndexOf("."); if (dot < 0) dot = full.length; local base; base = full.substring(slash + 1, dot); this.data.filename = base; // paint() this.data.fileName = base; // sync // Hide the slider while bounds/values are inconsistent RangeSlider.set("visible", 0); // Load into MIDIPlayer1 (track 1) MIDIPlayer1.setFile(full, true, true); if (MIDIPlayer1.setTrack != undefined) MIDIPlayer1.setTrack(1); // Start polling until the events are actually available midiRangeTimer.stopTimer(); midiRangeTimer.startTimer(50); // ----------------------------------------------------------- // COPY DROPPED FILE INTO AppData // ----------------------------------------------------------- local original; original = FileSystem.fromAbsolutePath(full); if (original == !undefined) { local filename; filename = original.toString(original.Filename); local appDataDir; appDataDir = FileSystem.getFolder(FileSystem.Downloads); local newFile; newFile = appDataDir.getChildFile(filename); original.copy(newFile); //original.move(appDataDir); Console.print("Saved copy to AppData: " + newFile.toString(newFile.FullPath)); } // Your existing flow // Save As "FAVORITE" Midi File in AppDataFolder // SaveFavoriteMidiFiles(); // setAnalysisWindowFull(); disableAllChords(); // groupMidiNotesByTimeInWindow(analyzeStartMs, analyzeEndMs); // RangeSlider.setValue({ min: RangeSlider.get("min"), max: RangeSlider.get("max")}); disableAllChords(); // addChordsFromCurrentWindow(); // UI flags this.data.text = "Loaded"; this.data.dropped = true; this.data.hover = false; this.changed(); this.repaint(); knbSmartStrength.changed(); pnlCustomMidiPanel.sendRepaintMessage(); pnlCustomMidiRipView.sendRepaintMessage(); }
-
@Chazrox I'd start with some Console.prints to see where the processing is getting up to. Is the filename you're storing what you expect it to be, is
original
a file object - instead of== undefined
, useif (isDefined(original) && original.isFile())
to be certain. -
@d-healey ok. I'll check all of that now.
-
This is where im at now, still not working completely. All of the functions work in this code block except for the part where I want the dropped file saved to the AppData folder. Can anyone help with this please?
Scroll down a little bit...
//! onPnlMIDIFilesFileDrop --------------------------------------------------------------- inline function onpnlMIDIFilesFileDrop_B(obj) { // HOVER STATE prevHover = this.data.hover ? 1 : 0; this.data.hover = (obj.hover && !obj.drop) ? 1 : 0; if ((this.data.hover ? 1 : 0) != prevHover) { this.repaint(); } // HOVER CHECK if (obj.hover && !obj.drop) { this.data.text = "Drop to load!"; this.data.dropped = false; this.changed(); return; } if (!obj.drop || obj.fileName == undefined) { return; } // PARSE BASE NAME local full = obj.fileName; local slash = Math.max(full.lastIndexOf("/"), full.lastIndexOf("\\")); local dot = full.lastIndexOf("."); if (dot < 0) { dot = full.length; } local base = full.substring(slash + 1, dot); this.data.filename = base; // paint() this.data.fileName = base; // sync Console.print("BASE__" + base); // HIDE RANGE SLIDER WHILE NO EVENTS RangeSlider.set("visible", 0); // LOAD MIDI PLAYER TRACK 1 MIDIPlayer1.setFile(full, true, true); if (MIDIPlayer1.setTrack != undefined) MIDIPlayer1.setTrack(1); // START POLLING EVENTS midiRangeTimer.stopTimer(); midiRangeTimer.startTimer(50); // ----------------------------------------------------------- // COPY DROPPED FILE INTO AppData // ----------------------------------------------------------- local original = FileSystem.fromAbsolutePath(full); local filename = original.toString(this.data.Filename); if (isDefined(original) && original.isFile()) { Console.print(filename.toString()); Console.print("ORIGINAL__" + original); local appDataDir = FileSystem.getFolder(FileSystem.Downloads); local newFile = appDataDir.getChildFile(filename); original.copy(newFile); // newFile.move(appDataDir); Console.print("NEWNEWNEW" + newFile); } Console.print("Saved copy to AppData: " + newFile.toString(newFile.FullPath)); // Save As "FAVORITE" Midi File in AppDataFolder // SaveFavoriteMidiFiles(); // setAnalysisWindowFull(); disableAllChords(); // groupMidiNotesByTimeInWindow(analyzeStartMs, analyzeEndMs); // RangeSlider.setValue({ min: RangeSlider.get("min"), max: RangeSlider.get("max")}); disableAllChords(); // addChordsFromCurrentWindow(); // UI flags this.data.text = "Loaded"; this.data.dropped = true; this.data.hover = false; this.changed(); this.repaint(); knbSmartStrength.changed(); pnlCustomMidiPanel.sendRepaintMessage(); pnlCustomMidiRipView.sendRepaintMessage(); }
none of my .prints return any results in that section either.
-
@Chazrox said in Copy & Save '.mid file' to 'folder' || Help.:
[snip]Ok lets start with your code...
// PARSE BASE NAME local full = obj.fileName; }
and later:
local original = FileSystem.fromAbsolutePath(full);
So you are taking the filename from the obj and (later)asking for the actual file using .fromAbsolutePath()
..but absolute path wants just that - an absolute path not the file name....so something like C:\documents\myfolder\myfilename.mid
so this:
local original = FileSystem.fromAbsolutePath(full);
is probably failing - I think. so this:
if (isDefined(original) && original.isFile()) { etc. etc...
is going to return false - and not execute....you need to do a LOT more Console.print debugging....
like this:
//! onPnlMIDIFilesFileDrop --------------------------------------------------------------- inline function onpnlMIDIFilesFileDrop_B(obj) { // HOVER STATE prevHover = this.data.hover ? 1 : 0; this.data.hover = (obj.hover && !obj.drop) ? 1 : 0; if ((this.data.hover ? 1 : 0) != prevHover) { this.repaint(); } // HOVER CHECK if (obj.hover && !obj.drop) { this.data.text = "Drop to load!"; this.data.dropped = false; this.changed(); return; } if (!obj.drop || obj.fileName == undefined) { return; } // PARSE BASE NAME local full = obj.fileName; local slash = Math.max(full.lastIndexOf("/"), full.lastIndexOf("\\")); local dot = full.lastIndexOf("."); //============ Console.print("full contains:" + full); //============= if (dot < 0) { dot = full.length; } local base = full.substring(slash + 1, dot); this.data.filename = base; // paint() this.data.fileName = base; // sync Console.print("BASE__" + base); // HIDE RANGE SLIDER WHILE NO EVENTS RangeSlider.set("visible", 0); // LOAD MIDI PLAYER TRACK 1 MIDIPlayer1.setFile(full, true, true); if (MIDIPlayer1.setTrack != undefined) MIDIPlayer1.setTrack(1); // START POLLING EVENTS midiRangeTimer.stopTimer(); midiRangeTimer.startTimer(50); // ----------------------------------------------------------- // COPY DROPPED FILE INTO AppData // ----------------------------------------------------------- local original = FileSystem.fromAbsolutePath(full); //=========== Console.print("Original contains:" + original); Console.print("Original is defined ==:" + isDefined(original)); Console.print("Original is a file ==:" + original.isFile()); //=========== local filename = original.toString(this.data.Filename); if (isDefined(original) && original.isFile()) { Console.print(filename.toString()); Console.print("ORIGINAL__" + original); local appDataDir = FileSystem.getFolder(FileSystem.Downloads); local newFile = appDataDir.getChildFile(filename); original.copy(newFile); // newFile.move(appDataDir); Console.print("NEWNEWNEW" + newFile); } Console.print("Saved copy to AppData: " + newFile.toString(newFile.FullPath)); // Save As "FAVORITE" Midi File in AppDataFolder // SaveFavoriteMidiFiles(); // setAnalysisWindowFull(); disableAllChords(); // groupMidiNotesByTimeInWindow(analyzeStartMs, analyzeEndMs); // RangeSlider.setValue({ min: RangeSlider.get("min"), max: RangeSlider.get("max")}); disableAllChords(); // addChordsFromCurrentWindow(); // UI flags this.data.text = "Loaded"; this.data.dropped = true; this.data.hover = false; this.changed(); this.repaint(); knbSmartStrength.changed(); pnlCustomMidiPanel.sendRepaintMessage(); pnlCustomMidiRipView.sendRepaintMessage(); }
-
@Lindon said in Copy & Save '.mid file' to 'folder' || Help.:
local filename = original.toString(this.data.Filename);
also whilst we are here, Im not sure you can say this:
local filename = original.toString(this.data.Filename);
what I think you mean is this:
local filename = original.toString(original.Filename);
But its not going to work anyway until you get a file into original, not a text string...
-
@Lindon bro Thanks!
Im gonna check this out right now and report back.
I really appreciate the time brotha!These are a bunch of new api's for me so im sure I made alot of mistakes and asking chat gpt to help sometimes make things worse. lol
-
@Lindon said in Copy & Save '.mid file' to 'folder' || Help.:
you need to do a LOT more Console.print debugging....
I really should.
-
I was setting my B panels callback to my A panel....thats why none of my prints were hitting. When I was dropping on panel B, Panel A script was running which is already doing what it needs to do fine.
uhggh.. lol.
its one letter sometimes messing your whole day up. haha.const var pnlMIDIFiles_B = Content.getComponent("pnlMIDIFiles_B"); pnlMIDIFiles_B.setFileDropCallback("All Callbacks", "*.mid", onpnlMIDIFilesFileDrop_B);
Your prints you added are extensive and im going to follow that from now on! Thank You sir! Im getting alot of info from these prints which I should help big time. Again
-
After going through all the debugs I figured it out. Thank you guys so much. This was the last function I needed for my plugin which is just save a history of dropped files...and aside from some visual tweaks.....IM DONE!! Im on to the next step. Now i'll be buggin you guys about code-signing soon. haha.
Thanks so much!
-
C Chazrox has marked this topic as solved
-
I'll be sharing a cleaned up snippet version and make an example of this for anybody that might be looking for it. Thanks again guys!