Tag Archives: Javascript

Using Objects like Arrays in JavaScript.

In some situations, there can be a lot of advantages of using Objects instead of Arrays in JavaScript.
But some of programmers just don’t like objects! Here I don’t want to enter this debate about why or what! I just want to show you how to use a JavaScript Object like an Array!

So one of the very useful methods of Arrays (in JavaScript! otherwise it’s just the opposite side let’s say in Java for example!) is that you can get and set the length of any array any-time you want! then you can iterate between items inside the array and do literally any thing you want (you can do this because you know the length!).

And you can do the exact (well in pragmatic point of view) same thing in new versions of JavaScript with Objects too! using the Keys function:

Object.keys(obj);

But you should know that: first of all this wasn’t there in older versions of JavaScript (So your code wouldn’t function properly in old IE browsers for instance) and secondary, in proprietary / customized versions of JavaScript that aren’t that fast to get up to date with the advances of JavaScript, you won’t probably have it even in the future! (for example in Adobe products they are still using VERY old versions of JS in their VERY modern products), so what can you do about it?

Here is the solution!
If your problem is old browsers / IE, you can use sophisticated Amazon solution which will make the functionality available in most of the older browsers /IE:

Please note that you can use this function in other environments as well, but it would be a little bit over-kill since in embedded systems for example you may know most of the times the version of JavaScript and if it’s available or not so you don’t need to and probably shouldn’t check it!

And for the environments, where you know that uses an older version of JavaScript and/or this function doesn’t exist, you can write a very simple function yourself that will tell you the length of the object properties and the key values (since knowing only the length won’t help you, since you can’t access properties by index!) like so:


Here by passing an object to this function you will get back an array of Object property keys and of-course you can get the length from it like a simple array as well!

As you see it is a super simple and very short function!

See you later, mim, 😉

manage paths in Adobe Extend-Script

Adobe Exten-Script (source: internet)
Adobe Exten-Script (source: internet)

Managing paths obtained from Adobe Extend-Script, Specially when you want to share these paths with an external 3th application (to do things which aren’t possible with just ExtendScript) is kind of difficult!
For instance let’s say we want to get the Path of an In-Design file being opened (and in front) in in-Design, we may simply do:


And get:

alert(app.activeDocument.filePath);
alert(app.activeDocument.filePath);

But there is obviously 3 (+1 not obvious here!) possible problems with this path (even if you can transfer that variable as easily as shout it out in an alert! look at #3):

  1. the Tilde (~) character at the beginning, an environment variable meaning “Home Directory“, which is not a recognizable standard character in all the different programing environments (for instance it’d be %~dp0 in Batch scripting and user.home, (more or less!) in Java) although it is supposed to be the absolute path using above code!
  2. existence of Slash characters (/) in the path instead of Backslash (\), making it hard (not so much tho!) to send it through a JSON package for example (or save it in a batch file -again- for instance), be-cause in most environments it’d be considered as a escape character and not a path level separator.
  3. It is an object and not a string (apparently!) and you can send, save or sometimes even use the myPath variable for instance directly! (it will give you an [object] instead of the path, which I guess should be the parent File object itself)
  4. in addition if our file wasn’t in our Desktop folder and was in a disk drive instead, we would have this problem too:
    alert(app.activeDocument.filePath);
    alert(app.activeDocument.filePath);

    as you can see, an additional Slash character (/) before the Drive letter (c)! which is incomprehensible to me why it is!! but it seems that Extend-Script itself doesn’t have any problem with it, but as I said when you want o communicate with 3th party apps it will become a problem immediately!

  5.     So as you may guess there is a lot of solutions for any and each of mentioned problems, but here I just wanted to suggest the most convenient one I figured out after trying a lot of different shenanigan techniques!
    Here we used Regular Expression Patterns to Replace() all (by using the g pseudo) the Slashes with Backslashes and by using fsName instead of filePath eliminating other problems (like getting “relative paths” (or fake absolutes!) instead of absolutes and the beginning slash character)
    And the result:

    alert(app.activeDocument.fullName.parent.fsName).toString().replace(/\\/g, '/');
    alert(app.activeDocument.fullName.parent.fsName).toString().replace(/\\/g, ‘/’);

    Awesome! 😀
    Please let me know if you have a better idea to this or if you see a problem or something, I appreciate it 😉
    mim,