Tutorial: Write your first Photoshop script

Tutorial: Write your first Photoshop script

I’ve already explained how to write a script for Illustrator using the ExtendScript toolkit in a previous tutorial. This time, I’m scripting for Photoshop and I’m also building a panel for my script using Adobe Configurator.

My Script: Export selected layer

There is a manual operation that I do ALL THE TIME in Photoshop: exporting a single layer to a single PNG or JPEG file. Every time, I copy the content of a layer, create a document, paste the content, save for the web, etc… So I took the opportunity to create a script that will automate these tasks. You could use the classic Photoshop actions, but I like the freedom of coding to control the experience.

I showcase my script “Export selected layer” at the beginning of my video tutorial, but you can test it yourself in Photoshop. To do so, just open the Adobe Exchange panel in Photoshop and search for my name “chaize”, or type “export”. You’ll be able to install and test my script.

The video tutorial: Develop a script for Photoshop in less than 15 minutes

It’s in High Definition on YouTube. Enjoy:

My Script: The source code

Here is the source code of my script that exports the selected layer of your PSD to a transparent PNG:

If you have any questions, don’t hesitate to post a comment.

In a future post, I’ll explain how to distribute your panel on the Adobe Exchange marketplace, and I’ll feature some cool scripts for Photoshop coders such as the xTools. I’ve also been told that the pan PNG-Express is a professional script to produce PNG files from layers, with A LOT of features compared with my little script.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

22 Comments

  1. Having just play around with photoshop scripts today; I got to admit, its a wonderful experience to automate repititive tasks with such freedom.

    Having said that, I don’t see any harm in using Actions along-side scripts either i.e. so far, I prefer using ‘doActions’ wherever need be.

    – Tahir.

  2. Great idea! Is this code meant for CS6? I am getting an error on line 19
    executeAction( idrasterizeLayer, desc5, DialogModes.NO );

    • Indeed the features is in cs6 and CC. Thanks for trying.

  3. Hey Michael,
    your tutorial inspired me to try Photoshop Scripting and making own panels. Thanks for that. But one thing didn’t let me go: you showed in your video, that when copying and pasting a layer to a new document, you are losing the layer styles.
    There’s one much better way to do that and you don’t loose anything. Just choose in “Layer” menu the command “Duplicate layer” and then select the new document option. That’s it!

    Anyway, thanks again for the tipps. Cheers, Patrick

    • Ah indeed. So many options in Photoshop :)

  4. This video was excellent. It will save me a lot of time as I write my first Photoshop CC script. Thanks.

  5. Awesome!! thanks alot, i’ve turned it into c# code and it works great…
    the only problem is that it’s very slow due to it’s recursion.. :(

  6. Thanks! Really helpful and one of the few descent articles around Photoshop extensions.

  7. Hey Michael,
    Thanks for the tutorial …. i have recently started photoshop scripting,
    i am working on a script to duplicate and rotate a single layer , the only problem is
    with the anchor points . i want every layer to rotate from a specific anchor point say x y (500,400) co-ordinate by say 45 degrees . Layer will be duplicated and rotated just to create a circular shape .

    thanks

  8. Hi there- Im knew to all of this and wish to create a poster template, just wondering how you would script to change the opacity of a layer?? thanks :)

    • @MB
      do you want to change the opacity of all the layers in your document at the same time ???

  9. Hello guys.
    I am new on the website and also with Photoshop Scripting.
    I need some help.
    I looked a little bit on the internet and found some pieces of what i need.
    Basically, i need a script that from a psd with layers and transparent background should merge layers…save to 2 different psd files that have different sizes (size of working psd and smaller one) and to 2 different folders (but same file name) and then from the big psd to save 4 jpeg RGB 72ppi for web in 4 different sizes and 4 different folders.
    Can anyone help me with that..?
    At least point me in the right direction?
    Thank you in advance.

    • Hi
      You should try with the “Actions” menu. Record your step and see if you can run it on several PSD files. Maybe you don’t need a script for this one.

      • i will try, but how to resize image to specific canvas size and center it?
        and also with action you cannot specify the folder where to save no?
        because every time i want to save the psd to the same folder where it is but to two different subfolders one LARGE and the other one SMALL…after this from the large psd I want to save 4 jpeg for web with the same name but to 4 different folders.

        • Try the script “image processor pro” from Russell Brown (Google will help you find it.

  10. Can anyone help me write this one?

    I’ve written the duplicate layer part, just like you showed in the video.

    Now I’m trying to apply a High Pass filter with a value of 12, set the layer mode to Overlay, and set the opacity to 45%, but I’m getting stuck.

  11. Hi Michael. Thank you for the tutorial! This is my first time scripting in PS and with ExtendedScript. What is the best way to check your code while working on it? Now I’m changing the text in a text layer for indication, but it is too cumbersome. Is there a log panel or something like that that you know of?

    Thanks,
    Uri.

  12. I’ve implemented the code exactly as shown and I keep on getting the following error.

    Error 1220: Illegal Argument at this line:

    var myNewDoc = app.documents.add(tempWidth,tempHeight,72,”exportedLayer”, NewDocumentMode.RGB, DocumentFill.TRANSPARENT);

    Any help would be appreciated!

  13. Hi Michaël,

    Thanks for great tutorial.
    Could you please make me a script for my this routine work. Thanks in advance

    I open more than 1 files (atleast 10+) of same size with one transparent layer only. Then I duplicate each files layer to a new file of same size in separate layers and close those single layer files, save this new file.

    Regards
    Ansaari

  14. Hi Buddy,

    Your tutorial really made a great Impact on me as this also taught a basic Script but also have given me a pity good Idea of Panels.
    You were my first teacher. And I have gained much from this.
    Thanks a lot for sharing this type of information with us.

  15. How can I add sometyhing like temResolution? For example when I’m going to change resolution of my PS file to 96dpi I want to save rasterize layer to this resolution without changing this in JS code.

  16. Hello
    I looking a script to convert RGB to CMYK with text as 100% Black
    can you make it ? is it possible ?
    you know where I can find it ? can some one help me ?
    thanks

Trackbacks/Pingbacks

  1. Photoshop Generator & Web Design | Creative droplets - […] Members of the Adobe Creative Cloud just need to update Photoshop CC to get this new technology. On this…

Submit a Comment

Your email address will not be published. Required fields are marked *