@@ -109,11 +109,11 @@ void BashScript::setupObjectContent(shared_ptr<ofAppGLFWWindow> &mainWindow){
109109
110110 // Load script
111111 watcher.start ();
112- if (filepath == " none" ){
112+ /* if(filepath == "none"){
113113 isNewObject = true;
114114 ofFile file (ofToDataPath("scripts/empty.sh"));
115115 filepath = copyFileToPatchFolder(this->patchFolderPath,file.getAbsolutePath());
116- }
116+ }*/
117117
118118 if (!isThreadRunning ()){
119119 startThread ();
@@ -135,11 +135,6 @@ void BashScript::updateObjectContent(map<int,shared_ptr<PatchObject>> &patchObje
135135 }
136136 }
137137
138- /* if(needToLoadScript){
139- needToLoadScript = false;
140- loadScript(filepath);
141- }*/
142-
143138 // path watcher
144139 while (watcher.waitingEvents ()) {
145140 pathChanged (watcher.nextEvent ());
@@ -195,15 +190,15 @@ void BashScript::drawObjectNodeGui( ImGuiEx::NodeCanvas& _nodeCanvas ){
195190 canvasZoom = _nodeCanvas.GetCanvasScale ();
196191
197192 // file dialog
198- string newFileName = " bashScript_" +ofGetTimestampString (" %y%m%d" )+" .sh" ;
193+ /* string newFileName = "bashScript_"+ofGetTimestampString("%y%m%d")+".sh";
199194 if(ImGuiEx::getFileDialog(fileDialog, saveScriptFlag, "Save new bash script as", imgui_addons::ImGuiFileBrowser::DialogMode::SAVE, ".sh", newFileName, scaleFactor)){
200195 ofFile fileToRead(ofToDataPath("scripts/empty.sh"));
201196 ofFile newBashFile (fileDialog.selected_path);
202197 ofFile::copyFromTo(fileToRead.getAbsolutePath(),checkFileExtension(newBashFile.getAbsolutePath(), ofToUpper(newBashFile.getExtension()), "SH"),true,true);
203198 filepath = copyFileToPatchFolder(this->patchFolderPath,checkFileExtension(newBashFile.getAbsolutePath(), ofToUpper(newBashFile.getExtension()), "SH"));
204199 threadLoaded = false;
205200 reloadScript();
206- }
201+ }*/
207202
208203 if (ImGuiEx::getFileDialog (fileDialog, loadScriptFlag, " Select a bash script" , imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, " .sh" , " " , scaleFactor)){
209204 ofFile bashFile (fileDialog.selected_path );
@@ -233,6 +228,47 @@ void BashScript::drawObjectNodeConfig(){
233228 if (ImGui::Button (" New" ,ImVec2 (224 *scaleFactor,26 *scaleFactor))){
234229 saveScriptFlag = true ;
235230 }
231+
232+ if (saveScriptFlag){
233+ newScriptName = " bashScript_" +ofGetTimestampString (" %y%m%d" )+" .sh" ;
234+ ImGui::OpenPopup (" Save new bash script as" );
235+ }
236+
237+ if (ImGui::BeginPopup (" Save new bash script as" )){
238+
239+ if (ImGui::InputText (" ##NewFileNameInput" , &newScriptName,ImGuiInputTextFlags_EnterReturnsTrue)){
240+ if (newScriptName != " " ){
241+ // save file in data/ folder
242+ ofFile fileToRead (ofToDataPath (" scripts/empty.sh" ));
243+ ofFile newBashFile (this ->patchFolderPath +newScriptName);
244+ ofFile::copyFromTo (fileToRead.getAbsolutePath (),checkFileExtension (newBashFile.getAbsolutePath (), ofToUpper (newBashFile.getExtension ()), " SH" ),true ,true );
245+ filepath = this ->patchFolderPath +newScriptName;
246+ threadLoaded = false ;
247+ reloadScript ();
248+ }
249+ ImGui::CloseCurrentPopup ();
250+ }
251+ ImGui::SameLine ();
252+ if (ImGui::Button (" Cancel" )){
253+ ImGui::CloseCurrentPopup ();
254+ }
255+ ImGui::SameLine ();
256+ if (ImGui::Button (" Create" )){
257+ if (newScriptName != " " ){
258+ // save file in data/ folder
259+ ofFile fileToRead (ofToDataPath (" scripts/empty.sh" ));
260+ ofFile newBashFile (this ->patchFolderPath +newScriptName);
261+ ofFile::copyFromTo (fileToRead.getAbsolutePath (),checkFileExtension (newBashFile.getAbsolutePath (), ofToUpper (newBashFile.getExtension ()), " SH" ),true ,true );
262+ filepath = this ->patchFolderPath +newScriptName;
263+ threadLoaded = false ;
264+ reloadScript ();
265+ }
266+ ImGui::CloseCurrentPopup ();
267+ }
268+ ImGui::EndPopup ();
269+
270+ }
271+
236272 ImGui::Spacing ();
237273 if (ImGui::Button (" Open" ,ImVec2 (224 *scaleFactor,26 *scaleFactor))){
238274 loadScriptFlag = true ;
@@ -243,16 +279,6 @@ void BashScript::drawObjectNodeConfig(){
243279 " Load and run a bash script files (Bourne-Again SHell). You can type code with the Mosaic code editor, or with your default code editor. Scripts will refresh automatically on save." ,
244280 " https://mosaic.d3cod3.org/reference.php?r=bash-script" , scaleFactor);
245281
246- // file dialog
247- string newFileName = " bashScript_" +ofGetTimestampString (" %y%m%d" )+" .sh" ;
248- if (ImGuiEx::getFileDialog (fileDialog, saveScriptFlag, " Save new bash script as" , imgui_addons::ImGuiFileBrowser::DialogMode::SAVE, " .sh" , newFileName, scaleFactor)){
249- ofFile fileToRead (ofToDataPath (" scripts/empty.sh" ));
250- ofFile newBashFile (fileDialog.selected_path );
251- ofFile::copyFromTo (fileToRead.getAbsolutePath (),checkFileExtension (newBashFile.getAbsolutePath (), ofToUpper (newBashFile.getExtension ()), " SH" ),true ,true );
252- filepath = copyFileToPatchFolder (this ->patchFolderPath ,checkFileExtension (newBashFile.getAbsolutePath (), ofToUpper (newBashFile.getExtension ()), " SH" ));
253- threadLoaded = false ;
254- reloadScript ();
255- }
256282
257283 if (ImGuiEx::getFileDialog (fileDialog, loadScriptFlag, " Select a bash script" , imgui_addons::ImGuiFileBrowser::DialogMode::OPEN, " .sh" , " " , scaleFactor)){
258284 ofFile bashFile (fileDialog.selected_path );
@@ -264,10 +290,6 @@ void BashScript::drawObjectNodeConfig(){
264290
265291// --------------------------------------------------------------
266292void BashScript::removeObjectContent (bool removeFileFromData){
267- if (filepath != ofToDataPath (" scripts/empty.sh" ,true ) && removeFileFromData){
268- // removeFile(filepath);
269- }
270-
271293 std::unique_lock<std::mutex> lck (mutex);
272294 stopThread ();
273295 condition.notify_all ();
@@ -277,41 +299,45 @@ void BashScript::removeObjectContent(bool removeFileFromData){
277299// --------------------------------------------------------------
278300void BashScript::loadScript (string scriptFile){
279301
280- filepath = forceCheckMosaicDataPath (scriptFile);
281- currentScriptFile.open (filepath);
302+ ofFile tmpf (scriptFile);
303+
304+ if (tmpf.isFile () && ofToUpper (tmpf.getExtension ()) == " SH" ){
305+ filepath = forceCheckMosaicDataPath (scriptFile);
306+ currentScriptFile.open (filepath);
282307
283- string cmd = " " ;
284- FILE *execFile;
308+ string cmd = " " ;
309+ FILE *execFile;
285310
286- cmd = " sh " +filepath;
287- execFile = popen (cmd.c_str (), " r" );
311+ cmd = " sh " +filepath;
312+ execFile = popen (cmd.c_str (), " r" );
288313
289- if (execFile){
290- scriptLoaded = true ;
291- watcher.removeAllPaths ();
292- watcher.addPath (filepath);
314+ if (execFile){
315+ scriptLoaded = true ;
316+ watcher.removeAllPaths ();
317+ watcher.addPath (filepath);
293318
294- ofLog (OF_LOG_NOTICE," [verbose] bash script: %s RUNNING!" ,filepath.c_str ());
295- ofLog (OF_LOG_NOTICE," " );
319+ ofLog (OF_LOG_NOTICE," [verbose] bash script: %s RUNNING!" ,filepath.c_str ());
320+ ofLog (OF_LOG_NOTICE," " );
296321
297- char buffer[128 ];
298- _outletParams[0 ] = new string ();
299- *static_cast <string *>(_outletParams[0 ]) = " " ;
300- while (!feof (execFile)){
301- if (fgets (buffer, sizeof (buffer), execFile) != nullptr ){
302- char *s = buffer;
303- std::string tempstr (s);
304- static_cast <string *>(_outletParams[0 ])->append (tempstr);
305- static_cast <string *>(_outletParams[0 ])->append (" " );
322+ char buffer[128 ];
323+ _outletParams[0 ] = new string ();
324+ *static_cast <string *>(_outletParams[0 ]) = " " ;
325+ while (!feof (execFile)){
326+ if (fgets (buffer, sizeof (buffer), execFile) != nullptr ){
327+ char *s = buffer;
328+ std::string tempstr (s);
329+ static_cast <string *>(_outletParams[0 ])->append (tempstr);
330+ static_cast <string *>(_outletParams[0 ])->append (" " );
331+ }
306332 }
307- }
308- // ofLog(OF_LOG_NOTICE,"%s",static_cast<string *>(_outletParams[0])->c_str());
309- ofLog (OF_LOG_NOTICE," [verbose]bash script: %s EXECUTED!" ,filepath.c_str ());
333+ // ofLog(OF_LOG_NOTICE,"%s",static_cast<string *>(_outletParams[0])->c_str());
334+ ofLog (OF_LOG_NOTICE," [verbose]bash script: %s EXECUTED!" ,filepath.c_str ());
310335
311- this ->saveConfig (false );
336+ this ->saveConfig (false );
312337
313- pclose (execFile);
338+ pclose (execFile);
314339
340+ }
315341 }
316342
317343}
0 commit comments