Open currently selected files in Sublime Text

I want to open selected finder files in Sublime Text despite what the default app for opening is set to for these files.

I kind of hoped this would work but no.

Thank you for any help.

Various ways – from a JavaScript for Automation action, you could try something like:

(function () {
    'use strict';

    var a = Application.currentApplication(),
        sa = (a.includeStandardAdditions = true, a),
        strCMD = Application("Finder")
        .selection()
        .reduce(function (a, x) {
            return a + 'open -a "Sublime Text" ' + x.url()
                .slice(7) + '\n';
        }, '');

    sa.doShellScript(strCMD);

    Application('Sublime Text')
        .activate();
})();

(Which should open each selected document in a separate Sublime Text window – if you have the Sublime Text command line tool installed, you can adjust strCMD above to open them all in the same instance)

1 Like

https://www.sublimetext.com/docs/2/osx_command_line.html

This is perfect. Thank you. :heart:

Updated the script to :

`(function () {
‘use strict’;

var a = Application.currentApplication(),
    sa = (a.includeStandardAdditions = true, a),
    strCMD = Application("Finder")
    .selection()
    .reduce(function (a, x) {
        return a + 'subl" ' + x.url()
            .slice(7) + '\n';
    }, '');

sa.doShellScript(strCMD);

Application('Sublime Text')
    .activate();

})();`

But I get an error. I just changed 'open -a ’ with ‘subl’ cli tool

The details depend on where you have installed subl – you should give the full path to it in the script.

https://www.sublimetext.com/docs/3/osx_command_line.html

In my case, it would need to be:

(function () {
    'use strict';

    var a = Application.currentApplication(),
        sa = (a.includeStandardAdditions = true, a),
        strCMD = Application("Finder")
        .selection()
        .reduce(function (a, x) {
            return a + '/usr/local/bin/subl "' + x.url()
                .slice(7) + '"\n';
        }, '');


    //return strCMD
    sa.doShellScript(strCMD);

    Application('Sublime Text')
        .activate();
})();

Incidentally - watch the quotes (single and double) I think there may be a quote glitch in your first draft.

1 Like

Oh, didn’t know we had to give the full path. It works really great now. Thank you once more. :slight_smile:

1 Like