Copy link to clipboard
Copied
Im just killing a cat with curiosity here but while going through JavaScript reference guides for other apps (ID) I notice that they use…
var = someObject;
with (someObject) {
someStatement;
someStatement;
etc.
}
I can't recall seeing this in any of the samples or help that I have received over here and Im wondering why? Is this something that is better suited to other apps? Im almost always working with either the app or a reference to the activeDocument. Im thinking this makes it much easier on the eye when looking at the syntax. Those of you with much more experience than I don't appear to do this is it more a question of style and it won't cause problems further down the line or are there pitfalls that should be avoided that I just can't see yet? Maybe scriptlistener?
Thanks…
Yes, for me it's a matter of style. Using with blocks might make the code easier to type but I find it harder to read once it gets cold.
Copy link to clipboard
Copied
The only time I have seen that in Photoshop scripts is working with dialogs to save typing.
with ( w.grp1.pnl1.btnPnl ) {
// The Ok and Cancel buttons close this dialog
okBtn.onClick = function ( ) { this.parent.parent.close( 1 ); };
cancelBtn.onClick = function ( ) { this.parent.parent.close( 2 ); };
}
It can also be used for DOM objects, strange that it's not used much in Photoshop scripts. I guess it's not as useful
with( activeDocument.activeLayer.textItem ){
// set a bunch of text settings
}
Copy link to clipboard
Copied
So it's possibly more a case of style then? rather than a performance hit or issues when using non-DOM stuff.
It does seem to save on the typing and in my case less chance of typo's too…
Thought you may have had some underlying reason that I have not happened across to date.
thanks mike…
Copy link to clipboard
Copied
Yes, for me it's a matter of style. Using with blocks might make the code easier to type but I find it harder to read once it gets cold.
Copy link to clipboard
Copied
Cool… only time will tell if I find this useful or not then
Copy link to clipboard
Copied
I'm familiar with using the 'with' statement in VB, I don't recall if I've used this in Javascript before (I assume what you posted is the Javascript equivalent of what I am used to in VB).
In VB,
document.objectRef.Shape.TextFrame.Text.fontSize = 12
document.objectRef.Shape.TextFrame.Text.font = "Arial"
document.objectRef.Shape.TextFrame.Text.xxxx = "XXX"
...etc.
can be replaced with
With document.objectRef.Shape.TextFrame.Text
.fontSize = 12
.font = "Arial"
.xxxx = "XXX"
...etc.
End With
Aside from making it easier to read (and often edit), I believe it only has to evaluate "document.objectRef.Shape.TextFrame.Text" once, instead of for each line. Not sure if any speed increase would be noticable in doing so; or if the same is true in Javascript.
Copy link to clipboard
Copied
Thanks Mark, I believe what you are doing in VB is pretty much the same thing.
It wouldn't be the first time if I was wrong though.
I don't know anywhere near enough on how JavaScript is executed in memory terms.
I have NO idea if this would mean less evaluating?
I have also seen this:
$.gc();
But don't know exactly when and where I should be cleaning up after me…
Coming over from the AppleScript side Im not used to doing the pots too… (boo!)
What I have been trying to put together recently is a collection of functions
for PS, AI & ID for pretty much all of the options I would like for all preference
objects. The 'with (object)' looked like saving me a lot of typing plus this looked
a little more like the 'tell' blocks that Im used too.
Copy link to clipboard
Copied
MarkWalsh wrote:
Aside from making it easier to read (and often edit), I believe it only has to evaluate "document.objectRef.Shape.TextFrame.Text" once, instead of for each line. Not sure if any speed increase would be noticable in doing so; or if the same is true in Javascript.
I think that is true, but the reference also has to be evaluated once with a more common method see in Photoshop javascript
var myText = app.activeDocument.activeLayer.textItem;
myText.size = 18;
myText.font = 'Some Font';
//etc
So I still think it's mostly style.
$.gc forces a Photoshop to clean up memeory. That memeory is cleared when the script ends so unless the script is very complex and runs for a long time $.gc is not needed.
Copy link to clipboard
Copied
Mike my scripts are neither of those so I will omit $.gc I don't think the guide had an example of when where & why…
Copy link to clipboard
Copied
Two rules of thumb:
- Never use 'with' statements. Use intermediate variables to save typing. 'with' statements have the potential to cause a lot of maintenance problems later on beyond the immediate problems of introducing a new name-space context.
- Use $.gc() when processing a large number of images or very large images. Before you do that, make sure that your History and Cache Levels preferences are set to something reasonable.
Copy link to clipboard
Copied
Images are typically 5200 x 3300 pixels-ish so about 50mb RGB's these are processed in batches of anything from 20 to 300 at a time.
I would image this is pretty lightweight compared to others do.
We use these at various sizes ranging from retail POS poster at 60'x40' right down to internet.
So normally each of these would end up in about 6 different states.
I usually set…
nonLinearHistory = false
numberofHistoryStates = just a few more than my script some times uses
I almost never use the clipboard with scripting
I've never tried purge();
If I did need $.gc() would this just be the once at end after processing a folder or better to keep doing this within my loop
Copy link to clipboard
Copied
purging is probably more helpful than gc.
If they are going to be useful, they would be a part of your loop. Do some timing tests to see if they make a difference.
-X
Copy link to clipboard
Copied
X,
I'm not sure I follow you. Are you saying that using 'with' changes the scope? How does it affect the namespace?
But I do agree with doing the timing test for gc. As I understand it ExtendScript already does garbage collection when it thinks it's needed. You only want to force gc when you run into problems that are not being handled by the normal gc. And that garbage collection ( repeated ) can effect performance
Copy link to clipboard
Copied
If we have code like this
function ftn(obj) {
with (obj) {
alert(name);
}
}
The interpreter will first try to resolve 'name' as a property of obj. That's one naming context/scope. If it doesn't find it there,
it looks for it as a local variable of ftn. If it can't find it there, it looks to the named parameters. And if it can't find it there,
it looks globally.
In this case, we know it's not a parameter or a local variable. It may be a property of obj or it may resolve globally to app.name (via PS 'magic').
The problem (whether or not it's called 'name') is that we can't detemine from looking at the code what the intent of the code is.
alert(obj.name) and alert(name) (without the 'with') are completely unambiguous.
-X
Copy link to clipboard
Copied
Ok, I see now. I have never seen with used that way. I don't see it much but when I do it's to set properties of the object like the examples MarkWalsh and I posted.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now