def script_code():
    """
    Return the source code of a module running as '__main__'. Acquisition scripts can use this to save their code.

    If attempting to load the source code raises an exception, return a string representation of the exception.

    Returns
    -------
    str
        The code, with lines separated by newline characters.
    """
    try:
        return inspect.getsource(sys.modules['__main__'])
    except Exception as e:
        return str(e)