Getting Stream Deck to display number, incrementing on button press.

Hi again,
Apologies in advance for extremely basic questions, but I'm trying to teach myself how to use BTT with SD and Javascript.

I'm having trouble working out how to make something happen when the button is pressed.

I've tried various things, at the moment I have this in the Actions assigned under run Real Javascript:

(async ()=> {
let a = a;
a++
let result = await callBTT(a, a++);
returnToBTT(a);
})();

And under the widget I have this:

(async ()=> {
let a = 5; 
a++  
returnToBTT(a); 
  })(); 

It's giving me a consistent 6. I'm guessing that callBTT is the button press, and I'm thinking that the async is going to allow me a 'await' function that perhaps waits for a keypress? I'm really just saying this to show that I have put a little effort into trying to work it out myself. I realise it's completely wrong!

Attempt No. 2 (or 25 depending on how you count it.)

[
  {
    "BTTStreamDeckButtonName" : "Timer Countdown",
    "BTTTriggerType" : 725,
    "BTTTriggerTypeDescription" : "Apple Script Widget",
    "BTTTriggerClass" : "BTTTriggerTypeStreamDeck",
    "BTTPredefinedActionType" : 281,
    "BTTPredefinedActionName" : "Run Real JavaScript",
    "BTTRealJavaScriptString" : "(async ()=> {\n\nlet count = 0;\nlet newcount = count + 1\n\n\nreturnToBTT(newCount);\n})()\n",
    "BTTEnabled2" : 1,
    "BTTAlternateModifierKeys" : 0,
    "BTTRepeatDelay" : 0,
    "BTTUUID" : "B3D4F6EA-E86E-4B34-9D63-7B1B4313CC8B",
    "BTTNotesInsteadOfDescription" : 1,
    "BTTNotes" : "Clicker",
    "BTTEnabled" : 1,
    "BTTModifierMode" : 0,
    "BTTOrder" : 21,
    "BTTDisplayOrder" : 0,
    "BTTMergeIntoTouchBarGroups" : 1,
    "BTTTriggerConfig" : {
      "BTTScriptRegex" : "",
      "BTTStreamDeckCornerRadius" : 12,
      "BTTStreamDeckDisplayOrder" : 0,
      "BTTStreamDeckSFSymbolName" : "",
      "BTTStreamDeckAlternateAttributedTitle" : "cnRmZAAAAAADAAAAAgAAAAcAAABUWFQucnRmAQAAAC6BAQAAKwAAAAEAAAB5AQAAe1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcY29jb2FydGYyNjM4Clxjb2NvYXRleHRzY2FsaW5nMFxjb2NvYXBsYXRmb3JtMHtcZm9udHRibFxmMFxmc3dpc3NcZmNoYXJzZXQwIEhlbHZldGljYTt9CntcY29sb3J0Ymw7XHJlZDI1NVxncmVlbjI1NVxibHVlMjU1O1xyZWQyNTVcZ3JlZW4yNTVcYmx1ZTI1NTt9CntcKlxleHBhbmRlZGNvbG9ydGJsOztcY3NzcmdiXGMxMDAwMDBcYzEwMDAwMFxjMTAwMDAwO30KXHBhcmRcdHg1NjBcdHgxMTIwXHR4MTY4MFx0eDIyNDBcdHgyODAwXHR4MzM2MFx0eDM5MjBcdHg0NDgwXHR4NTA0MFx0eDU2MDBcdHg2MTYwXHR4NjcyMFxwYXJkaXJuYXR1cmFsXHFjXHBhcnRpZ2h0ZW5mYWN0b3IwCgpcZjBcZnM1MCBcY2YyIGxhbGFsYX0BAAAAIwAAAAEAAAAHAAAAVFhULnJ0ZhAAAADeVWZjtgEAAAAAAAAAAAAA",
      "BTTScriptAlwaysRunOnInit" : 0,
      "BTTStreamDeckAlternateIconColor1" : "255, 255, 255, 255",
      "BTTStreamDeckAlternateIconColor2" : "255, 255, 255, 255",
      "BTTStreamDeckIconColor3" : "255, 255, 255, 255",
      "BTTScriptSettings" : {
        "BTTScriptType" : 3,
        "BTTAppleScriptString" : "(async ()=> {\n\nlet startCount = await callBTT('get_number_variable', {variable_name:'count'});\n\nlet newCount = startCount + 1;\n\nreturnToBTT(newCount);\n\n})();",
        "BTTAppleScriptUsePath" : false
      },
      "BTTStreamDeckAlternateIconColor3" : "255, 255, 255, 255",
      "BTTStreamDeckImageOffsetY" : 20,
      "BTTStreamDeckIconColor1" : "255.000000, 255.000000, 255.000000, 255.000000",
      "BTTStreamDeckMainTab" : 3,
      "BTTStreamDeckAlternateCornerRadius" : 12,
      "BTTStreamDeckAlternateBackgroundColor" : "110.000001, 193.000004, 56.000000, 255.000000",
      "BTTStreamDeckTextOffsetY" : -5,
      "BTTStreamDeckAppearanceTab" : 0,
      "BTTStreamDeckBackgroundColor" : "255.000000, 224.000002, 97.000002, 255.000000",
      "BTTScriptUpdateInterval" : 1,
      "Notes" : "Clicker",
      "BTTStreamDeckTextOffsetX" : 0,
      "BTTStreamDeckImageOffsetX" : 0,
      "BTTStreamDeckImageHeight" : 30,
      "BTTStreamDeckAttributedTitle" : "cnRmZAAAAAADAAAAAgAAAAcAAABUWFQucnRmAQAAAC5zAQAAKwAAAAEAAABrAQAAe1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcY29jb2FydGYyNjM4Clxjb2NvYXRleHRzY2FsaW5nMFxjb2NvYXBsYXRmb3JtMHtcZm9udHRibFxmMFxmc3dpc3NcZmNoYXJzZXQwIEhlbHZldGljYTt9CntcY29sb3J0Ymw7XHJlZDI1NVxncmVlbjI1NVxibHVlMjU1O1xyZWQyNTFcZ3JlZW4yXGJsdWU3O30Ke1wqXGV4cGFuZGVkY29sb3J0Ymw7O1xjc3NyZ2JcYzEwMDAwMFxjMTQ5MTNcYzA7fQpccGFyZFx0eDU2MFx0eDExMjBcdHgxNjgwXHR4MjI0MFx0eDI4MDBcdHgzMzYwXHR4MzkyMFx0eDQ0ODBcdHg1MDQwXHR4NTYwMFx0eDYxNjBcdHg2NzIwXHBhcmRpcm5hdHVyYWxccWNccGFydGlnaHRlbmZhY3RvcjAKClxmMFxmczc2IFxjZjIgKzF9AQAAACMAAAABAAAABwAAAFRYVC5ydGYQAAAA3lVmY7YBAAAAAAAAAAAAAA==",
      "BTTStreamDeckAlternateImageHeight" : 50,
      "BTTStreamDeckIconType" : 2,
      "BTTStreamDeckIconColor2" : "255, 255, 255, 255",
      "BTTStreamDeckSFSymbolStyle" : 1
    }
  }
]

Two important things:
1.) callBTT is just for calling BTT functions (see Using Java Script (not JXA) · GitBook (folivora.ai) )

2.) Every JS execution starts from a fresh context. So you need to explicitly persist and reload your data.

To increase a number you'd do something like this:

Assign as action

(async ()=> {

// load the existing value or if non exists set it to 0
let previous = await callBTT('get_number_variable', {variable_name:'your_variable_name'});

if(!previous) {
 previous = 0;
}

// set the new number (previous +1 )
await callBTT('set_number_variable', {variable_name:'your_variable_name', to: previous+1});

// if you provide the uuid of the widget, BTT will immediately execute it's script again to refresh the display content
callBTT('refresh_widget', {uuid: 'C6DDEFD2-F418-45AE-B63B-A4BEC597BEEB'});

returnToBTT(current);

})();

The widget script

(async ()=> {
// load the saved value 
let  currentValue = await callBTT('get_number_variable', {variable_name:'your_variable_name'});
if(!currentValue) {
currentValue = 0;
}
returnToBTT(currentValue)
  })();
1 Like

This is cool. I now have a clicker.

By creating a Named Trigger with Run Real Javascript with the following script, and enabling a long press action, it resets. Thanks for your help @Andreas_Hegenberg. It may just be me, but like this might be a useful one for your sample SD list, or in the tutorials, just to get those of us that aren't programmers started, along with the timer. Or maybe not, but thanks regardless, I like my clicker!

(async ()=> {

// load the existing value or if non exists set it to 0
let previous = await callBTT('get_number_variable', {variable_name:'your_variable_name'});

if(previous > 0) {
 previous = 0;
}

// if you provide the uuid of the widget, BTT will immediately execute it's script again to refresh the display content
callBTT('refresh_widget', {uuid: 'C6DDEFD2-F418-45AE-B63B-A4BEC597BEEB'});

returnToBTT(current);

})();