Let consuming scripts know if and how they ran in KBar

Issue #105 resolved
Rafi Khan created an issue

Scenario: Script authors want the ability to have a single script that can do multiple things and invoke that behavior from different KBar Buttons. The easiest/most elegant way to do this is to expose a variable that consuming scripts can check and switch their behavior against.

Suggestion by Lloyd: Expose a global kbar variable that can be probed by a consuming script so it can modify its behavior.

Official response

  • Rafi Khan reporter

    Here's what it will look like:

    var isKBarRunning = (typeof kbar !== 'undefined');
    
    if (isKBarRunning && kbar.button)
    {
       var version = kbar.version;   // '1.1.1'
    
       var button = kbar.button;  // Make a local copy of the kbar variable and button.
    
       var id = button.id;      // '4d8026d4-956c-4233-8730-3ea2abfaad35'
       var name = button.name;  // 'Show off a sample scriptlet'
       var argument = button.argument; // Any string value
    
       switch(button.argument)
       {
          case 'Function 1': 
             doSomething(); 
             break;
    
          case 'Function 2':
             doSomethingElse();
             break;
    
          default:
             alert('I dont know what this button is');
             break;
       }
    }
    else
    {
       // Run as normal
    }
    

Comments (16)

  1. lloydalvarez

    Per Rafi's suggestion, the consuming script can check it like this:

    if (typeof kbar !== 'undefined' && kbar.button.title === 'behavior_A') {
       // do the special thing
    } else {
       // act normal
    }
    
  2. Rafi Khan reporter

    Here's what it will look like:

    var isKBarRunning = (typeof kbar !== 'undefined');
    
    if (isKBarRunning && kbar.button)
    {
       var version = kbar.version;   // '1.1.1'
    
       var button = kbar.button;  // Make a local copy of the kbar variable and button.
    
       var id = button.id;      // '4d8026d4-956c-4233-8730-3ea2abfaad35'
       var name = button.name;  // 'Show off a sample scriptlet'
       var argument = button.argument; // Any string value
    
       switch(button.argument)
       {
          case 'Function 1': 
             doSomething(); 
             break;
    
          case 'Function 2':
             doSomethingElse();
             break;
    
          default:
             alert('I dont know what this button is');
             break;
       }
    }
    else
    {
       // Run as normal
    }
    
  3. lloydalvarez

    This is great and I love that you also have an ID as this will allow users the freedom to name the button whatever they prefer but I can't find where i can assign an id to the button in the KBar settings.

  4. lloydalvarez

    Also, for a "Run JSX" button, i am not able to change the name either so not sure how this would work at all unless the user needs to make a scriptlet button?

  5. lloydalvarez

    One more issue, i am running 1.1.0

    But when I query the object running in Global space, it says it's version 1.1.0 and the buttons object is empty:

    Screen Shot 2017-11-19 at 11.24.18 AM.png

  6. Rafi Khan reporter

    Button Id - The ID itself is auto generated when you make a button and is only available in the toolbar json file. Do you think asking an author to dig through that file is asking too much or does it need to be exposed explicitly somewhere in the UI? I'm trying to avoid exposing UI for these advanced scenarios unless absolutely necessary.

    JSX - Ah. You're right. I was only testing in scriptlets. I'll need to expose a button name for it. Will try to get that in today.

    Button and ID being null - The button property is only set when you click the button and cleared immediately after it runs. I do this to prevent buttons from accidentally polluting each other and leaving the possibility to expose services to the button later on. If you need it at runtime just do this: var button = kbar.button; and the data will stick around.

    Version issue - The 1.0.0 was a typo in the script. Will fix that in the next drop.

    ZXP installer - When I bump the version it always makes a new row for each product I've made. I'm not sure how else to modify the version such that it upgrades/replaces the old version. Do you know any trick to manage this?

  7. lloydalvarez

    Button Id - The ID itself is auto generated when you make a button and is only available in the toolbar json file. Do you think asking an author to dig through that file is asking too much or does it need to be exposed explicitly somewhere in the UI? I'm trying to avoid exposing UI for these advanced scenarios unless absolutely necessary.

    This would still require the author to know a constant name in order to find the id. I guess it's not that big of a deal to force the user to use a specific name to make this work since the icons are customizable. When we switch to using the metadata and custom icons we can switch to using the internal id which you can do behind the scenes and the user can then rename the button whatever she wants.

    JSX - Ah. You're right. I was only testing in scriptlets. I'll need to expose a button name for it. Will try to get that in today.

    Ok great, as this is the application I am looking to use with Ray Dynamic Color and Texture.

    ZXP installer - When I bump the version it always makes a new row for each product I've made. I'm not sure how else to modify the version such that it upgrades/replaces the old version. Do you know any trick to manage this?

    Let me look into this and get back to you.

  8. lloydalvarez

    ZXP installer - When I bump the version it always makes a new row for each product I've made. I'm not sure how else to modify the version such that it upgrades/replaces the old version. Do you know any trick to manage this?

    Checked on this and it should overwrite the old version. Did you use a different product ID in the extension manifest?

  9. Rafi Khan reporter

    Checked on this and it should overwrite the old version. Did you use a different product ID in the extension manifest?

    The product id is the same. I only bumped the version number.

    Btw, I emailed you an updated drop exposing the name of the JSX(BIN) as an editable field. You can modify it now.

  10. lloydalvarez

    Btw, I emailed you an updated drop exposing the name of the JSX(BIN) as an editable field. You can modify it now.

    Works beautifully!! http://share.aescripts.com/2t3u2F3j3r2l

    Screen Shot 2017-11-19 at 11.24.18 AM.png

    One more feature request. Since I now be common to link to the same script many times for each individual function, having a duplicate button in the settings will be a nice time saver.

  11. Remco Janssen

    Would it be possible to send some data/arguments along? Right now one would have to extract if from the button name, which might not be ideal.

  12. Rafi Khan reporter

    Copying/pasting buttons:

    I get this request a lot. It's on the list for the next release.

    Passing Data/Arguments

    Yes, it's possible and planned. In fact, it's the 'proper' way to do this feature. It requires more work/testing and I didn't think I could get it done in time for cybermonday. So instead I did the minimal safest change to support the scenario. I'll extend the support for args in a later release.

  13. Log in to comment