Jump to content

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


Recommended Posts

Posted

Greetings,

I'm trying to find an Alfred 3 workflow that will allow me to close all the tabs to the left or to the right of the selected tab in Safari 9.1.2. I was using a Workflow called BrowserTabs but it seems to have quite working.

 

Thanks,

Steve

Posted

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');
}
Posted (edited)
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
Posted

Thanks deanishe! I just bought you a beer.

 

I didn't totally understand your instructions because I've never created a workflow but you gave me enough and I got it working.  Thanks for for the help and for getting me to the next level of using Alfred. It seems so simple now. 

  • 4 years later...
Posted
8 hours ago, nycdude said:

Hi, does this "Close tabs to the right" code still work in safari 14?

 

It appears to, yes. Why didn't you take two minutes to test it for yourself and find out?

Posted (edited)

I don't know how to test it. I'm not a developer or anything close to that; just a user of Alfred workflows, but I will find someone to test for me. Thanks!

Edited by nycdude
  • 3 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...