Using ShellEx to run an external script

Greetings,

I have a rackspace script that pulls in data from a file created by a shell script. I would like to trigger the shell script from within GP and I think ShellEx is the way. I am a Mac user so according to the documentation I should be able to use this.

ShellEx : Run an OS Shell command (Mac only) that returns the output of the command
Declaration: function ShellEx (command : String) returns String
Category: System
Parameters
command : String
returns String

I am noodling around trying to get it working but if anyone has done this already could you please share the scripting around it? Grateful for any help folks can provide.
This is the snippet from my script as of this posting:

// Function to refresh MOTU data by running the shell script
function RefreshMOTUData() returns Boolean
    var scriptPath : String
    var result : Integer
    
    // Update this path to where you've saved your zsh script
    scriptPath = "/Users/mbpm4/Documents/Gig Performer/828data003.sh"
    
    // Execute the shell script
    result = Shell(scriptPath)
    
    // Return true if successful (exit code 0)
    return result == 0
end

The compiler errors at the return result line:

Syntax Error in "Main": Line 73, Col 5: Unexpected or unrecognized token: 'return'
Mismatched input 'return' expecting End

Entire script compiles and runs when I don’t have the above function included so clearly I have something wrong in this function.

Entire script:

// Gig Performer GPScript – Load MOTU channel data with label coloring and names

var
    // Widgets for 25 rows × 6 fields (Label, Fader, Mute, Solo, Aux5, Aux6)
    lbl1_1, lbl1_2, lbl1_3, lbl1_4, lbl1_5, lbl1_6 : Widget
    lbl2_1, lbl2_2, lbl2_3, lbl2_4, lbl2_5, lbl2_6 : Widget
    lbl3_1, lbl3_2, lbl3_3, lbl3_4, lbl3_5, lbl3_6 : Widget
    lbl4_1, lbl4_2, lbl4_3, lbl4_4, lbl4_5, lbl4_6 : Widget
    lbl5_1, lbl5_2, lbl5_3, lbl5_4, lbl5_5, lbl5_6 : Widget
    lbl6_1, lbl6_2, lbl6_3, lbl6_4, lbl6_5, lbl6_6 : Widget
    lbl7_1, lbl7_2, lbl7_3, lbl7_4, lbl7_5, lbl7_6 : Widget
    lbl8_1, lbl8_2, lbl8_3, lbl8_4, lbl8_5, lbl8_6 : Widget
    lbl9_1, lbl9_2, lbl9_3, lbl9_4, lbl9_5, lbl9_6 : Widget
    lbl10_1, lbl10_2, lbl10_3, lbl10_4, lbl10_5, lbl10_6 : Widget
    lbl11_1, lbl11_2, lbl11_3, lbl11_4, lbl11_5, lbl11_6 : Widget
    lbl12_1, lbl12_2, lbl12_3, lbl12_4, lbl12_5, lbl12_6 : Widget
    lbl13_1, lbl13_2, lbl13_3, lbl13_4, lbl13_5, lbl13_6 : Widget
    lbl14_1, lbl14_2, lbl14_3, lbl14_4, lbl14_5, lbl14_6 : Widget
    lbl15_1, lbl15_2, lbl15_3, lbl15_4, lbl15_5, lbl15_6 : Widget
    lbl16_1, lbl16_2, lbl16_3, lbl16_4, lbl16_5, lbl16_6 : Widget
    lbl17_1, lbl17_2, lbl17_3, lbl17_4, lbl17_5, lbl17_6 : Widget
    lbl18_1, lbl18_2, lbl18_3, lbl18_4, lbl18_5, lbl18_6 : Widget
    lbl19_1, lbl19_2, lbl19_3, lbl19_4, lbl19_5, lbl19_6 : Widget
    lbl20_1, lbl20_2, lbl20_3, lbl20_4, lbl20_5, lbl20_6 : Widget
    lbl21_1, lbl21_2, lbl21_3, lbl21_4, lbl21_5, lbl21_6 : Widget
    lbl22_1, lbl22_2, lbl22_3, lbl22_4, lbl22_5, lbl22_6 : Widget
    lbl23_1, lbl23_2, lbl23_3, lbl23_4, lbl23_5, lbl23_6 : Widget
    lbl24_1, lbl24_2, lbl24_3, lbl24_4, lbl24_5, lbl24_6 : Widget
    lbl25_1, lbl25_2, lbl25_3, lbl25_4, lbl25_5, lbl25_6 : Widget

    rowRef : Widget Array
    btnLoad : Widget

    arr_lbl_row1  : Widget Array = [lbl1_1, lbl1_2, lbl1_3, lbl1_4, lbl1_5, lbl1_6]
    arr_lbl_row2  : Widget Array = [lbl2_1, lbl2_2, lbl2_3, lbl2_4, lbl2_5, lbl2_6]
    arr_lbl_row3  : Widget Array = [lbl3_1, lbl3_2, lbl3_3, lbl3_4, lbl3_5, lbl3_6]
    arr_lbl_row4  : Widget Array = [lbl4_1, lbl4_2, lbl4_3, lbl4_4, lbl4_5, lbl4_6]
    arr_lbl_row5  : Widget Array = [lbl5_1, lbl5_2, lbl5_3, lbl5_4, lbl5_5, lbl5_6]
    arr_lbl_row6  : Widget Array = [lbl6_1, lbl6_2, lbl6_3, lbl6_4, lbl6_5, lbl6_6]
    arr_lbl_row7  : Widget Array = [lbl7_1, lbl7_2, lbl7_3, lbl7_4, lbl7_5, lbl7_6]
    arr_lbl_row8  : Widget Array = [lbl8_1, lbl8_2, lbl8_3, lbl8_4, lbl8_5, lbl8_6]
    arr_lbl_row9  : Widget Array = [lbl9_1, lbl9_2, lbl9_3, lbl9_4, lbl9_5, lbl9_6]
    arr_lbl_row10 : Widget Array = [lbl10_1, lbl10_2, lbl10_3, lbl10_4, lbl10_5, lbl10_6]
    arr_lbl_row11 : Widget Array = [lbl11_1, lbl11_2, lbl11_3, lbl11_4, lbl11_5, lbl11_6]
    arr_lbl_row12 : Widget Array = [lbl12_1, lbl12_2, lbl12_3, lbl12_4, lbl12_5, lbl12_6]
    arr_lbl_row13 : Widget Array = [lbl13_1, lbl13_2, lbl13_3, lbl13_4, lbl13_5, lbl13_6]
    arr_lbl_row14 : Widget Array = [lbl14_1, lbl14_2, lbl14_3, lbl14_4, lbl14_5, lbl14_6]
    arr_lbl_row15 : Widget Array = [lbl15_1, lbl15_2, lbl15_3, lbl15_4, lbl15_5, lbl15_6]
    arr_lbl_row16 : Widget Array = [lbl16_1, lbl16_2, lbl16_3, lbl16_4, lbl16_5, lbl16_6]
    arr_lbl_row17 : Widget Array = [lbl17_1, lbl17_2, lbl17_3, lbl17_4, lbl17_5, lbl17_6]
    arr_lbl_row18 : Widget Array = [lbl18_1, lbl18_2, lbl18_3, lbl18_4, lbl18_5, lbl18_6]
    arr_lbl_row19 : Widget Array = [lbl19_1, lbl19_2, lbl19_3, lbl19_4, lbl19_5, lbl19_6]
    arr_lbl_row20 : Widget Array = [lbl20_1, lbl20_2, lbl20_3, lbl20_4, lbl20_5, lbl20_6]
    arr_lbl_row21 : Widget Array = [lbl21_1, lbl21_2, lbl21_3, lbl21_4, lbl21_5, lbl21_6]
    arr_lbl_row22 : Widget Array = [lbl22_1, lbl22_2, lbl22_3, lbl22_4, lbl22_5, lbl22_6]
    arr_lbl_row23 : Widget Array = [lbl23_1, lbl23_2, lbl23_3, lbl23_4, lbl23_5, lbl23_6]
    arr_lbl_row24 : Widget Array = [lbl24_1, lbl24_2, lbl24_3, lbl24_4, lbl24_5, lbl24_6]
    arr_lbl_row25 : Widget Array = [lbl25_1, lbl25_2, lbl25_3, lbl25_4, lbl25_5, lbl25_6]
  
    
    // Function to refresh MOTU data by running the shell script
function RefreshMOTUData() returns Boolean
    var scriptPath : String
    var result : Integer
   
    // Update this path to where you've saved your zsh script
    scriptPath = "/Users/mbpm4/Documents/Gig Performer/828data003.sh"
    
    // Execute the shell script
    result = Shell(scriptPath)
    
    // Return true if successful (exit code 0)
    return result == 0
end
    
function LoadFile()
    var i, ii : Integer
    var row : String
    var rows, rowItems : String Array
    var labelLen : Integer
    var suffix6, suffix10 : String
    var formatSuffix : String
    var newline : String = <<<
>>>
    var textfile, dbStr, fmt : String
    var dbVal, t, shade, greenVal : double

    textfile = LoadStringFromTextFile("/Users/mbpm4/Documents/Gig Performer/828es_motu_values.txt")

    while textfile <> "" do
        row = StringUpToFirstOccurrence(textfile, newline, false, false)
        row = StringUpToFirstOccurrence(row, "//", false, false)
        if row <> "" then rows <-- row end
        textfile = StringAfterFirstOccurrence(textfile, newline, false, false)
    end

    for i = 1; i <= 25 and i < Size(rows); i = i + 1 do
        rowItems = ParseCSVString(rows[i])

        select
            i == 1  do rowRef = arr_lbl_row1
            i == 2  do rowRef = arr_lbl_row2
            i == 3  do rowRef = arr_lbl_row3
            i == 4  do rowRef = arr_lbl_row4
            i == 5  do rowRef = arr_lbl_row5
            i == 6  do rowRef = arr_lbl_row6
            i == 7  do rowRef = arr_lbl_row7
            i == 8  do rowRef = arr_lbl_row8
            i == 9  do rowRef = arr_lbl_row9
            i == 10 do rowRef = arr_lbl_row10
            i == 11 do rowRef = arr_lbl_row11
            i == 12 do rowRef = arr_lbl_row12
            i == 13 do rowRef = arr_lbl_row13
            i == 14 do rowRef = arr_lbl_row14
            i == 15 do rowRef = arr_lbl_row15
            i == 16 do rowRef = arr_lbl_row16
            i == 17 do rowRef = arr_lbl_row17
            i == 18 do rowRef = arr_lbl_row18
            i == 19 do rowRef = arr_lbl_row19
            i == 20 do rowRef = arr_lbl_row20
            i == 21 do rowRef = arr_lbl_row21
            i == 22 do rowRef = arr_lbl_row22
            i == 23 do rowRef = arr_lbl_row23
            i == 24 do rowRef = arr_lbl_row24
            i == 25 do rowRef = arr_lbl_row25
        end

        for ii = 0; ii < 6; ii = ii + 1 do
            SetWidgetLabel(rowRef[ii], rowItems[ii])
        
           // Color the format label
if ii == 0 then
    formatSuffix = StringAfterFirstOccurrence(rowItems[ii], "(", false, false)

    if formatSuffix == "Mono)" then
        SetWidgetFillColor(rowRef[ii], RGBToColor(0.5, 0.7, 1.0, 1.0)) // Light blue
    elsif formatSuffix == "Stereo L)" or formatSuffix == "Stereo R)" then
        SetWidgetFillColor(rowRef[ii], RGBToColor(0.3, 0.5, 1.0, 1.0)) // Medium blue
    else
        SetWidgetFillColor(rowRef[ii], RGBToColor(0.235, 0.235, 0.235, 1.0)) // Fallback gray
    end
end
            if ii == 1 then
                dbStr = StringUpToFirstOccurrence(rowItems[ii], " dB", false, false)
                dbVal = StringToDouble(dbStr)

                if dbVal <= -60.0 then
                    SetWidgetFillColor(rowRef[ii], RGBToColor(0.235, 0.235, 0.235, 1.0))
                elsif dbVal > -60.0 and dbVal <= 0.0 then
                    t = (dbVal + 60.0) / 60.0
                    shade = 0.235 + (0.75 - 0.235) * t
                    SetWidgetFillColor(rowRef[ii], RGBToColor(shade, shade, shade, 1.0))
                elsif dbVal > 0.0 and dbVal <= 12.0 then
                    t = dbVal / 12.0
                    greenVal = 1.0 - t
                    SetWidgetFillColor(rowRef[ii], RGBToColor(1.0, greenVal, 0.0, 1.0))
                else
                    SetWidgetFillColor(rowRef[ii], RGBToColor(0.249,0.255,0.275, 1.0))
                end
            end

            if ii == 2 then
                if rowItems[ii] == "On" then
                    SetWidgetFillColor(rowRef[ii], RGBToColor(0.976, 0.027, 0.027, 1.0))
                else
                    SetWidgetFillColor(rowRef[ii], RGBToColor(0.235, 0.235, 0.235, 1.0))
                end
            end

            if ii == 3 then
                if rowItems[ii] == "On" then
                    SetWidgetFillColor(rowRef[ii], RGBToColor(0.749, 0.690, 0.133, 1.0))
                else
                    SetWidgetFillColor(rowRef[ii], RGBToColor(0.235, 0.235, 0.235, 1.0))
                end
            end

            if ii == 4 or ii == 5 then
                if rowItems[ii] <> "-60.0 dB" then
                    SetWidgetFillColor(rowRef[ii], RGBToColor(0.024, 0.639, 0.251, 1.0))
                else
                    SetWidgetFillColor(rowRef[ii], RGBToColor(0.235, 0.235, 0.235, 1.0))
                end
            end
        end
    end
end

On WidgetValueChanged(newValue : double) from btnLoad
    if newValue == 1.0 then
        LoadFile()
    end
End

Just for completion’s sake, here is what it populates in GP

Hmmm, have you reviewed the GP Script language manual and the section explaining functions?

On a cursory look, I see a number of things wrong with your example

  1. At the top you refer to the ShellEx function but in your actual script you are using the Shell function - that’s a bit different
  2. In functions, result is a special pre-declared variable that holds the value to be returned to the caller, so declaring it as a local variable will supercede the pre-declared variable and nothing will get returned.
  3. There is no return keyword in GPScript (GPScript is not C)

Try this

function RefreshMOTUData() returns Boolean
    var scriptPath : String
    var scriptRC : Integer
    
    // Update this path to where you've saved your zsh script
    scriptPath = "/Users/mbpm4/Documents/Gig Performer/828data003.sh"
    
    // Execute the shell script
    scriptRC = Shell(scriptPath)
    
    // Return true if successful (exit code 0)
    result = scriptRC == 0
end

Thanks @dhj! That did it. Script compiles and everything works as expected now. :waving_hand: :trophy: