Right.
This is a bit confusing as the .jsx file you posted doesn't quite match the 'goal' we've been working toward so far, so the attached script still aims toward the original request, ish.
I've completely re-written it in order to keep it as easy to add or delete stuff you do or do not want.
It's commented throughout so If you want to edit it yourself you shouldn't have any trouble identifying what each section does.
In a nutshell, the script:
01. Opens a 'psd' and 'tif' file based on the filenames.
02. Extracts and copies the 'path' in the 'psd' file.
03. Pastes the 'path' into the 'tif' file. (Inc if it has a locked 'background' layer)
04. Creates a vector mask using the path. (Removes (hides) 'background')
05. Saves the 'tif' source file using the psd filename with '_modified' added to it and as a 'psd' in the selected 'tif' folder.
It doesn't name any layers 'Layer 2' nor delete any layers from the psd, in fact the psd is left untouched.
I wasn't sure if these were still a requirement.
Should be pretty simple to add though if required.
See how it goes for now.
Used PS 2022 (23.5.2.751)
Regards.
MrToM.