• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Simple Folder Check Javascript

Community Beginner ,
Sep 24, 2020 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?

TOPICS
Scripting

Views

355

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
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 25, 2020 Sep 25, 2020

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

...

Votes

Translate

Translate
Community Expert ,
Sep 24, 2020 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

Votes

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
community guidelines
Community Beginner ,
Sep 25, 2020 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?

 

Votes

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
community guidelines
Community Expert ,
Sep 25, 2020 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

Votes

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
community guidelines
Community Beginner ,
Sep 25, 2020 Sep 25, 2020

Copy link to clipboard

Copied

LATEST

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

Votes

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
community guidelines