[Solved] JScript AddColumn does not work, what am I doing wrong?

Have you tried restarting Opus? (Fully exit it via File > Exit Directory Opus; don't just close the window.)

If that fixes it, an old version of the script may have been stuck, tying up the thread for the script or the thread that populates its columns.

Works here, have you tried to disable and reenable the script or remove and add the columns? Or even restart dopus, although might not make a difference but worth a try.

Also specify the full path to the file and look into Escape Characters I noticed you were not making use of them. Also learn the difference between double and single quotes when dealing with escape characters. You'll know what I mean if you need to put quotes around the path to the file if there is spaces in it.

The main thing is your script works, so your issue should be pretty easy to resolve.

Could be the script is not loading, that could be for many reasons, maybe its not enabled, maybe the file is locked... We just need to work through a few steps to identify where its stuck.

  1. Edit the script and save it (just add a space some where, or increase your version number). This will trigger DOpus to reload the script. If there are issues loading the script it will tell you log window. You can also log messages via DOpus.Output in the init method, that will let you know its loading the first time.
  2. Check the script is loaded and enabled by looking in the preferences > scripts. You should be able to see your script in there with the right version number. Increment the version number in the script to ensure its reading the right file.
  3. Restart DOpus, see @leo's post above.

With these steps, if it does not best if you can be explicit as to what you tried from the points above and where you got stuck. I.E. I asked if you could see the scripts in the preferences, not sure if you answered. All good, but it helps us think about what the issue is.

Anyway don't fret, its frustrating for sure, but more the joy when you get it working. I'm sure we will get you there.

RE loading the file, The relative path worked on my PC @leo is right though, does not seem like a good pattern. If you can, I would store that in a variable or script config. But that depends on what you want to do with it.

Not meaning to contradict anything that @leo or @yonder said. I didnt see them when I started typing.

Yes, pretty much the first thing I tried was to completely restart DOpus (systray also) - did not help

yes, multiple (dozens) of times, over an over again... Both, enabling and disabling the plugin, adding/removing columns, refreshing the folder - nothing helped

Did it - did not help. Although, before it worked even with the relative path, iirc. Well, even if path was the problem - the script should write to the console always and before file error (if any) - and it simply doesn't.... It's like it doesn't work at all.

:confounded: : oh, how much I hope so... because I'm banging my head agains the wall here. I have 100TB+ of files to move while migrating my server. And these custom columns would be so much help in managing the mess StableBit DrivePool made. So I am very desperate for this to work. I could literally save me weeks or more of headaches.

Should be loaded and enabled. Athough how do I confirm that, except for the obvious ways? DOpus prefferences and ScripWizard both say it's enabled.
The file is not locked, according to LockHunter.

Yep, done that, and the version changes in both dopus prefs and ScriptWizard column. Still - doesn't work.
Actually, sometimes - there is output in the console, that script was reloaded, the other times - not even that.
But the script itself - still doe not work.

That's all javascript related, you mean? Will look into this, thanks. Though, do I currently have something wrong in the script?


Now this morning, after some coffee I got into same meaningless testing as yesterday - all the things you just told me to do, and I have done yesterday numerous times - with no results.

Then something really strange happened. By share accident. Please take a look:

Notice:

  1. Nothing happens, when I add the columns - not even a peep, while there should be console output!
  2. After addinf the columns, I refresh the folder using F5 (hard to see in this gif) - nothing happens.
  3. Then I use ScriptWizard button to go to the scripts folder
  4. And then I use the Back button - and the script suddenly works... Console output finally shows that it is "alive" at least :smile:

I mean wtf? :confounded:
What the hell is happening here? :slight_smile:

Oh, and line 51 is:

    myCustomData = JSON.parse(fileData);

What is wrong with it? JSON.Parse is the problem or what? I'm not used to this type of vague error description :smile: c#, VS and resharper had spoiled me.

You want to load the JSON file from the current folder?

Try this, I tweaked your script a little.

var myCustomData;
var doFsu = DOpus.FSUtil;

function OnInit(initData){
    
    initData.name = "TESTING - myCustom Columns";
    initData.desc = "This script is just for testing purposes."
    initData.version = "1.2";
    DOpus.Output("myCustomData loading version " + initData.version )
    initData.copyright = "(c) 2017 cryodream"
    initData.min_version = "12.5"
    initData.default_enable = true;


    var col = initData.AddColumn();
	col.name = "myCustomStatus";
	col.method = "OnMyCustomColumn";
	col.label = "Status";
	col.header = "Status";
	col.justify = "center";
	col.autogroup = true;
	col.multicol = true;

    var col = initData.AddColumn();
	col.name = "myCustomCategory";
	col.method = "OnMyCustomColumn";
	col.label = "Category";
	col.header = "Category";
	col.justify = "center";
	col.autogroup = true;
	col.multicol = true;
}

function OnMyCustomColumn(scriptColData) {
    if (typeof myCustomData === 'undefined') {
        DOpus.Output("myCustomData not found... loading from JSON")
        ReadJsonToVariable(scriptColData.item.path);
    }
    else {
        DOpus.Output("using previously loaded myCustomData")
    }

    if (typeof myCustomData === 'undefined') {
        DOpus.Output("Error: myCustomData is still null :(")
    }
    else
    {
        scriptColData.columns("myCustomStatus").value = myCustomData.Status;
        scriptColData.columns("myCustomCategory").value = myCustomData.Category;
    }
}

function ReadJsonToVariable(folderPath) {
    var jsonFile = folderPath + "\\info.json";
      if(!doFsu.Exists(jsonFile))
     {
        DOpus.Output("Error: Can't find json File, Path: " + jsonFile);
        return;
    }
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var fileStream = fso.openTextFile(jsonFile);
    var fileData = fileStream.readAll();
    fileStream.Close();
    myCustomData = JSON.parse(fileData);
}

If you add a DOpus.Output line to the OnInit function, does that produce output?

If you're using ScriptWizard in the script, try without it, to simplify things. It adds a lot of extra code which might complicated things in places you don't think to look.

Renaming the script's columns may also be worth a try, in case Opus is confused. There is something that can go wrong when making a lot of edits to column scripts which has been reported but we haven't seen ourselves or found a way to reproduce, so you may be running in to that. Renaming the columns should fix it. (The internal names of them.) But only if you can see output from OnInit.

Thank you, @wowbagger every little helps, especially helps to learn the new language.
But here's what I'm getting in console:

 Tuesday, August 1, 2017 12:28 AM cryodream_TESTING_myCustomColumns.js:  myCustomData loading version 1.2
 Tuesday, August 1, 2017 12:28 AM TESTING - myCustom Columns:  myCustomData not found... loading from JSON
 Tuesday, August 1, 2017 12:28 AM TESTING - myCustom Columns:  Error at line 64, position 5
 Tuesday, August 1, 2017 12:28 AM TESTING - myCustom Columns:  Syntax error (0x800a03ea)
 Tuesday, August 1, 2017 12:28 AM TESTING - myCustom Columns:  myCustomData not found... loading from JSON
 Tuesday, August 1, 2017 12:28 AM TESTING - myCustom Columns:  Error at line 64, position 5
 Tuesday, August 1, 2017 12:28 AM TESTING - myCustom Columns:  Syntax error (0x800a03ea)
  • The script still did not work at all, until I tried the same trick by opening the scripts folder in that tab and using back to navigate back to the folder that I'm testing. What I don't get is what's so special about this C:\Users\cryodream\AppData\Roaming\GPSoftware\Directory Opus\Script AddIns folder and why navigating back from it triggers my script to works, when nothing else does? I tried this trick by using any other path - nope, it only works on the scripts folder. So is it the ScriptWizard that influences this? Because I have ScriptWizard displaying scripts info with it's custom columns...

  • As you can see - still getting error on the line. This is consusing, and I know too litlle javascript to actually understand where the problem is... JSON.parse?

myCustomData = JSON.parse(fileData);  // this is line 64

I don't think that I do... At least not intentionally.

  • The only time I can see the output from OnInit, is when I navigate back from the scripts folders... Same as before. Sometimes, I can see after I have made an edit and saved the file, and it's auto-reloaded.
  • I changed the names of the columns and this happened. Notice the too columns with no labels at the end:

This is getting really ridiculous...

  • I have edited the code to be as simple as possible - just to test the columns - no json, anything, take a look at the code below to see how simple it is.
  • And look at the image below and notice, how I do get dopus output from column values - they have been set - but still - nothing shows up in the dopus lister for these columns.
  • And, obviously the oldie but goodie - ONLY works at all, when navigating back from scripts folder, which is so weird.

And the code:

var myCustomData;

function OnInit(initData){
    
    initData.name = "TESTING - myCustom Columns";
    initData.desc = "This script is just for testing purposes."
    initData.version = "1.07";
    DOpus.Output("myCustomData loading version " + initData.version )
    initData.copyright = "(c) 2017 cryodream"
    initData.min_version = "12.5"
    initData.default_enable = true;


    var col = initData.AddColumn();
	col.name = "myCustomStatus_001";
	col.method = "OnMyCustomColumn";
	col.label = "Status 001";
	col.justify = "center";
	col.autogroup = true;
	col.multicol = true;

    var col = initData.AddColumn();
	col.name = "myCustomCategory_001";
	col.method = "OnMyCustomColumn";
	col.label = "Category 001";
	col.justify = "center";
	col.autogroup = true;
	col.multicol = true;
}

function OnMyCustomColumn(scriptColData) {
    if (typeof myCustomData === 'undefined') {
        DOpus.Output("myCustomData not found... loading from JSON")
        myCustomData = {Status: "work", Category: "whatever"};
    }
    else {
        DOpus.Output("using previously loaded myCustomData")
    }

    if (typeof myCustomData === 'undefined') {
        DOpus.Output("Error: myCustomData is still null :(")
    }
    else
    {
        scriptColData.columns("myCustomStatus_001").value = myCustomData.Status;
        scriptColData.columns("myCustomCategory_001").value = myCustomData.Category;

        DOpus.Output(scriptColData.columns("myCustomStatus_001").value);
        DOpus.Output(scriptColData.columns("myCustomCategory_001").value);
    }
}

This is some really annoying bull■■■■... :smile:

That script v1.07 worked on my pc. Do you have multiple scripts with the same script name in the addins folder? Multiple versions perhaps, some older test ones and a newer one?
Maybe its loading an old script or getting confused between scripts?

The two empty columns are not unexpected when you are doing dev, when you save the script the columns might need to be removed and re-added.

Confirm you don't have multiple versions of the same script in the addins folder, Remove and re add those columns. Else I'm kinda stumped, sorry mate.

Do other custom columns work, from a known working script.

@wowbagger thank you so much for your patience and help.

I am an idiot.

If you look at my last screenshot, you actually may see the problem :smile:
And it's so stupid, I'm really embarrassed to admit...

The view was in list mode not details, notice how none of the columns show anything :blush:

I was so frustrated with script not working at all, even when I simplified it to the minimum, and it worked - I didn't notice that the lister was in List mode, my favorite and default mode.

I'm really sorry for wasting your time on my last post :slight_smile:

2 Likes

haha, nice one. Thanks for sharing mate.

:wink:

Is your plan to read the JSON file from a child folder and display the value next to the parent?
I can see value in a generic script that might do that. Similar to the regexp columns scripts. Except acting on a folders content not file names. I am sure something similar has been requested before.

@wowbagger @Leo

I had almost a full day to reflect on all this situation and made some very interesting observations...
I think, when people will discuss and debate this "columns not working at all in DOpus" covfefe in the future, a couple of things will be clear:

  • Nobody will care, and who could blame them, that a poor guy (me) made a teeny-tiny mistake, which finally, as it turns out, was not the main story here.
  • The main story was the people who "tried" to help.

The facts are simple. Both pictures, even the first one, that I posted yesterday morning - they both clearly show that the lister was not in details mode. In light of this, there are only 2 possible conclusion to be made here:

  • Either, the people trying to help, were not so bright after-all, that they could miss such an easy to spot mistake.
  • Or, which is way worse, they deceived me by making me think they actually cared... But in reality, they didn't even look at the pretty pictures. One was even animated. How cool is that?

Dear me, how embarrassing must be for them, now that people will see their true nature...

P.S.
My lawyers told me to add, that this is actually an attempt to make a joke. I have no idea why. I think this is some serious stuff right there...

I've read this topic, which is exactly dealing with JSON problem and it seems it was solved when JScript was updated to 5.8

I added the versio check for javascript and it is 5.8:

DOpus.Output("jscript version: " + ScriptEngineMajorVersion() + "." + ScriptEngineMinorVersion());

result:

jscript version: 5.8

Then how come this line:

myCustomData = JSON.parse(fileData);

still gives me this error:

Error at line 67, position 5
Syntax error (0x800a03ea)

I tried googling the error code, it may have something to do with windows 8.1 and IE 11, but it's over my head... :confused:

Any help would be appreciated...

The third option is they had all successfully blocked from their mind all memory of list mode! :wink:

2 Likes

Try this script column-custom-text-and-regexp.

It stores config as JSON in a variable. If you can open the dialog add a couple of columns and apply the changes. Then your JScript supports the JSON command and the issue is with what you are passing to that command.

I understand you are working with JSON files. However I liked the idea of having a generic script that would display columns for a folder by reading a file within that folder.

I extended this XML/XPATH script to support doing that. You can use it to create custom columns that should do what you need. You will need to replace your info.json, with an info.xml.

@wowbagger thank you so much for all your help. I actually have managed to work it out what was going on and why it wasn't working for me. Brace yourself :slight_smile:

So, after about a whole week trying to work this shit out:

  • I switched from vbscript to jscript (actually I didn't know either of them, so both were a learning curve... although jscript syntax is way more understandable and easier to use, I must say)
  • I rewrote the script maybe 10 different ways to try things out
  • I even reinstalled windows (switched from 8.1 to 10)
  • nothing helped, because it seems JSON.parse was refusing to work on my system for whatever reason

Then I accidentally found out why. And the reason was... drum roll... invisible/hidden garbage chars in my json file:

{
  "Status": "done",
  "Category": "move"
}

At least I assume that this was the culprit.
I have no idea how that garbage got there, because I can't remember exactly what editor I used to create the file. The list of editors I could have used:

  • notepad
  • VS Code
  • Sublime Text
  • Visual Studio
  • WebStorm

Or maybe even some other editor/IDE, but probably it was one 5 above. My money is on that f****er Notepad.
Was this because of text encoding, or something like that? I am kinda interested how or why this would happen.

Well, live and learn. You guys asked me what is in that f***ing file. Well, I looked at it in a text editor - and those chars never showed. Until I simply read the file and wrote to output :slight_smile: As simple as that.

JSON.parse works as it should and my script finally is complete.

Sorry, that I could not write this down yesterday, when I found this. But like I said before - I am in the middle of migrating my server, so I was busy with that.

Thanks so much, anyone, who tried to help. This community is awesome.
@wowbagger you rock, dude, thanks again.
I will most certainly look into your XML and XPath columns script, that sounds like exactly what I was going after next :slight_smile:

1 Like

That looks like it might be a UTF-8 BOM.

You are probably right. After you said this, I think I remember now making this json file in Notepad and saving it specifically as UTF-8.

I had to google what the hell is that BOM :smile: Never actually had a problem like this before.

Please cut out the swearing, especially in huge bold headings. People read the forum at work and it can cause problems for them.

Sorry, it was just a very accurate description of how I felt after a week of hair-pulling :slight_smile: