Welcome!

Sign in with your CogSci, Facebook, Google, or Twitter account.

Or register to create a new account.

We'll use your information only for signing in to this forum.

Supported by

[solved] forcing newlines in loop variables and/or sketchpad text elements

pmheiderpmheider Posts: 2
edited January 2014 in OpenSesame

How does one force newlines in text strings that in loop variables or sketchpad text elements?

I've tried using \n in normal strings and triple quotes for multi-line strings in the python script editor. Inserting the Unicode equivalent (U+000A) works, but that seems like a weird work-around.

Comments

  • sebastiaansebastiaan Posts: 2,737
    edited January 2014

    Hi Pmheider,

    I'm afraid there's no really elegant solution for this, so I opened up a feature request: https://github.com/smathot/OpenSesame/issues/69

    But life can get easier with a little inline_script. Let's say that we use '_BR_' to indicate a newline in loop variables, like so:

    line one_BR_line two
    

    You can convert this into the proper unicode symbol by putting a small piece of code in the prepare phase of an inline_script at the start of the sequence under the loop.

    newline_mask = '_BR_'
    newline_unicode = 'U+000A'
    for var, val in self.experiment.variables.items():
        if type(val) == str and newline_mask in val:
            val = val.replace(newline_mask, newline_unicode)
            self.experiment.set(var, val)
    
    

    Whether this will work in a sketchpad depends on the back-end. Under the legacy back-end, the newline will simply be treated as an unknown symbol. Under psycho it should work.

    Thanks for bringing this to my attenion!

    Cheers,
    Sebastiaan

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

  • pmheiderpmheider Posts: 2
    edited 5:01AM

    When I first figured out that Unicode would work, I wrote a similar script to deal with newlines in the items.

    Unfortunately, a funny thing happens to newlines and U+000A's in the editor. If I save and close an inline_script containing code like this:

    reNewlines = re.compile(r'\n')

    I will get an error message because of an unclosed quote. That seems odd until you go back to the inline_script to check the problem. Here's what the code looks like now:

    reNewlines = re.compile(r'
    ')

    Oops.

  • sebastiaansebastiaan Posts: 2,737
    edited 5:01AM

    Hi Pmheider,

    Well, you're partly right. If you close the script and open it again you will see the U+000A converted to a newline (in the script). And if you then press Apply, the script will break. But if you insert the script as is and leave it untouched, it should work.

    I'm sorry, but this is the best I can offer at the moment! The issue is noted though.

    Regards,
    Sebastiaan

    There's much bigger issues in the world, I know. But I first have to take care of the world I know.
    cogsci.nl/smathot

Sign In or Register to comment.