Close tabs to left and right of current tab : Safari help

I found this script online that will close tabs to the left of the safari tab :

Here is the code :

// Close Safari tabs to the left of the active one.

ObjC.import('stdlib')

function run(argv) {
var count = 0,
safari = Application('Safari'),
win = safari.windows[0],
tabs = win.tabs,
currentIdx = win.currentTab.index();

if (currentIdx == 1) {
console.log('No tabs to close.')
return
}

// start at currentIdx - 2 due to 0-indexing in JS
for (i = currentIdx - 2; i >= 0; i--) {
var tab = tabs[i];
tabs[i].close();
count++;
}
console.log(count + ' tab(s) closed');
}

This however doesn't work at all. What is wrong with it? And also I would really like to preserve my pinned tabs if I can.

Thank you for any help.