Want to script photoshop to automate things? We will talk about a few things in this guide to get you the best head start you can with VBS scripting.
Scripting or Actions?
They both are used to automate tasks but at the same time have their own advantages and disadvantages. If you wanted more of a challenge then i would use scripting. Here are some advantages of scripting:
- You can specify documents and layers with variables instead of by name.
- You can execute these scripts in applications using a simple VB compiler.
- Has many more possibilities then actions.
- Gives you a challenge sometimes.
Disadvantages:
- Much harder then simply creating an action (But its worth it).
- Can take hours of coding until you get it right...
- Can take lots of practice before you can do it effectively.
- Small errors can grow without notice.
If you want a easy way to automate more basic actions for specific layers or documents i would use actions. Here are some advantages of actions:
- Far less time consuming then scripting.
- Errors are rare and never effect your action to much.
- Don't require executables or applications like scripting.
- Much easier then scripting walls of text.
Disadvantages:
- Far less possibilities then scripting.
- If you do get a error you have to start over.
- Only uses specifically named layers and documents.
- Cant be shared to the public.(Not that i know of)
After reading these decide which one you would like to do. If you want to try scripting keep reading this guide. If you want to learn to use actions look up my guide on creating simple actions somewhere in the graphics section.
Scripting takes Time.
If your still reading that means you want to try scripting. Well let me tell you something, scripting takes a lot of time and patience. If you don't have either then it will end up consuming more time from constant pauses. You need time to test scripts and keep testing them. You need time to find your errors in your walls of text and time to figure out how to fix them. You need time to write the code itself and create the structure of the code. Luckily for you VBScript is a very basic coding language which also limits its possibilities but it still takes lots of time.
Creating our code.
Well lets start creating a basic code. Lets open Photoshop with a small piece of code.
Code:
Dim appRef
appRef = CreateObject("Photoshop.Application")
"Dim appRef" basically declares "appRef" a variable. "appRef = CreateObject("Photoshop.Application")" Makes appRef a object and that object was made "Photoshop.application".Note that this code is only used for photoshop and you cant use it to open other applications. Now lets decide what ruler units we want. Ruler units are the units we use when we want to resize a image or canvas like in Inches or Pixels. Here is the code which sets our ruler units at pixels.
Code:
Dim originalRulerUnits
originalRulerUnits = appRef.Preferences.RulerUnits
appRef.Preferences.RulerUnits = 1
"Dim originalRulerUnits" declares originalRulerUnits a variable. "originalRulerUnits = appRef.Preferences.RulerUnits" Makes the variable we just created represent Photoshops(appRef) ruler units. "appRef.Preferences.RulerUnits = 1" Is what we set the actual ruler units to but in this case "1" is pixels.
1 = pixels
2 = inches
3 = cm
4 = mm
5 = points
6 = picas
7 = percent
For example lets set our ruler units to mm(MilliMeters).
Code:
Dim originalRulerUnits
originalRulerUnits = appRef.Preferences.RulerUnits
appRef.Preferences.RulerUnits = 4
I changed the 1 to 4 in which 4 represents millimeters. These are the 2 most basic pieces of code you will use in your scripts. You can also set ruler units for a certain document to so it only applies to that document. We wont get into that though i'll just tell you that you need to make a variable that represents the document then do the same code for appRef with originalRulerUnits but instead of appRef use docRef or whatever you set the variable of the document to. Now lets create a new document with a small code.
Code:
Dim docRef
Set docRef = appRef.Documents.Add(4, 4)
This makes docRef a variable and it represents a document we just created in appRef(Photoshop). The document is (4, 4) and with the RulerUnits code we used above that means 4x4 pixels. Now we cant use this document without a layer so lets create one.
Code:
Dim artLayerRef
Set artLayerRef = docRef.ArtLayers.Add
artLayerRef.Kind = 2
This just makes a variable then makes a layer and represents it with that variable. Well that should be enough of the basics for now.
Executing our scripts.
This is very very easy to do. You can do it with a program as simple as notepad or as advanced as VB 2007 compiler. Lets start with notepad. All you need to do is put your code into notepad and go to file-> save as and save it as a vbs file then go to where you saved it and it will be a executable script file you can use anytime. Now lets use a compiler. First open any VB compiler 2005 and up (as far as i know) and put a command button into it. Give it a name that has to do with the script. Now view the buttons code and set it to "on click" and paste or type your code in then it will execute when you press that button. You just need to compile it or test it in debug mode.
"Hello World" example.
Thanks to Adobe they gave us all a example hello world script in there manual which i used to start coding.It includes some things i didn't go over but you should still hopefully understand it. Here it is:
Code:
Dim appRef
Set appRef = CreateObject( "Photoshop.Application" )
' Remember current unit settings and then set units to
' the value expected by this script
Dim originalRulerUnits
originalRulerUnits = appRef.Preferences.RulerUnits
appRef.Preferences.RulerUnits = 2
' Create a new 4x4 inch document and assign it to a variable.
Dim docRef
Dim artLayerRef
Dim textItemRef
Set docRef = appRef.Documents.Add(4, 4)
' Create a new art layer containing text
Set artLayerRef = docRef.ArtLayers.Add
artLayerRef.Kind = 2
' Set the contents of the text layer.
Set textItemRef = artLayerRef.TextItem
textItemRef.Contents = "Hello, World!"
' Restore unit setting
appRef.Preferences.RulerUnits = originalRulerUnits
Enjoy!