[AppleScript] Multiple Files thru photoshop. txt->psd.

Discussion in 'Mac Programming' started by troyman11, Apr 8, 2013.

  1. troyman11, Apr 8, 2013
    Last edited: Apr 8, 2013

    macrumors newbie

    Joined:
    Feb 18, 2008
    #1
    Hello,

    Right now I have this code for taking a files contents and putting it into photoshop. Im trying to expand on this.
    I want applescript to let me either choose the file I want it to use or even better just take a list of these files. Input them into Photoshop and Save As "###.psd".

    Example:
    Folder 1 contains txt files John.txt and Jack.txt.
    use first file
    use its contents in photoshop
    save as john.psd
    use second file... repeat
    save as jack.psd

    My mind is all over the place with this one so anything will help.

    Code:
    set theNames to ("/Users/myname/Desktop/TEST.txt" as «class utf8»)
    set lastName to second paragraph of theNames
    
    tell application "Adobe Photoshop CS5"
    	get properties of art layer 3 of current document
    	set kind of art layer 3 of current document to text layer
    	get properties of text object of art layer 3 of current document
    	get contents of text object of art layer 3 of current document
    	set contents of text object of art layer 3 of current document to lastName
    end tell
    
     
  2. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    You've given the answers to your questions yourself. A list of files and repeat. You'll have to use a loop. Can you attach an example of John.txt and Jack.txt to your post?
     
  3. troyman11, Apr 8, 2013
    Last edited: Apr 8, 2013

    thread starter macrumors newbie

    Joined:
    Feb 18, 2008
    #3
    Dunno the commands

    I don't exactly know those commands. such as saving and looping. I uploaded a .txt. The format is extremely basic. I just want to automate this process as much as I can. You helped me last time so hopefully I get your expertise again.
     

    Attached Files:

    • John.txt
      File size:
      17 bytes
      Views:
      45
  4. kryten2, Apr 8, 2013
    Last edited: Apr 13, 2013

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    In the example I'm using ASObjC Runner to get the part of the filename without the extension. See info link on how to get/use/install it.

    An example :

    Code:
    set theFolder to choose folder with prompt "Choose folder with text files."
    
    tell application "Finder" to set theItems to items of theFolder as alias list
    
    repeat with anItem in theItems
    	set anItem to contents of anItem
    	set theNames to (read anItem as «class utf8»)
    	set firstName to first paragraph of theNames
    	set lastName to second paragraph of theNames
    	tell application "ASObjC Runner"
    		set theFileName to name stub of (about file anItem include only {"name stub"})
    	end tell
    	createTextLayersPS(theFileName, theFolder, firstName, lastName)
    end repeat
    
    on createTextLayersPS(psFileName, psFolderName, psFirstName, psLastName)
    	-- Change tell application line below to your version of Photoshop
    	tell application "Adobe Photoshop CS6"
    		make new document with properties {name:psFileName}
    		--create a variable named theDocRef --assign the current (active) document to it 
    		set theDocRef to the current document
    		--create a variable for the text layer, create the layer as an art layer object 
    		--and use the kind property of the art layer object to make it a text layer
    		set theFirstTextLayer to make new art layer in theDocRef with properties ¬
    			{name:"First Name", kind:text layer}
    		set theSecondondTextLayer to make new art layer in theDocRef with properties ¬
    			{name:"Last Name", kind:text layer}
    		set properties of text object of theFirstTextLayer whose name is "First Name" to {contents:psFirstName, size:30.0, stroke color:{class:RGB hex color, hex value:"913b8e"}}
    		set properties of text object of theSecondondTextLayer whose name is "Last Name" to {contents:psLastName, size:48.0, stroke color:{class:RGB hex color, hex value:"339966"}, position:{3, 4}}
    		set thesavedoc to save theDocRef in file ((psFolderName as text) & psFileName) as Photoshop format appending lowercase extension
    		close thesavedoc
    	end tell
    end createTextLayersPS
    
    Info : http://www.macosxautomation.com/applescript/apps/runner.html
     

    Attached Files:

  5. troyman11, Apr 8, 2013
    Last edited: Apr 8, 2013

    thread starter macrumors newbie

    Joined:
    Feb 18, 2008
    #5
    Error

    Code:
    error "Can’t make some data into the expected type." number -1700
    Keep getting this error at set theNames to (read anItem as «class utf8»)
     
  6. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #6
    Attach your txt files. They need to be plain text files with Unix LF linefeeds and UTF8 encoding. I've used your John.txt file in the example. Does the script error with only John.txt in the folder? Are you using the script or did you make changes to it? If so post the code. Provide as much information as you can with your reply. You have the opportunity to attach files to your replies. Please make use of it.
     
  7. thread starter macrumors newbie

    Joined:
    Feb 18, 2008
    #7
    Code I am working with

    Code:
    set theFolder to choose folder with prompt "Choose folder with text files."
    
    tell application "Finder" to set theItems to items of theFolder as alias list
    
    repeat with anItem in theItems
    	set anItem to contents of anItem
    	set theNames to (read anItem as «class utf8»)
    	set firstName to first paragraph of theNames
    	set lastName to second paragraph of theNames
    	set IDNUMBER to 3rd paragraph of theNames
    	set MEMBERSHIP to 4th paragraph of theNames
    	tell application "ASObjC Runner"
    		set theFileName to name stub of (about file anItem include only {"name stub"})
    	end tell
    	createTextLayersPS(theFileName, theFolder, firstName, lastName)
    end repeat
    
    on createTextLayersPS(psFileName, psFolderName, psFirstName, psLastName)
    	-- Change tell application line below to your version of Photoshop
    	tell application "Adobe Photoshop CS5"
    		open document "/Users/vixtrasolution/Desktop/Testfolder/TEST.psd"
    		
    		get properties of art layer 1 of current document
    		set kind of art layer 1 of current document to text layer
    		get properties of text object of art layer 1 of current document
    		get contents of text object of art layer 1 of current document
    		set contents of text object of art layer 1 of current document to firstName
    		--
    		get properties of art layer 2 of current document
    		set kind of art layer 2 of current document to text layer
    		get properties of text object of art layer 2 of current document
    		get contents of text object of art layer 2 of current document
    		set contents of text object of art layer 2 of current document to lastName
    		--
    		get properties of art layer 3 of current document
    		set kind of art layer 3 of current document to text layer
    		get properties of text object of art layer 3 of current document
    		get contents of text object of art layer 3 of current document
    		set contents of text object of art layer 3 of current document to IDNUMBER
    		--
    		get properties of art layer 4 of current document
    		set kind of art layer 4 of current document to text layer
    		get properties of text object of art layer 4 of current document
    		get contents of text object of art layer 4 of current document
    		set contents of text object of art layer 4 of current document to MEMBERSHIP
    		
    		save current document in file (((psFolderName as text) & psFileName) & ".psd") as Photoshop format
    	end tell
    end createTextLayersPS
     

    Attached Files:

  8. kryten2, Apr 9, 2013
    Last edited: Apr 9, 2013

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #8
    The example script given will work for the example you gave in your first post eg n txt files --> creation of n psd files with the names of the txt files.
    This is what happens :
    The repeat block loop through the txt files. It gets the first and second paragraph from the txt file, the name of the txt file without the extension to be used to save the psd file in the handler, calls the handler with parameters.
    Adding things to a script without really knowing what they do will always fail.
    This line :

    Code:
    set theFolder to choose folder with prompt "Choose folder with text files."
    couldn't be more clearer than that. Select a folder with txt files. Your testfolder also contains a TEST.psd file. That's why you get the error "Can’t make some data into the expected type." number -1700 when you try to read this file. This can be solved by changing this line :

    Code:
    tell application "Finder" to set theItems to items of theFolder as alias list
    into this :

    Code:
    tell application "Finder" to set theItems to (items of theFolder whose name extension is "txt") as alias list
    The handler :

    You added this in the repeat loop :

    Code:
    	set IDNUMBER to 3rd paragraph of theNames
    	set MEMBERSHIP to 4th paragraph of theNames
    
    but the handler doesn't use them eg :

    Code:
    	createTextLayersPS(theFileName, theFolder, firstName, lastName)
    
    should be something like this :

    Code:
    	createTextLayersPS(theFileName, theFolder, firstName, lastName, IDNUMBER, MEMBERSHIP)
    and to reflect the changes this :

    Code:
    on createTextLayersPS(psFileName, psFolderName, psFirstName, psLastName)
    should be something like this :

    Code:
    on createTextLayersPS(psFileName, psFolderName, psFirstName, psLastName,psIdNumber,psMemberShip)
    
    Inside the handler you use the parameters of the handler eg :

    Code:
    set contents of text object of art layer 1 of current document to psFirstName
    set contents of text object of art layer 2 of current document to psLastName
    set contents of text object of art layer 3 of current document to psIdNumber
    set contents of text object of art layer 4 of current document to psMemberShip
    
    Also this line will not work :

    Code:
    open document "/Users/vixtrasolution/Desktop/Testfolder/TEST.psd"
    and this line :

    Code:
    save current document in file (((psFolderName as text) & psFileName) & ".psd") as Photoshop format
    
    will not save your TEST.psd file.
     
  9. thread starter macrumors newbie

    Joined:
    Feb 18, 2008
    #9
    U r the man

    You are the man. I was able to fix the errors and now it is running perfectly.
    I do have one more question. As u can see this is for a membership ID card. On the back I would like to put a barcode on it.

    Instead of me manually resizing the image I would like to know if this is possible.
    Place a .TIFF image from a folder.
    Resize to be the exact specifications of the template barcode.
    Put it over the barcode template. (same coordinates?)
    Save as a .jpeg
    Repeat

    I dunno if its different for text files and images. If this is not possible, its completely fine because you already helped me so much.

    Thanks,
    Troy

    attached is properties, back.psd and .tiff example
     

    Attached Files:

  10. kryten2, Apr 13, 2013
    Last edited: Apr 13, 2013

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #10
    Perhaps not exactly what you wanted but you can give it at try.

    Code:
    -- Change tell application line below to your version of Photoshop
    tell application "Adobe Photoshop CS6"
    	activate
    	-- Change paths to location of your documents
    	set myFilePath to alias POSIX file "/Users/kryten/Documents/TestFolder/BackExample/Example.tif" as text
    	set myTemplateFilePath to alias POSIX file "/Users/kryten/Documents/TestFolder/BackExample/BACK.psd" as text
    	open alias myTemplateFilePath
    	open alias myFilePath
    	set theDocRef to document "Example.tif"
    	set theTemplateDocRef to document "BACK.psd"
    	resize image theDocRef width 1844 height 306 resolution 300 resample method bicubic
    	select all theDocRef
    	copy
    	set current document to theTemplateDocRef
    	select theTemplateDocRef region {{110, 40}, {110, 346}, {1954, 346}, {1954, 40}}
    	paste with clipping to selection
    	merge art layer 1 of current document
    	save theTemplateDocRef in file ((alias POSIX file "/Users/kryten/Documents/TestFolder/BackExample/" as text) & "Back") as JPEG appending lowercase extension with copying
    	close theDocRef saving no
    	close theTemplateDocRef saving no
    	--quit
    end tell
     

Share This Page