Using QProcess to run shell script

• Apr 24, 2023 - 02:29

Hi! I'm currently working on a plugin that needs to run a shell script within it to run a set of python commands for some audio processing tasks. I've written the following function, where the run_script.sh is the shell script that should be run (and I've been passing in absolute paths, since I'm just trying to get it to work on my local machine for now). I've tried both the commented out start line just calling the source command as well as the other start call that actually uses bash, and while the first one completely doesn't work (says that it failed), the second one completely freezes MuseScore.

  // Run python script for audio transcribing
  function runCommand(){
      proc.start("pwd");
      proc.waitForFinished(300000);
      console.log(proc.readAllStandardOutput());
      // proc.start("source /path/to/script/run_script.sh");
      proc.start("bash", ["-c", "source \"\"/path/to/script/run_script.sh\"\""]);
      var venv = proc.waitForFinished(-1);
      console.log(venv);
      console.log("Output", proc.readAllStandardOutput());
      // console.log("running command", cmd);
      var args = new Array();
  }

Does anyone have input as to why it would freeze upon trying to run the bash command? Is there anyway to prevent this from happening/ get it to actually run the right shell script, or is that not feasible given how QProcess has been incorporated into MuseScore?

This is also my first time writing a plugin (but have been looking through forums and QML documentation), so apologies if I'm misunderstanding anything. Thanks so much!


Comments

In reply to by Jojo-Schmitz

That's a good point; I had seen somewhere on some QML forum that there should be 2, but now that I try to find the forum post again, I can't find it. With that being said, since then, I've tried:

  1. proc.start("bash", ["-c", "source \"/path/to/script/run_script.sh\""]);
  2. proc.start("bash", ["-c", "source", "/path/to/script/run_script.sh"]);

And both of these have crashed musescore.

If it maybe helps, the current bash script looks like:

#!/bin/bash
ls

as a test just to see whether I can get something running, and it works in my regular terminal.

In reply to by Jojo-Schmitz

Okay so the thing that actually ended up working was proc.start('bash -c "source /path/to/script/run_script.sh"'); Similarly, to do a pure ls command, I would do proc.start('bash -c "ls"')

With that being said, even though the thing doesn't freeze my musescore anymore, my shell script doesn't run. It contains a python script locally and in effect, does something like the following:
- cd to the right directory to activate the virtual environment
- runs the python script.

The bash command also doesn't work (no output, even when it just has an ls command in it) when trying to run the bash command with just the direct path to the python venv and running the script.

Both of these commands (the shell script and just the python call directly) work in my terminal (outside of the folder where they are located). I was wondering if you might know whether this is just a limitation with Musescore or whether I might just be doing something wrong again?

Thanks so much for your help so far!

In reply to by gh20

Try this:

 d = '/bin/sh -c "~/Documents/MuseScore3/Plugins/script.sh"';
proc.start(d)
var val=proc.waitForFinished(5000)
// DEBUG
try {
    var out=proc.readAllStandardOutput()
    console.log("-- Command output: "+out)
    if (val) {
    console.log('terminated correctly.')
    } else {
    console.log('failure')
    }
} catch (err) {
    console.log("--" + err.message);
}

And the script I used is

#!/bin/sh
date >> /tmp/date.txt
ls

It is working fine. It is just a matter of finding the right quote combination !

In reply to by parkingb

So I copied this exact code into my Musescore 3 (hopefully correctly, though I might've done something wrong there), and while the command says that it ran successfully (see the second screenshot), nothing actually gets printed. Is it due to my OS? I'm on a Mac (if that changes anything)

The script:

#!/bin/sh
date >> /tmp/date.txt
ls

In reply to by gh20

Sorry i didn't realize the screenshots don't preview:

import QtQuick 2.0
import MuseScore 3.0

MuseScore {
      menuPath: "Plugins.pluginName"
      description: "Description goes here"
      version: "1.0"
      QProcess {
    id: proc
  }
      onRun: {
            var d = '/bin/sh -c "~/Documents/MuseScore3/Plugins/script.sh"';
            proc.start(d)
            var val=proc.waitForFinished(5000)
            // DEBUG
            try {
                  var out=proc.readAllStandardOutput()
                  console.log("-- Command output: "+out)
                  if (val) {
                  console.log('terminated correctly.')
                  } 
                  else {
                  console.log('failure')
                  }
            }
            catch (err) {
                  console.log("--" + err.message);
            }           
      }
}

and output:

Running…
Plugin Details:
  Menu Path: Plugins.pluginName
  Version: 1.0
  Description: Description goes here
  Requires Score
Debug: -- Command output: 
Debug: terminated correctly.

In reply to by gh20

Your

Debug: -- Command Output:

is weird.
On my Ubuntu, the execution of that code returns some(*) content. I.e. a typical ls command output.
So I guess, your shell script is not running.
Are your sure your script.sh file is located in ~/Documents/MuseScore3/Plugins/ ?
If you launch it manually, does it write a date into /tmp/date.txt ?

(*): "some content" = the content of the folder /tmp/.mount_MuseScIRyi0X/bin/ which is (I guess) where MuseScore is unpacked when ran as an AppImage

In reply to by parkingb

Honestly, I wonder if it's because I'm on MacOS. The script.sh file is in that file path, and when I launch it manually, it writes a date into the correct file in the correct place. It's alright though; I was using this for a class project, and the fact that it just doesn't work on MacOS (at least on mine) is frustrating but not the worst thing. Thanks for all your support!

Do you still have an unanswered question? Please log in first to post your question.