assurance-tunnel
assurance-tunnel
assurance-tunnel
assurance-tunnel

Use Javascript to select a line of Text


  • Tech Staff

    I recently had a user trying to add quick titling to a show.
    He was using the old Text/ure actor (cpu only) and having some troubles.
    As a solution I put together this VERY short Javascript script.

    Inputs:

    1. TEXT (MultiLine from 'Text' actor, or other source)
    2. Integer (the line to output, Zero indexed)

    Output:

    1. the single line of Text you selected

    ================================================

    function main()
    {
    // arguments[0] = text in
    // arguments[1] = line to select

    var lines = arguments[0].split("\r");  // note "\n" is another possible option
    return lines[arguments[1]];
    }

    ================================================



  • @dusx said:

    or other source

     And from an external text file?

    best wishes

    bonemap


  • Beta Gold

    @bonemap said:

    @dusx said:
    or other source
     And from an external text file?



     Yes, that'd be great. I'm sure you can whip that up in a trice can't your Ryan?!
    I've been using this excellent actor, but a Javascript solution seems more elegant...


  • Tech Staff

    And now from an external file

    I recently had a user trying to add quick titling to a show.
    He was using the old Text/ure actor (cpu only) and having some troubles.
    As a solution I put together this VERY short Javascript script.
    Inputs:
    1. Integer (the line to output, Zero indexed)
    Output:
    1. the single line of Text you selected
    ================================================
    function main(){


    // arguments[0] = line to select
    var txt = read ("file.txt");
    var lines = txt.split("\r");  // note "\n" is another possible option
    return lines[arguments[0]];
    }
    ================================================
    Oof course this is only reading the hard coded file name.
    Just use 2 inputs if you want theo pass in the filename

    My phone was making this difficult 😐 

    [EDIT] Addition (use dynamic text to load file):

    Inputs:

    1. Text file name or path (requires either a relative path, or absolute path)
    2. Integer (the line to output, Zero indexed)

    Output:

    1. the single line of Text you selected

    ================================================

    function main() 

    // arguments[0] = text file name or path

    // arguments[1] = line to select

    var txt = read (arguments[0]);

    var lines = txt.split("\r");  // note "\r\n" is another possible option, this may be operating system dependant

    return lines[arguments[1]]; 

    }

    ================================================


  • Beta Gold

    @DusX

    Thanks for that. Brilliant.
    But I'm having the oddest experience with this: Here's the Isadora test file I created:
    Javascript test file

    and the associated text file
    Which should go in the same directory as the Isadora file.

    Isadora loads the file fine, but the second and subsequent lines of the text are placed in a lower position on the stage than the first line.
    I've tried encoding in ANSI and in UTF-8 in case that's the issue, but there's no difference...
    Am sure it's me doing something wrong, but I'd be grateful if you could have a look and see if you can spot the problem.
    Cheers
    Mark






  • Tech Staff

    @mark_m

    I can dig in right now, but I did look at your txt file and your lines end with CR LF (opened notepad++ and set view / symbols to all)

    So I suggest changing the line break to use the \r\n option as listed in the comments.

    The split is probably removing the  \n and leaving the \r on the beginning of the next line.


  • Beta Gold

    @dusx

    Yes, I see... (I mean I see the CR LF thing, I don't speak Javascript well enough to really understand). Anyhow, by making the first line blank - or to be precise containing nothing but a CRLF -  I can get a consistent placement of all the lines of text from line 2 onwards.
    Thanks!
    Mark (not...)





  • Tech Staff

    @mark_m

    Does it not work as expected if you change the line:

    var lines = txt.split("\r");

    to 

    var lines = txt.split("\r\n");


  • Beta Gold

    @dusx

    Brilliant, Ryan, that did the trick.

    Thanks!


 

Looks like your connection to TroikaTronix Community Forum was lost, please wait while we try to reconnect.