How to check whether a folder exists on JXA?
I would like to use Javascript for Automation to check whether a given folder exists. Does anyone know how to do that?
I would like to use Javascript for Automation to check whether a given folder exists. Does anyone know how to do that?
The last example was not exactly correct:
ObjC.import('Foundation')
var app = Application.currentApplication();
app.includeStandardAdditions = true;
var path = Path('/Users/yourname/Desktop/Test_Folder').toString();
app.displayAlert(path);
isDir=Ref();
$.NSFileManager.alloc.init.fileExistsAtPathIsDirectory(path, isDir);
app.displayAlert(isDir[0]);
The last example was not exactly correct:
ObjC.import('Foundation')
var app = Application.currentApplication();
app.includeStandardAdditions = true;
var path = Path('/Users/yourname/Desktop/Test_Folder').toString();
app.displayAlert(path);
isDir=Ref();
$.NSFileManager.alloc.init.fileExistsAtPathIsDirectory(path, isDir);
app.displayAlert(isDir[0]);
var app = Application.currentApplication()
app.includeStandardAdditions = true
var path = Path('/Users/yourname/Desktop/Test_Folder');
var finderApp = Application("Finder");
var status = finderApp.exists(path);
app.displayAlert(status);
This is the simplest approach. It can also be done as:
ObjC.import('Foundation')
var app = Application.currentApplication()
app.includeStandardAdditions = true
var path = Path('/Users/yourname/Desktop/Test_Folder');
isDir=Ref()
$.NSFileManager.alloc.init.fileExistsAtPathIsDirectory(path, isDir);
app.displayAlert(isDir[0]);
In the latter case, it will be true if the object at the end of the path is a directory, and it exists, otherwise false is returned.
I found the solution before your answer but still thanks! :)
How to check whether a folder exists on JXA?