def get_script(tools, standalone=True, exit=True, modality='fmri'):
    """ Return a Matlab script running the provided tools. If standalone is 
        True, then the script will include SPM initialization commands and the
        resulting script can be directly fed to Matlab. If standalone is False,
        the resulting script can be loaded in SPM Batch Editor.
    """
    script = []
    if standalone:
        script.extend(["spm('defaults','{}');".format(modality),
            "spm_jobman('initcfg');"])
    for index, tool in enumerate(tools):
        script.append(tool.get_script(1 + index))
    if standalone:
        script.append("spm_jobman('run',matlabbatch);")
        if exit:
            script.append('exit();')
    return '\n'.join(script)
