def savenonamebib(bib, dirpath):
    """Given a target directory and a bib string with no filename to save it into, tries the following filenames until one is not taken : 
    _ nameinbib.bib (nameinbib = the entry name in the bib, e.g. @article{author_year=nameinbib)
    _ JACS_nameinbib.bib (JACS is replaced by the initials of the journal name)
    _ JACS_nameinbib_2.bib
    _ JACS_nameinbib_3.bib
    ..."""
    from pathlib import Path
    openbib = bib.strip().split('\n')
    for line in openbib:
        if line.strip().startswith('@'):
            name = line.split('{')[-1].replace(',', '').replace("'", '')
        elif line.strip().startswith('year'):
            year = int(line.split('=')[-1].replace('{', '').replace('}', ''
                ).replace(',', '').strip())
        elif line.strip().startswith('journal'):
            journame = line.split('=')[-1].replace('{', '').replace('}', ''
                ).lower().replace('the', '').replace('of', '').strip()
            while '  ' in journame:
                journame = journame.replace('  ', ' ')
            journame = journame.split(' ')
            temp = ''
            if len(journame) == 1:
                temp = journame[0]
            else:
                for word in journame:
                    temp += word[0].upper()
            journame = temp
            filename = name + '.bib'
            if Path(filename).is_file():
                filename = journame + '_' + filename
                while Path(filename).is_file():
                    filename = filename.replace('.bib', '')
                    if filename.split('_')[-1].isdigit and int(filename.
                        split('_')[-1]) != year:
                        filename = filename[:-1] + str(int(filename[-1]) + 1
                            ) + '.bib'
                    else:
                        filename = filename + '_2.bib'
    with Path(dirpath / filename).open(mode='w') as outputfile:
        outputfile.write(bib)
    return filename
