In Part 1 of this two-part tutorial about scripting Photoshop, we covered some of the basics of using scripts, as well as the many resources available. In this second part, we'll create a practical, real-world script from scratch.
As mentioned in the Introduction to Scripting Photoshop, toggling the visibility of a layer (on and off) is an example of conditional logic, something for which scripting is ideally suited. So, let's see how you would write such a script from scratch.
For this project we'll use JavaScript because it's the only cross-platform scripting language supported by the Creative Suite; however, you could just as easily write a similar script using AppleScript for Mac, or VBScript for PC.
Tip: Although you can write Photoshop scripts using JavaScript, AppleScript, or VisualBasic Script, JavaScript has two distinct advantages: it's platform independent (i.e., it works on both Mac and PC), and many Web designers are already familiar with it.
Tip: So what's the difference between a ".js" and a ".jsx" file? Fundamentally, nothing; they're both JavaScript files/scripts. However, if you double-click on a ".js" file in Apple's Finder or Windows' Explorer, it'll likely open in your browser (and display an error dialog). But if you have any of the CS3 applications installed, ".jsx" files will be associated with ExtendScript Toolkit. (For more information about ExtendScript Toolkit, see Chapter 9 of the JavaScript Reference Guide.)