How to paste a variable from Run Real JavaScript

Hello,

New to BTT. I've looked over the documentation, and I can't seem to find what I'm after. I created a script to get the current UTC time and I want to paste that variable using a keyboard shortcut. Is that possible? I also explored creating a dynamic variable in hopes I could tackle it that way, but no dice.

Run Real JavaScript

// function to convert local time to UTC

async function getUTCTime() {
    const localTime = new Date();  // get the local time
    const utcTime = localTime.toISOString();  // convert the local time to UTC
    const formattedUTCTime = utcTime
      .replace(/:/g, '-');  // replace ':' with '-' for filename compatibility
    
     returnVariableValue(formattedUTCTime);
}

I'm not really sure how I would paste the results of the script with a keyboard shortcut with this approach. Any guidance would be appreciated.

Dynamic Variable

Dynamic Variable name:
dynamic_utc

// function to convert local time to UTC
getUTCTime() 
const localTime = new Date();  // get the local time
const utcTime = localTime.toISOString();  // convert the local time to UTC
const formattedUTCTime = utcTime
.replace(/:/g, '-');  // replace ':' with '-' for filename compatibility
    
returnVariableValue(formattedUTCTime)

I tested this script in the BTT editor and it returns the current UTC time. However, I can see where the variable shows up anywhere for use. What am I missing in this approach?

As a side question, what is the difference between returnToBTT and returnVariableValue? What do I use where?

Here is a simple example:

async function getUTCTime() {
    const localTime = new Date();  // get the local time
    const utcTime = localTime.toISOString();  // convert the local time to UTC
    const formattedUTCTime = utcTime
      .replace(/:/g, '-');  // replace ':' with '-' for filename compatibility
    
    //this will paste the variable
    await paste_text({text: formattedUTCTime, format: 'NSPasteboardTypeString'})
}

If you provide the function name in the "async function to call" you don't need to use returnToBTT or returnVariableValue (they are basically the same just in different contexts but most of the time you won't need them anymore - only in situations where you use an anonymous function and you need to return something to BTT)

The functions you can call from JS are listed here:
http://docs.folivora.ai/docs/1106_java_script.html#pastetext

Andreas,

Thanks for this. Do you have any guidance on making a dynamic variable available?