Highlighted

Simple Folder Check Javascript

Community Beginner ,
Sep 24, 2020

Copy link to clipboard

Copied

Hey there,

 

i am trying to do a simple folder check.

var folder = new Folder;
folder = folder.selectDlg("Select a folder");

alert( 'isFolder: ' + Folder(folder).exists );

if(folder instanceof Folder || folder == null) {
    alert("Please select a folder");
    exit();
}
else {
    alert("Do something");
}

when i run this i get true even if i have no folder selected. For example if i on my mac go to my Desktop and do not select anything and hit open, it still tells me true.

 

any ideas?

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

In that case, try the following

 

var desktop = new Folder("~/Desktop")
var dest = desktop.selectDlg("Select a folder");
if(dest == null || dest.fsName == desktop.fsName) {
    alert("Please select a folder");
}
else {
    alert("You selected " + dest.path + "/" + dest.name);
}

 

In this code, the file browser dialog will always open with the desktop folder. The final selected folder will be in the dest variable, if the user does not select any folder then she will be prompted to select a folder.

-Manan

TOPICS
Scripting

Views

63

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Simple Folder Check Javascript

Community Beginner ,
Sep 24, 2020

Copy link to clipboard

Copied

Hey there,

 

i am trying to do a simple folder check.

var folder = new Folder;
folder = folder.selectDlg("Select a folder");

alert( 'isFolder: ' + Folder(folder).exists );

if(folder instanceof Folder || folder == null) {
    alert("Please select a folder");
    exit();
}
else {
    alert("Do something");
}

when i run this i get true even if i have no folder selected. For example if i on my mac go to my Desktop and do not select anything and hit open, it still tells me true.

 

any ideas?

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

In that case, try the following

 

var desktop = new Folder("~/Desktop")
var dest = desktop.selectDlg("Select a folder");
if(dest == null || dest.fsName == desktop.fsName) {
    alert("Please select a folder");
}
else {
    alert("You selected " + dest.path + "/" + dest.name);
}

 

In this code, the file browser dialog will always open with the desktop folder. The final selected folder will be in the dest variable, if the user does not select any folder then she will be prompted to select a folder.

-Manan

TOPICS
Scripting

Views

64

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 24, 2020 0
Adobe Community Professional ,
Sep 24, 2020

Copy link to clipboard

Copied

The reason for this behaviour is that the selectDlg will open the folder chooser window with a folder selected, so even if you don't choose any folder explicitly, the preselected folder is returned. So in your case, the Desktop folder is returned. You need to check if the user cancelled the browser window, in which case the return value would be null. Something like the following should suffice

 

var folder = new Folder().selectDlg("Select a folder");
if(folder == null) {
    alert("Please select a folder");
}
else {
    alert("You selected " + folder.path + "/" + folder.name);
}

 

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 24, 2020 0
Community Beginner ,
Sep 25, 2020

Copy link to clipboard

Copied

hmm that works for the most part. Let me explain the second part. So i have a script that when you select or create a folder, it will then populate the folder with a links folder and copy photos into that links folder. I am building a template flyer design for work. This all works great, but using the code above, if the user doesn't select or create a folder on the desktop, the file will be saved on the desktop and the links folder. I want it nicely packaged into a folder structure of its own. Does this make sense?

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 25, 2020 0
Adobe Community Professional ,
Sep 25, 2020

Copy link to clipboard

Copied

In that case, try the following

 

var desktop = new Folder("~/Desktop")
var dest = desktop.selectDlg("Select a folder");
if(dest == null || dest.fsName == desktop.fsName) {
    alert("Please select a folder");
}
else {
    alert("You selected " + dest.path + "/" + dest.name);
}

 

In this code, the file browser dialog will always open with the desktop folder. The final selected folder will be in the dest variable, if the user does not select any folder then she will be prompted to select a folder.

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 25, 2020 0
Community Beginner ,
Sep 25, 2020

Copy link to clipboard

Copied

duh! thank you... I was trying to over complicate this thing!!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 25, 2020 0