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

Unable to create Folder -object in JS - only works in JSX

Contributor ,
Jun 30, 2016 Jun 30, 2016

I'm making a Photoshop panel and I have a drop-down menu that I want to populate with elements based on the number of files in a folder. However, for reasons unknown to me I seem to be completely unable to create Folder-objects in the javascript file. I've tested the below three lines of code both in my JSX file and my JS file. In the JSX one it works as it should: I get an alert dialog printing out the temp/dummy name of the folder. In JS though the entire script comes to a halt after the row creating the folder. Why is this?

alert("trying to make folder...");

var lol = new Folder();

alert(lol);

TOPICS
Actions and scripting
1.4K
Translate
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
Adobe
Enthusiast ,
Jun 30, 2016 Jun 30, 2016

var FF=  Folder(Folder.desktop + "/myFolder");

if (!FF.exists) FF.create();

Translate
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
Contributor ,
Jun 30, 2016 Jun 30, 2016

Specifying a path changes nothing for me - the object is still not created, instead the code comes to a halt before the second alert. Also doing theFolder.create() does nothing either

EDIT: I even tried hardcoding the folder path and also make sure that the folder didn't exist already, but for reasons unknown the Folder-object is still not created.

fontAbsPath = "c:/Users/martin.dahlin/Desktop/Test/";

alert("trying to make folder... fontAbsPath is:\n" + fontAbsPath); // This is correct

var fontFolder = new Folder(fontAbsPath); // Script HALTS here! 😞

if(!fontFolder.exists){

    alert("created folder because it didn't exist");

    fontFolder.create();

}else{

    alert("didn't create the folder because it already exists!");

}

Translate
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
Contributor ,
Jul 01, 2016 Jul 01, 2016

It baffles me that the very same code works in JSX but not in JS.

I noticed one difference though:
The place where the Folder creation fails is inside a callback function of CSInterface.evalScript()

var csInterface = new CSInterface();

var extensionId =  csInterface.getExtensionID();

function init(){

    themeManager.init();

    // Event listener for PhotoshopJSONCallback

    csInterface.addEventListener("com.adobe.PhotoshopJSONCallback" + extensionId, PhotoshopCallbackUnique);

    // The callback for the event listener

    function PhotoshopCallbackUnique(csEvent){

        try{

            if (typeof csEvent.data === "string"){

                var eventData = csEvent.data.replace("ver1,{", "{");

                var eventDataObject = JSON.parse(eventData);

                csEvent.data = eventDataObject;

                // Retrieve metadata and update controls

                csInterface.evalScript("myFunc()", function(result){

                    // Code

                    var fontFolder = new Folder(getMetadata("c:/Users/martin.dahlin/Desktop/Test/")); // Test

                    alert("created Folder object - it is:\n" + fontFolder); // Code halts here!!!

                   

                    // Rest of the code


EDIT: Moved the Folder creation inside it's own function and it STILL fails. Creating folders in the JS file appears to be a big no-no.

EDIT2: Even with the below code outside the init() in the JS-file breaks down the entire panel:

var test = new Folder("c:/Users/martin.dahlin/Desktop/Test/");

alert(test);

Translate
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
Enthusiast ,
Jul 01, 2016 Jul 01, 2016

The best person to help you may be id Davide Barranca:

Photoshop, etc. | Random thoughts on Photoshop, Coding and all the rest

Translate
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
Contributor ,
Jul 01, 2016 Jul 01, 2016

I figured it out.
The File and Folder -objects are an ExtendScript -thing. They don't exist in JavaScript (at least I don't think so - as you cannot even create new empty Folder objects!)

Translate
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 ,
Jul 01, 2016 Jul 01, 2016
Translate
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
Participant ,
Jul 02, 2016 Jul 02, 2016

I ran into the same problem when trying to populate my panel from scripts in my scripts folder.

The workaround I ended up with is generating an HTML file with a JSX and then loading it into the panel.

You can find my solution here:

Populating an HTML panel from folders

Translate
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
Advocate ,
Jul 06, 2016 Jul 06, 2016

Sorry for being late to the party, but the email linked to the AdobeID which I use to reply to Forum threads isn't active anymore and I really looove those green bars below my avatar

Heimdaal​, you can use the CEP "Other API" to deal with Files and Folders!

Please check here.​

Regards,

Davide

---

Photoshop HTML Panels Development Course

http://htmlpanelsbook.com

Translate
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
Participant ,
Jul 06, 2016 Jul 06, 2016

Took me a while to figure out that ReadDir() is actually capitalized readdir(), while ReadFile() is actually capitalized readFile(), nice and consistent, Adobe.

I'm assuming this is clearer with the Extension Builder installed?

Translate
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
Advocate ,
Jul 06, 2016 Jul 06, 2016
LATEST

I wouldn't use Extension Builder, as its latest update is from 2013 I'd say. If you want to explore CEP API it's easier to attach a debugger to a Panel and play with the Chrome Developer Tools console.

Davide

Translate
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