
|
    ![TRASH [Used to be STORE]](../../navigation/menu_trash_off.gif)
| |
|
|
Introduction. With new release of Photoshop 7 Adobe also released SDK kit which allows you to control Photoshop with simple VB Scripts.
It's about time that Adobe did this, they should do this long time back. Let's take a look how you can use this ability for automating you daily tasks.
Actions vs. Scripting:
Actions are also very nice feature but they are very limited just for basic functions while with scripting you can do almost everything like with mouse (or tablet:). But the main difference is that you can drive script with custom parameters inserted for example via HTML document e.g. Internet Explorer. At the top of this is fact that you can also run actions from script which gives you unlimited posibilities.
I'm going to show you how you can use Visual Basic Script to make a new header for your Homepage. Take a look at following image. That's the final result of what we want to get from Photoshop.

|
|
1. Preparing PSD in Photoshop. First we gonna start in Photoshop. Make a new file which has all layers and stuff you want to have in header. Merge all of them except the Text layer with some generic header (like - My Header or Header 1. It doesn't really matter what you put there). Give it a name. In our case we name it textLayer.

Important thing which you have to consider, is if your header will always have the same width or this will change depending on the amount of text. We are going to describe second situation which is little bit trickier. First, you need to do Canvas Size and make the width of canvas at least 600 or more pixels. This will guarantee us that you can write as long headers as you want. Save the file to let's say
c:\temp\my_file.psd
|
|
2. Creating form in HTML. Second thing we have to do is to prepare environment for the script. We will have to send some variables so let's take a closer look at them.
Create new html document. I recommend to write all this stuff in non-wysiwyg editor such as Notepad, or Editplus. Create Form and three Inputs.
<img src="my_header.gif" width="223" height="45" border=0 alt="My Header">
You can place sample finished header here so you always new which header are you editing.
<form method=post action="" id=Form1>
Make a new form. Important here is to give it an ID. I chose Form1.
<input type="text" name="" id="abc"><br>
Into this first Input we'll write our header. Also don't forget to give it unique ID.
<input size=50 type="text" name="" value="c:\temp\" id="abc2"><br>
This input will serve as Save as command. Basically you will write there a path where you want your header to be saved. I wrote a predefined path there, so you don't have to write always c:\....
<input type="submit" onClick=prepareScript() value="Make">
</form>
Finish the form with third input, which is actually submit button. We will use prepareScript() function at onClick event.
|
|
3. prepareScript() function.
Since we are using VBScript, our script tag will be slightly different than usual JavaScript. Don't forget to put Script tag inside Head.
<script language="VBScript">
<!--
//-->
</script>
This is our starting point. Everything from now you will write inside of this tag.
I divided whole script into two Functions: prepareScript() and doScript(). First one will prepare all variables needed and then sends it to the second one which will be actually comunicating with Photoshop. So let's take a closer look at the first function now:
First of all we are going to do some statements of two basic variables:
Dim appRef
Dim gifSaveOptions
Then declare our function:
Function prepareScript()
Now declare three variables - myValue is the text for header; myPath is the path where we will save the file; TheForm is variable which will serve us as an input to our Form.
Dim myValue
Dim myPath
Dim TheForm
This is how we will get the values from form:
Set TheForm = Document.forms("Form1")
myValue = TheForm.abc.value
myPath = TheForm.abc2.value
Now we need to check couple of things. First we need to know if user wrote some header - we will use the checkValue variable as a storage for this information. Then we do almost the same with checkPath. Difference is that we don't really care what is written in the first input so all we need is to know if the input has any value (Len(myValue)) while in path input we already have something in default and we don't know how long the end path will be. So what we do is to check if the string in path input ends with backslash (\). If so then user probably didn't entered any filename because no filename can end with backslash. So that we gather theese informations from form and then do simple If - Else command to find out if we can continue to next function.
Note: In the path input you don't have to write extension e.g. .Gif or .Jpg. Just write the name of the file it self - "my_header".
checkValue = Len(myValue)
checkPath = Right(myPath,1)
If first input is empty Insert Text message will be displayed:
If checkValue = 0 Then
MsgBox ("Insert Text")
If filename isn't specified you'll get Insert Path message:
Else
If checkPath = "\" Then
MsgBox ("Insert Path")
If everything is okay, we proceed to doScript() function with myValue and myPath variables:
Else
doScript myValue, myPath
End If
End If
End Function
|
|
|
|
4. doScript() function.
Okay. We're getting closer to finish:)
We declare doScript() function and use Set to create two new objects - appRef (which is object for Photoshop) and gifSaveOptions (which we will use for saving purposes. If you want to use JPG as output just write jpgSaveOptions. See reference manual for other options:
Function doScript(myValue, myPath)
Set appRef = CreateObject( "Photoshop.Application" )
Set gifSaveOptions = CreateObject("Photoshop.GIFSaveOptions")
We need to remember current unit settings and then set units to the value expected by this script:
Dim originalRulerUnits
originalRulerUnits = appRef.Preferences.RulerUnits
appRef.Preferences.RulerUnits = 1
Now we need to open our previously created psd file:
Dim fileName
fileName = "c:\temp\my_header.psd"
Set docRef = appRef.Open(fileName)
And select layer with the Text. Since we named it textLayer it's not really a problem:
docRef.ActiveLayer = docRef.Layers("textLayer")
We attach myValue variable to the active layer so that what is written in variable is now written in text layer:
docRef.ActiveLayer.TextItem.Contents = myValue
Now we need to convert our layer from psPointText to psParagraphText from which we can get width information for cropping:
docRef.ArtLayers("textLayer").TextItem.Kind = 2
We create new variable whereCrop and get it's value from our Text layer:
Dim whereCrop
whereCrop = docRef.ArtLayers("textLayer").TextItem.Width
We substract some pixels (in this case 16 is enough) to get some space around the text. Otherwise image would be cut right at the end of text:
whereCrop = whereCrop - 16
We do flatten whole document and perform crop operation:
docRef.Flatten
docRef.Crop Array(0,0,whereCrop,45)
In crop array: Values between brackets are document's bounds - top, left, right, bottom.
We're pretty much done. Last thing we need to do is to save our document.
|
|
5. Saving.
Now we'll use the gifSaveOptions object. Basically every parameter what you write here you already know from Save from web command from Photoshop. For my header I need just specify number of colors. See reference manual for other options:
gifSaveOptions.Colors = 16
Following command will save the document to path specified by user (myPath), using options we gave him (gifSaveOptions), document will be saved as a copy (true) and in lowercase (psLowerCase).
docRef.SaveAs myPath,gifSaveOptions, true, psLowerCase
If you don't want to bother yourself with closing the opened image in photoshop add this to end of your script:
docRef.ActiveHistoryState = docRef.HistoryStates(1)
docRef.Save
docRef.Close
End Function
//-->
</script>
It will first turn the image to original state, then perform Save and Close commands.
Now try to run your script. Fill required informations and enjoy. You will probably see warning message upon submiting script about ActiveX Components but don't get scared. Since you wrote the script you have full controll what it does. So just answer Yes and let Photoshop do his job :)
|
|
Conclusion As you can see it's not really hard to write some basic scripts. It can save you lot of time when you need to create new headers/buttons or menu items for your website. This is especially useful for buttons because you can make also onMouseOver state in one script so upon one submit you'll get two buttons (on and off).
|
|