Skip to main content
zsaaro
Inspiring
August 5, 2021
Answered

A friendly reminder to wrap your scripts inside a function or it will clash with other scripts

  • August 5, 2021
  • 2 replies
  • 576 views

Hey everybody. I just solved an issue where my scripts worked just fine, but when running together some global variables actually override each other and crashed the script that launched earliest.

I didn't know this could happen, it can also happen with scripts that are completely unrelated to each other, which means the people who will use your scripts may encounter issues you never will simply because they have other scripts installed that lead to the same issue.

 

What solved it for me was making sure the entire thing is wrapped inside a function that launches itself, like this:

 

(function(){

 // your script goes here

}();

In my case my script is a whole folder of files to make it easier for me to work, so just make sure all your includes or evalFiles are inside this function, like so:

(function(){
   #include "helpers.js"
   // your script goes here
}();

If you are making a dockable ScriptUIPanel, in that case you need to reference the document, you can do so by passing it as an argument to the wrapper function like so:

(function(DOM){
   #include "helpers.js"
	var windowPanel = DOM instanceof Panel ? DOM : new Window("palette", "My Script", undefined, {resizeable: true});
});

(In case this is confusing to you, think of DOM as a variable which we assign the document as its value (by passing 'this' at the end of the wrapper function). Then we can use it to declare a window that can be dockable when the script is in the ScriptsUI Panels folder)

 

Please share some information in the comments if you experienced this before as I was banging my head against the wall trying to find a solution and a Google search brought up pretty much nothing, so your comments or information may help other people find this post in the future. Thank you!

 

This topic has been closed for replies.
Correct answer zsaaro

Correction: the Dockable ScriptUIPanel example (the last example) should pass 'this' as an argument to the function, like so:

(function(DOM){
   #include "helpers.js"
	var windowPanel = DOM instanceof Panel ? DOM : new Window("palette", "My Script", undefined, {resizeable: true});
})(this);

 

2 replies

Mathias Moehl
Community Expert
Community Expert
August 6, 2021

Thank you for this post!

Really surprising that there is not much info about this so far.

From my daily experience with doing customer support for all our extensions and scripts I can add that there are also some scripts out there which modify existing JavaScript objects and hence can cause your scripts to stop working. Here is my usual support reply in these scenarios:

______________

This sounds like another script is corrupting your scripting API.
Could you please
1. close all other scripts and extension panels you have open in your user interface
2. restart Ae
3. try if the script works fine now

If it is working now, please launch all your other scripts again one by one and after launching them close and open my script again to check if it still works. If my script stops working after launching one of the other scripts, this script must be the bad guy that causes the trouble. In that case, please contact the author of this script and let me know which one it is, such that I can contact the author, too.

---------

 

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
zsaaro
zsaaroAuthorCorrect answer
Inspiring
August 5, 2021

Correction: the Dockable ScriptUIPanel example (the last example) should pass 'this' as an argument to the function, like so:

(function(DOM){
   #include "helpers.js"
	var windowPanel = DOM instanceof Panel ? DOM : new Window("palette", "My Script", undefined, {resizeable: true});
})(this);