@YutMarma
I bet that syntax error comes from you using PythonScript 2, which very out-of-date version that’s still on the plugin list. I recommend upgrading to PythonScript 3, but in case you don’t have time for that, it’s pretty easy to make this script compatible with Python 2:
'''
Uses PythonScript v3.0.16 or higher: https://github.com/bruderstein/PythonScript/releases
First referenced in this Notepad++ community discussion: https://community.notepad-plus-plus.org/topic/25950/creating-two-columns-from-two-files-via-copy-paste
== SUMMARY ==
This script can paste together any number of files line-by-line.
== EXAMPLE ==
For example, if you have the following files:
----------------
----------------
file A
line 1 of file A
line 2 of file A
line 3 of file A
----------------
file B
line 1 of file B
line 2 of file B
----------------
file C
line 1 of file C
line 2 of file C
line 3 of file C
----------------
----------------
and you entered this into the first dialog:
[ 3] file A
[2 ] file B
[1 ] file C
and you clicked Yes for the second prompt
and you entered FOOBAR into the third dialog
and you clicked Yes for the fourth prompt
you would get a new buffer opened with the following text:
line 1 of file CFOOBARline 1 of file BFOOBARline 1 of file A
line 2 of file CFOOBARline 2 of file BFOOBARline 2 of file A
== NOTES ==
This script does basic data validation. Specifically, if the files you wanted to paste together have different numbers of lines, final file will have as many lines as the file with the fewest lines, assuming you click Yes on the dialog that notifies you of this.
I intentionally did not provide an option for the final file to include lines from files that are longer than the shortest file, because the result could be confusing or ambiguous.
'''
from Npp import editor, notepad
import re
def pl1of2al1of1():
all_filenames = [x[0] for x in notepad.getFiles()]
if len(all_filenames) < 2:
notepad.messageBox("only one file is open, so this script can't work")
return
# find the user's preferences
print(all_filenames)
selected_files = []
while True:
txt = notepad.prompt(("Put a whole number in the box for each file you want to combine, or hit Cancel to exit.\r\n"
"The numbers determine the order in which the lines of each file are pasted."),
'Select files to combine', '\r\n'.join('[ ] ' + fname for fname in all_filenames))
if txt is None:
return
selected_file_text = re.findall(r'^ *\[ *([+-]?\d+) *\] *(\S[^\r\n]*?) *\r?$', txt, re.MULTILINE)
print(selected_file_text)
if len(selected_file_text) < 2:
notepad.messageBox('You must put whole numbers in the boxes for at least two files')
continue
selected_nums = set(int(x[0]) for x in selected_file_text)
if len(selected_nums) < len(selected_file_text):
notepad.messageBox('Each chosen file must have a distinct number')
continue
selected_files = [x[1].strip() for x in sorted(selected_file_text, key=lambda x: int(x[0]))]
if not all(file in all_filenames for file in selected_files):
notepad.messageBox('You accidentally changed the name of a file you selected. Make sure not to enter any text, other than putting numbers in the boxes.')
continue
if notepad.messageBox(('The n^th line of the combined file will have the n^th lines of each of the chosen files in the order shown:\r\n' +
'\r\n'.join(selected_files) +
'\r\n---------------\r\n' +
'Is that what you want?'),
'confirm files and order', MESSAGEBOXFLAGS.YESNO
) == MESSAGEBOXFLAGS.RESULTYES:
break
sep_str = notepad.prompt('Enter some buffer text to put between the n^th line of file N and the n^th line of file N+1', 'Enter buffer text', '')
if sep_str is None:
return
# get the text
lines_by_file = []
eol = '\r\n'
previously_opened_file = notepad.getCurrentFilename()
for file in selected_files:
notepad.open(file)
eol = ['\r\n', '\r', '\n'][editor.getEOLMode()]
lines_by_file.append(editor.getText().splitlines())
notepad.open(previously_opened_file)
# combine the text
all_nlines = [len(x) for x in lines_by_file]
min_nlines = min(all_nlines)
if len(set(all_nlines)) != 1:
if notepad.messageBox(('Not all files have the same number of lines.\r\n' +
'As a result, all lines past line {0} will be cut off.\r\n' +
'Yes: Trim off all the extra lines.\r\n' +
'No: Cancel the operation.').format(min_nlines + 1),
'Choose what to do with extra lines', MESSAGEBOXFLAGS.YESNO
) == MESSAGEBOXFLAGS.RESULTNO:
return
combined_text = eol.join(
sep_str.join(lines[ii] for lines in lines_by_file)
for ii in range(min_nlines)
)
notepad.new()
editor.setText(combined_text)
if __name__ == '__main__':
pl1of2al1of1()