0

I am converting some of the automator workflows I made over the years into JXA and I am having some trouble with some basic functions. Firstly, I am simply trying to open a folder in finder from a specific path. I came across this link for opening a location in finder, but it does not seem to work for a folder.

In other Javascript implementations, I am used to doing something like this:

var f = new File("path/to/my/file/or/folder");
f.execute();

it doesn't seem to work in JXA. I can navigate up and down a tree using something like this:

var run = function(){
    var finder = Application("Finder");
    finder.includeStandardAdditions = true;

    return finder.startupDisk.folders["Users"].open();
}

But that seems very tedious and I don't know how to make it dynamic. Is there a way to take a POSIX path and open finder at that location?

Thanks for all the help !

Community
  • 1
  • 1

1 Answers1

2
(function () {
    'use strict';

    var a = Application.currentApplication(),
        sa = (a.includeStandardAdditions = true, a),
        fi = Application('Finder');

    var strPath = $('~/Code/')
        .stringByStandardizingPath.js

    fi.reveal(Path(strPath));

    // OR e.g.
    //fi.reveal(sa.pathTo('downloads folder'));

    fi.activate();
})();
houthakker
  • 688
  • 5
  • 13
  • You don't require the variables a and sa. What does 'use strict' do? – OrigamiEye Mar 26 '20 at 07:05
  • The standard additions reference is only needed for the pathTo alternative. Use strict, inter alia, enables a more informative set of error messages. – houthakker Mar 27 '20 at 08:44