AppleScripting Photoshop

Discussion in 'Mac Programming' started by nazkav, Jun 2, 2012.

  1. macrumors newbie

    Joined:
    Jun 2, 2012
    #1
    Hello,
    Basically I am trying to write a script that will work in conjunction with photoshop for work. The purpose of the script is
    to open up multiple files in photoshop off of a server using an applescript droplet. The files will all have different names and be in different locations
    on said server. Photoshop must then crop the files to three different pixel sizes and save them back on the server in specified folders, then Photoshop must covert original opened files to CMYK and save a jpeg and a tiff in another folder on the server. Also the files will be named "filename_rgb.jpg" and I want the CMYK files to be named "filename_cmyk.jpg" and "filename_cmyk.tif" here is my code so far:
    Code:
    on open myFiles
    	tell application "Adobe Photoshop CS4"
    		activate
    		set myFiles to alias "Macintosh HD:Users:tomfinnane:Desktop:structure:2000x2000:Kav.jpg"
    		open myFiles as JPEG
    		set ruler units of settings to pixel units
    		set theDocument to current document
    		tell theDocument
    			resize image width 468 height 468 resolution 300 resample method bicubic
    		end tell
    		save theDocument in file "Macintosh HD:Users:tomfinnane:Desktop:structure:492x492" as JPEG with options {class:JPEG save options, embed color profile:false, quality:12}
    		close theDocument
    		set myFiles to alias "Macintosh HD:Users:tomfinnane:Desktop:structure:2000x2000:Kav.jpg"
    		open myFiles as JPEG
    		set ruler units of settings to pixel units
    		set theDocument to current document
    		tell theDocument
    			resize image width 200 height 200 resolution 300 resample method bicubic
    		end tell
    		save theDocument in file "Macintosh HD:Users:tomfinnane:Desktop:structure:200x200" as JPEG with options {class:JPEG save options, embed color profile:false, quality:12}
    		close theDocument
    		set myFiles to alias "Macintosh HD:Users:tomfinnane:Desktop:structure:2000x2000:Kav.jpg"
    		open myFiles as JPEG
    		set ruler units of settings to pixel units
    		set theDocument to current document
    		tell theDocument
    			resize image width 95 height 95 resolution 300 resample method bicubic
    		end tell
    		save theDocument in file "Macintosh HD:Users:tomfinnane:Desktop:structure:95x95" as JPEG with options {class:JPEG save options, embed color profile:false, quality:12}
    		close theDocument
    		set myFiles to alias "Macintosh HD:Users:tomfinnane:Desktop:structure:2000x2000:Kav.jpg"
    		open myFiles as JPEG
    		tell theDocument
    			convert to profile "Working CMYK" intent relative colorimetric with blackpoint compensation and dithering
    		end tell
    		save theDocument in file "Macintosh HD:Users:tomfinnane:Desktop:structure:CMYK" as JPEG with options {class:JPEG save options, embed color profile:false, quality:12}
    		close theDocument
    		set myFiles to alias "Macintosh HD:Users:tomfinnane:Desktop:structure:2000x2000:Kav.jpg"
    		open myFiles as JPEG
    		tell theDocument
    			convert to profile "Working CMYK" intent relative colorimetric with blackpoint compensation and dithering
    		end tell
    		set theName to name of current document
    		save theDocument in file "Macintosh HD:Users:tomfinnane:Desktop:structure:CMYK" as TIFF with options {class:TIFF save options, embed color profile:false}
    	end tell
    end open
    I have managed to get photoshop to open the files and perform the actions required but when I try to save the last tiff I get an error "file/folder expected" also I can't get the naming portion correct. Also I foresee problems opening multiple files off of a different location. I have set this script up to work on my laptop at home but it must be portable and work from multiple Mac's all connected to the same file server. I am a Creative Photoshop retoucher with an interest in scripting and programming that is why this problem has landed at my door. I am sorry for the length of this question and the fact that the script is so far from complete but I have no prior knowledge of Applescript and accepted this challenge as my boss asked me personally.

    I hope there's someone out there who can help

    Thanks

    nazkav
     
  2. kryten2, Jun 2, 2012
    Last edited: Jun 3, 2012

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    I have to admit this one took a little while as I've never scripted Photoshop before. Also had to change a few things from your example especially the saving bits. The naming you would like wasn't exactly clear. Here's a working droplet script tested on Leopard with Photoshop CS2. Dropped files are resized 3 times and 2 cmyk files are created from the original file. The original file is left untouched. Replace CS2 with CS4 and change the paths. Unfortunately I'm on a standalone Mac so couldn't test the server side of your problem. If you want help with that you should post some more information eg what folders are shared on the server(What do you see in Finder-->Shared), a location of a file on the server, an example of a location where you want to put the resized files. Like : Shared Folder on server--> Folder structure --> the resized files go inside the structure folder. Screenshots would make things a lot easier.

    Code:
    -- This droplet processes files dropped onto the applet 
    on open these_items
    	repeat with i from 1 to the count of these_items
    		set this_item to item i of these_items
    		set this_item_string to this_item as string
    		set the_item_name to do shell script "sed -E 's|:$||;s|.*:||;s|\\.[^.]*$||' <<< " & quoted form of this_item_string
    		my resize_image(this_item, 468, 468, 300, "LeopardFirewire:Users:test:Desktop:" & the_item_name & "_rgb_492x492.jpg")
    		my resize_image(this_item, 200, 200, 300, "LeopardFirewire:Users:test:Desktop:" & the_item_name & "_rgb_200x200.jpg")
    		my resize_image(this_item, 95, 95, 300, "LeopardFirewire:Users:test:Desktop:" & the_item_name & "_rgb_95x95.jpg")
    		my CMYK_image_jpg(this_item, "LeopardFirewire:Users:test:Desktop:" & the_item_name & "_cmyk.jpg")
    		my CMYK_image_tif(this_item, "LeopardFirewire:Users:test:Desktop:" & the_item_name & "_cmyk.tif")
    	end repeat
    end open
    
    on resize_image(the_file, thewidth, theheight, theresolution, thefilename)
    	tell application "Adobe Photoshop CS2"
    		activate
    		open the_file as JPEG
    		set ruler units of settings to pixel units
    		set theDocument to current document
    		tell theDocument
    			resize image width thewidth height theheight resolution theresolution resample method bicubic
    		end tell
    		set thesavedoc to save theDocument in file thefilename as JPEG with options {class:JPEG save options, embed color profile:false, quality:12}
    		close thesavedoc
    	end tell
    end resize_image
    
    on CMYK_image_jpg(the_file, thefilename)
    	tell application "Adobe Photoshop CS2"
    		activate
    		open the_file as JPEG
    		set theDocument to current document
    		tell theDocument
    			convert to profile "Working CMYK" intent relative colorimetric with blackpoint compensation and dithering
    		end tell
    		set thesavedoc to save theDocument in file thefilename as JPEG with options {class:JPEG save options, embed color profile:false, quality:12}
    		close thesavedoc
    	end tell
    end CMYK_image_jpg
    
    on CMYK_image_tif(the_file, thefilename)
    	tell application "Adobe Photoshop CS2"
    		activate
    		open the_file as JPEG
    		set theDocument to current document
    		tell theDocument
    			convert to profile "Working CMYK" intent relative colorimetric with blackpoint compensation and dithering
    		end tell
    		set thesavedoc to save theDocument in file thefilename as TIFF with options {class:TIFF save options, embed color profile:false}
    		close thesavedoc
    	end tell
    end CMYK_image_tif
    Tip :

    Code:
    --(path to desktop as text) is the same as "Macintosh HD:Users:tomfinnane:Desktop:"
    -- so this also works for constructing paths
    set the_desktop_path to (path to desktop as text)
    my CMYK_image_tif(this_item, the_desktop_path & the_item_name & "_cmyk.tif")
    
     

    Attached Files:

  3. thread starter macrumors newbie

    Joined:
    Jun 2, 2012
    #3
    Applescript Photoshop

    Thank you for your in depth help with my problem.
    I've been trying your script all day but unfortunately I keep getting an error when I drop the files on to the droplet

    The error is this:

    Can't get alias "Macintosh HD:Users:tomfinnane:Desktop:Kav_1.jpg

    This is probably easily fixed but my Applescript knowledge doesn't extend that far. Any more help would be appreciated. By the way I'm trying to open 3 JPEG'S from my Desktop.

    Thanks


    nazkav
     
  4. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    Strange shouldn't happen as the script worked for me. To debug this I'll need a couple of things.

    1) Post the script you're using with the changes you've made(put it between the code tags!)
    2) I'll attach another script for you to open in Script Editor and run after you made the necessary changes eg CS4 and paths. It's the same as the droplet with the exception that you have to choose a file manually. It will only process one file and you can comment out the second and third resize handlers and the cmyk handlers eg leave the first my resize_image as it is and put -- before the following four my statements. This will also speed things up. When the script errors it should highlight a section of the script where the error occured. Post a screenshot of the script window. Run the script again this time with the event log pane selected and post a screenshot of the event log. Or select all the text in the event log pane, put it in a text file and post that as attachment.
     

    Attached Files:

  5. thread starter macrumors newbie

    Joined:
    Jun 2, 2012
    #5
    Applescript Photoshop

    Hello,

    I've tried your script and it all works fine so here is the script I'm using:
    Code:
    on open theseItems
    	set theLocation to "Macintosh HD:Users:tomfinnane:Desktop"
    	tell application "Finder"
    		set archiveFolder to make new folder at theLocation with properties {name:"TEST"}
    		set firstJpgFolder to make new folder at archiveFolder with properties {name:"2000x2000"}
    		set secondJpgFolder to make new folder at archiveFolder with properties {name:"468x468"}
    		set thirdJpgFolder to make new folder at archiveFolder with properties {name:"200x200"}
    		set fourthJpgFolder to make new folder at archiveFolder with properties {name:"95x95"}
    		set cmykFolder to make new folder at archiveFolder with properties {name:"CMYK"}
    	end tell
    	repeat with i from 1 to the count of theseItems
    		set thisItem to item i of theseItems
    		set thisItemString to thisItem as string
    		set theItemName to do shell script "sed -E 's|:$||;s|.*:||;s|\\.[^.]*$||' <<< " & quoted form of thisItemString
    		my image(thisItem, "Macintosh HD:Users:tomfinnane:Desktop:" & thisItemString & "_rgb.jpg")
    		my resize_image(thisItem, 468, 468, 300, "Macintosh HD:Users:tomfinnane:Desktop:" & thisItemString & "_rgb.jpg")
    		my resize_image(thisItem, 200, 200, 300, "Macintosh HD:Users:tomfinnane:Desktop:" & thisItemString & "_rgb.jpg")
    		my resize_image(thisItem, 95, 95, 300, "Macintosh HD:Users:tomfinnane:Desktop:" & thisItemString & "_rgb.jpg")
    		my CMYK_image_jpg(thisItem, "Macintosh HD:Users:tomfinnane:Desktop:" & thisItemString & "_cmyk.jpg")
    		my CMYK_image_tif(thisItem, "Macintosh HD:Users:tomfinnane:Desktop:" & thisItemString & "_cmyk.tif")
    	end repeat
    end open
    
    on image(the_file, theFileName)
    	tell application "Adobe Photoshop CS4"
    		activate
    		open the_file as JPEG
    		set theDocument to current document
    		set theSaveDoc to save theDocument in file theFileName as JPEG with options {class:JPEG save options, embed color profile:false, quality:12}
    		close theSaveDoc
    	end tell
    end image
    
    on resize_image(the_file, theWidth, theHeight, theResolution, theFileName)
    	tell application "Adobe Photoshop CS4"
    		activate
    		open the_file as JPEG
    		set ruler units of settings to pixel units
    		set theDocument to current document
    		tell theDocument
    			resize image width theWidth height theHeight resolution theResolution resample method bicubic
    		end tell
    		set theSaveDoc to save theDocument in file theFileName as JPEG with options {class:JPEG save options, embed color profile:false, quality:12}
    		close theSaveDoc
    	end tell
    end resize_image
    
    on CMYK_image_jpg(the_file, theFileName)
    	tell application "Adobe Photoshop CS4"
    		activate
    		open the_file as JPEG
    		set theDocument to current document
    		tell theDocument
    			convert to profile "Working CMYK" intent relative colorimetric with blackpoint compensation and dithering
    		end tell
    		set theSaveDoc to save theDocument in file theFileName as JPEG with options {class:JPEG save options, embed color profile:false, quality:12}
    		close theSaveDoc
    	end tell
    end CMYK_image_jpg
    
    on CMYK_image_tif(the_file, theFileName)
    	tell application "Adobe Photoshop CS4"
    		activate
    		open the_file as JPEG
    		set theDocument to current document
    		tell theDocument
    			convert to profile "Working CMYK" intent relative colorimetric with blackpoint compensation and dithering
    		end tell
    		set theSaveDoc to save theDocument in file theFileName as TIFF with options {class:TIFF save options, embed color profile:false}
    		close theSaveDoc
    	end tell
    end CMYK_image_tif
    Just to explain. The bit at the beginning is there to create a file structure. I obviously haven't changed the save paths as I couldn't open the files in the first place.

    Thank you so much again for all your help

    nazkav
     
  6. thread starter macrumors newbie

    Joined:
    Jun 2, 2012
    #6
    applescript Photoshop

    Sorry just thought of something, I am trying to open multiple files on the droplet. Could that have something to do with it?

    Thanks

    nazkav
     
  7. kryten2, Jun 4, 2012
    Last edited: Jun 4, 2012

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #7
    So everything is fine now?

    With what?

    I foresee problems with your tell Finder block. First time you drop something on the droplet the folders will be created, the second time you drop something it will error saying that the folders already exist. You can solve that by using something like this :


    Code:
    set theLocation to "Macintosh HD:Users:tomfinnane:Desktop:"
    tell application "Finder"
    	if not (exists folder "TEST" of folder theLocation) then
    		make new folder at theLocation with properties {name:"TEST"}
    	end if
    	set archiveFolder to "Macintosh HD:Users:tomfinnane:Desktop:TEST:"
    	if not (exists folder "2000x2000" of folder archiveFolder) then
    		set firstJpgFolder to make new folder at archiveFolder with properties {name:"2000x2000"}
    	end if
    	if not (exists folder "468x468" of folder archiveFolder) then
    		set secondJpgFolder to make new folder at archiveFolder with properties {name:"468x468"}
    	end if
    	if not (exists folder "200x200" of folder archiveFolder) then
    		set thirdJpgFolder to make new folder at archiveFolder with properties {name:"200x200"}
    	end if
    	if not (exists folder "95x95" of folder archiveFolder) then
    		set fourthJpgFolder to make new folder at archiveFolder with properties {name:"95x95"}
    	end if
    	if not (exists folder "CMYK" of folder archiveFolder) then
    		set cmykFolder to make new folder at archiveFolder with properties {name:"CMYK"}
    	end if
    end tell
    But then there's another problem with this as the variables simply won't get set because all conditionals return true on a second drop. I would move them out of the if end if block if I were you like I did with the archiveFolder.

    I'll try to post a zipped movie as an attachment of my original script in action.
     
  8. thread starter macrumors newbie

    Joined:
    Jun 2, 2012
    #8
    Applescript Photoshop

    Thank you again for your help I will try your solution and let you know. In answer to your questions, basically I tried your script (the one which prompts you to select a file) and it all worked fine. But I was wondering if the reason the droplet wasn't working was because I was dropping multiple files on to it. If your wondering why I'm using a droplet it is because that was the brief from work, they want a droplet on the desktop that is able to handle multiple files.

    Thank you

    nazkav
     
  9. kryten2, Jun 4, 2012
    Last edited: Jun 4, 2012

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #9
    That's what droplets are for. To drop multiple files onto. I had just finished that screenrecording I promised when I saw your reply and decided to take one last closer look at the code you posted and I spotted an error. Don't know if this will fix your problem but it's worth a try. See if you can find it. I'll highlight the relevant parts.

    Code:
    --My code
    
    set [COLOR="Red"]the_item_name[/COLOR] to do shell script "sed -E 's|:$||;s|.*:||;s|\\.[^.]*$||' <<< " & quoted form of [COLOR="Blue"]this_item_string[/COLOR]
    my resize_image(this_item, 468, 468, 300, "LeopardFirewire:Users:test:Desktop:" & [COLOR="red"]the_item_name[/COLOR] & "_rgb_492x492.jpg")
    
    --Your code
    
    set [COLOR="Red"]theItemName[/COLOR] to do shell script "sed -E 's|:$||;s|.*:||;s|\\.[^.]*$||' <<< " & quoted form of [COLOR="blue"]thisItemString[/COLOR]
    my resize_image(thisItem, 468, 468, 300, "Macintosh HD:Users:tomfinnane:Desktop:" & [COLOR="blue"]thisItemString[/COLOR] & "_rgb.jpg")
    For the ScreenRecording. Save the zip files to a folder(eg Folder Screen on your Desktop)-->unzip them(you'll see out_aa,out_ab, out_ac and out_ad)-->move the zip files out of the folder--> Open Terminal--> cd into the folder with the out_ files ( eg cd Desktop/Screen)-->cat the splitted files back together eg cat out_a[a-d]>ScreenRecording.mov Enjoy the droplet in action. I'll attach the script I used in the Screenrecording. It's adapted for use with Adobe Photoshop CS6 thanks to this stupid bug I encountered. More info about the bug here : http://helpx.adobe.com/photoshop/kb/silent-fail-or-error-file.html. So don't copy this blindingly and expect it to work. Take a look at the link and the adaptations will make sense.
     

    Attached Files:

  10. thread starter macrumors newbie

    Joined:
    Jun 2, 2012
    #10
    Applescript Photoshop

    Hello again,

    I've tried all that you told me to do and after massive headaches it finally works. So I would like to thank you for all your help in this matter you have been brilliant.

    Thank you a thousand times

    nazkav
     
  11. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #11
    No headaches for me at least not massive ones :) Just a fun challenge. Thank you for the feedback.
     
  12. macrumors newbie

    Joined:
    Jan 11, 2013
    #12
    AppleScripting Photoshop

    Kryten2,

    I found your work on this applescript and wanted to give it a try. It gets me to the point of creating a TEST folder on the desktop with the correct subfolders, however, I get an error message that reads it "Can't get alias" with the path to the User folder and the Desktop with the folder name I am dragging onto the droplet. Any help or ideas that I can attempt would be great. Thanks in advance for any help.

    applepie375
     

Share This Page