Jump to content

Need a workflow to close left tabs or right tabs in Safari


Recommended Posts

Put this in a Run Script Action with Language = /usr/bin/osascript (JS)

// 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');
}
Link to comment
And here's "Close Tabs to Right":

 

 

 

 

// Close Safari tabs to the right 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();
  // console.log('Tab ' + currentIdx + '/' + tabs.length + ' active.');
  if (currentIdx == tabs.length) {
    console.log('No tabs to close.')
    return
  }
 
  var first = tabs.length - 1,
    last = currentIdx;
  for (i = first; i >= last; i--) {
    // console.log('Closing tab ' + (i+1) + ' ...');
    var tab = tabs[i];
    tabs[i].close();
    count++;
  }
  console.log(count + ' tab(s) closed');
}

 

 

Edited by deanishe
Link to comment
  • 4 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...