Forum Discussion

BrettAmstutz's avatar
BrettAmstutz
Community Member
30 days ago

Have Closed Captions generate as a variable

I am working on a project, where I have a character who is talking to the user and going into scenarios. Rather than create caption shapes and then typing all of the speaking points out and matching it to the audio on the slide, I was hoping to have/create a variable where the created closed captions could be shown in a text field or wherever else I want on the page rather than the standard top or bottom of the page. This way I could modify the way they look, where they are located, and reduce the time it takes to create conversation scenarios by not having duplicate work. 

13 Replies

  • But you would also have to create the mechanism to update it.

  • BrettAmstutz's avatar
    BrettAmstutz
    Community Member

    I'll be honest, I don't know how I would do that. Any suggestions on where to start? 

  • Nedim's avatar
    Nedim
    Community Member

    As Phil mentioned, the recommended approach is to create your own custom text variable in Storyline and then extract the captions directly from the caption container in real time as they appear.

    At the moment, there isn’t a built-in Storyline variable that exposes caption text (as far as I know). However, you can ensure captions are enabled either through the player settings or by using a trigger such as “Set Player.DisplayCaptions to True” when the timeline starts.

    From there, the remaining part can be handled with JavaScript. By observing the caption container in the DOM, you can capture the caption text whenever it appears or changes and pass it into a Storyline variable.

    Here’s an example:

    • ThierryEMMANUEL's avatar
      ThierryEMMANUEL
      Community Member

      Hello Nedim​ . I hope you're well. 🤚 I’m interested in this possibility (having subtitles in a text variable to display them in speech bubbles, for example). I tried to do it (as it seems you managed to do) with the AI assistant, but of course, neither it nor I succeeded! I did, however, guide it by showing it the ‘right’ method using the last sentence of your last paragraph (and after creating a text variable and setting ‘Player.DisplayCaptions’ to TRUE). Do you have any more tips to help me assist the Assistant? Please.

      Also, I haven’t managed to correctly retrieve “currenttime” and “totaltime” as in your example, which could be useful. I’ve made several requests to the Assistant, created similar SL variables and tried to retrieve the existing values (in the Player??), but I’ve had just as many failures in getting it right.

      Finally, do you think it’s possible NOT to display the subtitles if they’re correctly entered into a Storyline variable that’s already displayed??

      Thanks in advance. Looking forward to your brilliant replies. Thierry

      • Nedim's avatar
        Nedim
        Community Member

        Hello ThierryEMMANUEL​ 

        I was initially disappointed with the AI Assistant in Storyline. I tried it through the trial version, but I don’t use it anymore. My expectations were high. I assumed it would understand the Storyline environment, including its JavaScript API integration and DOM structure, and generate reliable code within that context.

        In practice, results were inconsistent. For more complex code, it often required multiple iterations, and even then the output was frequently redundant or overly verbose. In many cases, the same functionality could be achieved with simpler, more maintainable code. When the generated code failed, I had to rely on external LLMs anyway, which reduced its overall usefulness.

        That said, the Assistant can still be helpful for beginners, especially for simpler tasks like basic animations. However, many of those can also be implemented directly using Storyline triggers. Anyway, building more complex animations or manipulating the DOM structure and appearance can be time-consuming, and is often more efficiently handled with other LLMs. If time isn’t a priority, experimenting with it can still be useful and even enjoyable.

        I was impressed with your use of "observer" that I used unintentionally in my last paragraph. It is partial answer to your question "Do you have any more tips to help me assist the Assistant?". Prompting matters: precise JavaScript terminology improves results. Be explicit with element access (object('id') or DOM queries) and reference common methods like addEventListener, setTimeout, setInterval, map, filter, observer etc or even different Web APIs.

        For example, suppose you have six rectangles on your slide. Your initial prompt might be: “Paint all odd rectangles blue.” Let me know how many iterations it took, or if the Assistant got it right at all.

        A more precise prompt could be: “Fill all odd rectangles blue by selecting <path> elements inside elements with a data-model-id attribute. Use JavaScript array methods like filter, forEach, or map to identify and apply the fill.” I’m confident the Assistant would perform much better with this revised prompt.

        My approach is to first write a basic script with the elements and methods I expect will work, then refine, simplify, and iterate in the right direction using tools like Gemini or ChatGPT. 

        Now, the code. I had already implemented a similar solution using a MutationObserver. After reading your post, I tried the same approach by asking the Assistant to generate code using the observer method, mainly to compare results. The Assistant did produce working code, but it was unnecessarily long (around 100 lines), whereas my version was significantly shorter and more efficient.

        My code:

        (function () {
        
            const container = document.querySelector('.caption-container');
            container.style.visibility = 'hidden'; // added for Thierry									
        
            function getCaptionText() {
                const p = container.querySelector('.caption div p');
                return p ? p.textContent.trim() : "";
            }
        
            function updateCaption() {
                const text = getCaptionText();
                console.log("Caption:", text);
                setVar("Captions", text);
            }
        
            const observer = new MutationObserver(() => {
                updateCaption();
            });
        
            observer.observe(container, {
                childList: true,
                subtree: true,
                characterData: true
            });
        
        })();

        Finally, do you think it’s possible NOT to display the subtitles if they’re correctly entered into a Storyline variable that’s already displayed??

        Yes, this is achieved by applying visibility: hidden to the caption container, which keeps the element in the DOM while making it invisible—unlike display: none, which removes it from the layout entirely and can cause the script to malfunction, as the container becomes inaccessible.

        I hope this at least partially answers your questions about the Assistant and the caption-related script.