Adobe Photoshop

My time with Photoshop is really limited to converting images from one format to another. There are other, cheaper, applications that can do this, but since most of the images I work with are EPS, Photoshop is really the only way to go for speed.

CS4: Convert art folders full of EPS files to 300 dpi RGB TIFFs

property kRootArtFolder : “path:to:parent:art:folder:”
property kArtFolders : {"ch01:", "ch02:", "ch03:"}

set kRootArtFolder to choose folder
set mDestinationFolder to choose folder

property pTrashList : {}

(* Convert the EPS files to hi-res TIF *)

set lastChapterFolder to count kArtFolders
repeat with thisChapterFolder from 1 to lastChapterFolder
	set theChapterFolder to item thisChapterFolder of kArtFolders
	log {"theChapterFolder", theChapterFolder}
	set theArtFolder to (kRootArtFolder & theChapterFolder) as string

	set fileNameList to {}

	tell application "System Events"
		set fileNameList to get the name of every disk item of alias theArtFolder
	end tell

	log {"Processing..."}
	set lastFile to (get count of items in fileNameList)
	repeat with thisFile from 1 to lastFile
		set theFileName to item thisFile of fileNameList
		set theFileExtension to GetFileExtension(theFileName) of me
		if (theFileExtension = "eps") or (theFileExtension = "EPS") then
			set theFilePath to (theArtFolder & theFileName) as string
			my ProcessArt(theFilePath, theFileName)
		end if
	end repeat

	log {"Trashing..."}
	set lastItemToTrash to (count pTrashList)
	repeat with thisItemToTrash from 1 to lastItemToTrash
		set theItemToTrash to item thisItemToTrash of pTrashList
		tell application "System Events"
			try
				set theResult to move alias theItemToTrash to (path to trash)
			end try
		end tell
	end repeat

	set pTrashList to {}
end repeat

on ProcessArt(theFilePath, theFileName) -- (string) as void
	log theFilePath
	tell application "Adobe Photoshop CS4"
		open alias theFilePath with options {constrain proportions:true, resolution:300.0, use antialias:true} without showing dialogs
		set end of pTrashList to theNewFilePath
		try
			do action "Trim transparent pixels" from "Default Actions"
		end try
		try
			flatten document 1
		end try
		set theTIFFilePath to CreateTIFFilePath(((mDestinationFolder & theFileName) as string)) of me
		save document 1 in theTIFFilePath as TIFF with options {byte order:Mac OS, embed color profile:false, image compression:none}
		close document 1
	end tell

end ProcessArt

on GetFileExtension(theFileName) -- (theFilename as string) as String
	set AppleScript's text item delimiters to "."
	set fileNameList to every text item of theFileName
	set AppleScript's text item delimiters to ""

	try
		return item 2 of fileNameList as string
	on error
		return ""
	end try
end GetFileExtension

on CreateTIFFilePath(theEPSFilePath) -- (string) as string
	set theTIFFilePath to replace_chars(theEPSFilePath, ".eps", ".tif") of me
	return theTIFFilePath
end CreateTIFFilePath

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

CS4: Convert folder of RGB n dpi JPGs to CMYK 300 dpi TIFs

global kSourceFolder
global kDestinationFolder

set kSourceFolder to alias "Macintosh HD:Users:philipr:Desktop:sourceFolder:" --choose folder
set kDestinationFolder to alias "Macintosh HD:Users:philipr:Desktop:destinationFolder:" --choose folder

set theItems to list folder kSourceFolder without invisibles
set itemCount to (get count of items in theItems)
repeat with thisItem from 1 to itemCount
	set theItem to item thisItem of theItems
	set theItem to (kSourceFolder & theItem) as string
	my ProcessFile(item thisItem of theItems, theItem)
end repeat

on ProcessFile(theFileName, theFile)

	tell application "Adobe Photoshop CS4"
		set theFileAlias to theFile as alias

		open theFileAlias showing dialogs never

		set theNewFileName to ProcessFileName(theFileName) of me

		change mode document 1 to CMYK
		resize image document 1 resolution 300 resample method none

		save document 1 in ((kDestinationFolder & theNewFileName) as string) as TIFF with options {byte order:Mac OS, embed color profile:false, image compression:none}

		close document 1
	end tell

end ProcessFile

on ProcessFileName(originalName)

	set the5DigitISBN to (get characters 6 thru 10 of originalName)

	set theNewName to (the5DigitISBN & "_CVRF_300CMY.tif") as string

	return theNewName
end ProcessFileName

CS4: Prepare cover images for O’Reilly’s Safari

(*
File: Convert Cover Images For Safari
Author(s): Philip Regan
Source Code Copyright: Copyright (c) 2010 Philip Regan All Rights Reserved.
Additional Copyright: None
Source:	replace_chars (c) Apple, Inc. Used with permission
Requirements: Adobe Photoshop CS4
Notes:	
Change History:
    10_05_03_01_00_000: Started source
*)

global gSourceFolder
global gDestinationFolder

property kFileExt_large : "_large.jpg"
property kFileExt_s : "_s.jpg"
property kFileExt_xs : "_xs.jpg"
property kFileExt_cs : "_cs.jpg"

property pFileList : {}

my ResetGlobalVariables()

tell application "Finder"
	set gSourceFolder to alias "Macintosh HD:Users:philipr:Desktop:RawCovers:"
	set gDestinationFolder to alias "Macintosh HD:Users:philipr:Desktop:SafariCovers:"
	--set gSourceFolder to choose folder with prompt "Please select the source file directory."
	--set gDestinationFolder to choose folder with prompt "Please select a directory to save the converted files."
	my createList(gSourceFolder)
end tell

if (get count of items in pFileList) > 0 then
	my ProcessFiles(gDestinationFolder)
end if

on ResetGlobalVariables()
	set gSourceFolder to ""
	set gDestinationFolder to ""
	set pFileList to {}
end ResetGlobalVariables

on ProcessFiles(mSaveFolder) -- (list)
	set fileCount to (get count of items in pFileList)
	repeat with thisFile from 1 to fileCount
		set theFile to item thisFile of pFileList

		set bookSaveFolder to MakeSaveFolder(mSaveFolder as string, theFile as string) of me

		my ProcessCurrentDocument(400.0, theFile as string, kFileExt_large, bookSaveFolder)
		my ProcessCurrentDocument(145.0, theFile as string, kFileExt_s, bookSaveFolder)
		my ProcessCurrentDocument(76.0, theFile as string, kFileExt_xs, bookSaveFolder)
		my ProcessCurrentDocument(42.0, theFile as string, kFileExt_cs, bookSaveFolder)

	end repeat
end ProcessFiles

on ProcessCurrentDocument(mNewWidth, mFilepath, mNewExtension, mSaveFolder)
	--log "ProcessCurrentDocument:" & {mNewWidth, mFilepath, mNewExtension, mSaveFolder}

	tell application "Adobe Photoshop CS4"

		open alias mFilepath showing dialogs never
		set theDocument to document 1

		try
			flatten theDocument
		end try

		try
			change mode theDocument to RGB
		end try

		set channelCount to (get count of channels in theDocument)

		set legalChannelNames to {"Red", "Green", "Blue"}
		repeat with thisChannel from channelCount to 1 by -1
			set theChannel to channel thisChannel of theDocument
			set theChannelName to name of theChannel
			if theChannelName is not in legalChannelNames then
				try
					delete theChannel
				end try
			end if
		end repeat

		try
			resize image theDocument width mNewWidth resolution 72 resample method bicubic
		end try

		set newFileName to GetNewFileName(mFilepath, mNewExtension) of me

		set fileDestination to ((mSaveFolder as string) & newFileName) as string

		try
			save theDocument in fileDestination as JPEG with options {embed color profile:false, format options:standard, quality:12}
		end try
		try
			close theDocument without saving
		end try
	end tell

end ProcessCurrentDocument

(* File Management Methods *)

on MakeSaveFolder(mSaveFolder, mRawFilePath) -- (string) as string
	set theFolderName to GetNewFileName(mRawFilePath, "") of me
	set theFolderName to replace_chars(theFolderName, "_CVRF_300CMY.tif", "") of me
	set bookSaveFolder to (mSaveFolder & theFolderName & ":") as string
	tell application "Finder"
		set theResult to make new folder ¬
			at mSaveFolder ¬
			with properties {name:theFolderName}
	end tell
	return bookSaveFolder
end MakeSaveFolder

on GetNewFileName(mFilepath, mNewExtension) -- (string, string) as string
	set newFileName to (current date) as string
	set currentFileName to GetFileNameFromPath(mFilepath) of me
	set newFileName to replace_chars(currentFileName, "_CVRF_300CMY.tif", mNewExtension) of me
	return newFileName
end GetNewFileName

on GetFileNameFromPath(mPath) -- (string) as string
	set oldDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {":"}
	set thePathList to every text item of (mPath as string)
	set AppleScript's text item delimiters to oldDelims
	set currentFileName to last item of thePathList
	return currentFileName
end GetFileNameFromPath

(* Apple's Methods *)

on createList(mSource_folder)
	set item_list to ""

	tell application "System Events"
		set item_list to get the name of every disk item of mSource_folder
	end tell

	set item_count to (get count of items in item_list)

	repeat with i from 1 to item_count
		set the_properties to ""

		set the_item to item i of the item_list
		set the_item to (((mSource_folder as string) & the_item) as string) as alias

		tell application "System Events"
			set file_info to get info for the_item
		end tell

		if visible of file_info is true then
			set end of pFileList to the_item
			if folder of file_info is true then
				my createList(the_item)
			end if
		end if

	end repeat
end createList

on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

Add Security Lines to Images

There are a variety of ways to apply security lines to make them difficult to remove. This is mine. Add salt to taste.

(*
File: Add Security Lines to Images
Author(s): Philip Regan
Source Code Copyright: Copyright (c) 2010 Philip Regan All Rights Reserved.
Additional Copyright: None 							
Source:	New source only; no adaptations.
Requirements: Adobe Photoshop CS4
Notes:	

Change History:
    10_08_17_01_00_000: Started source
*)

global kSourceFolder

set kSourceFolder to "Macintosh HD:Users:philipr:Desktop:SourceArt copy:"

set theItems to list folder kSourceFolder without invisibles
set itemCount to (get count of items in theItems)
repeat with thisItem from 1 to itemCount
	set theItem to item thisItem of theItems
	set theItemExtension to GetFileExtension(theItem) of me
	set theItem to (kSourceFolder & theItem) as string
	my ProcessFile(theItem, theItemExtension)
end repeat

on ProcessFile(theFile, theType) -- (string, string) as void
	tell application "Adobe Photoshop CS4"

		set ruler units of settings to pixel units
		set type units of settings to pixel units

		open alias theFile
		set theDocument to document 1
		tell theDocument

			set x to 1.0
			set y to 1.0
			set w to width
			set h to height

			if theType is in {"jpg", "JPG"} then

				set path1Layer to make new art layer at beginning with properties {name:"path1Layer", blend mode:overlay}
				set path1tla to {class:path point info, kind:corner point, anchor:{x, y}, left direction:{x, y}, right direction:{x, y}}
				set path1bra to {class:path point info, kind:corner point, anchor:{w, h}, left direction:{w, h}, right direction:{w, h}}
				set path1tl2br to {class:sub path info, entire sub path:{path1tla, path1bra}, operation:shape xor, closed:false}
				set path1 to make new path item with properties {entire path:{path1tl2br}, name:"tl2br", kind:normal}
				tell path1Layer
					stroke path path1 tool brush tool without simulate pressure
				end tell
				delete path1

				set path2bla to {class:path point info, kind:corner point, anchor:{x, h}, left direction:{x, h}, right direction:{x, h}}
				set path2tra to {class:path point info, kind:corner point, anchor:{w, y}, left direction:{w, y}, right direction:{w, y}}
				set path2bl2tr to {class:sub path info, entire sub path:{path2bla, path2tra}, operation:shape xor, closed:false}
				set path2 to make new path item with properties {entire path:{path2bl2tr}, name:"bl2tr", kind:normal}
				tell path1Layer
					stroke path path2 tool brush tool without simulate pressure
				end tell
				delete path2

			else if theType is in {"tif", "TIF"} then

				set path1Layer to make new art layer at beginning with properties {name:"path1Layer", blend mode:overlay}
				set path1tla to {class:path point info, kind:corner point, anchor:{x, y}, left direction:{x, y}, right direction:{x, y}}
				set path1bra to {class:path point info, kind:corner point, anchor:{w, h}, left direction:{w, h}, right direction:{w, h}}
				set path1tl2br to {class:sub path info, entire sub path:{path1tla, path1bra}, operation:shape xor, closed:false}
				set path1 to make new path item with properties {entire path:{path1tl2br}, name:"tl2br", kind:normal}
				tell path1Layer
					stroke path path1 tool brush tool without simulate pressure
				end tell
				delete path1

				set path2bla to {class:path point info, kind:corner point, anchor:{x, h}, left direction:{x, h}, right direction:{x, h}}
				set path2tra to {class:path point info, kind:corner point, anchor:{w, y}, left direction:{w, y}, right direction:{w, y}}
				set path2bl2tr to {class:sub path info, entire sub path:{path2bla, path2tra}, operation:shape xor, closed:false}
				set path2 to make new path item with properties {entire path:{path2bl2tr}, name:"bl2tr", kind:normal}
				tell path1Layer
					stroke path path2 tool brush tool without simulate pressure
				end tell
				delete path2

			end if -- theType

			flatten
			save
			close

		end tell
	end tell
end ProcessFile

on GetFileExtension(theFilename) -- (theFilename as string) as String
	set AppleScript's text item delimiters to "."
	set fileNameList to every text item of theFilename
	set AppleScript's text item delimiters to ""

	try
		return item 2 of fileNameList as string
	on error
		return ""
	end try
end GetFileExtension

Add text layer to document

The really odd part about this one is that stroke color is really the fill color for the text; stroke color doesn’t have the same meaning in Photoshop as it does in Illustrator and InDesign.

tell application "Adobe Photoshop CS4"
	set theDocument to document 1
	tell theDocument
		set newLayer to make new art layer with properties {kind:text layer, opacity:50, blend mode:multiply}

		tell newLayer

			set contents of text object to "Dave Bowman and Frank Poole"
			set justification of text object to left
			set size of text object to 12 -- points
			set stroke color of text object to {class:RGB color, red:0, green:0, blue:0}

		end tell -- newLayer
	end tell -- document
end tell -- application

CS4: Open Image In Photoshop Based On Extension

A common task in my work is to take a variety of image formats and convert them all into a single target format. For example, TIFF, EPS, and PDF into JPG. It used to be that I would handle opening on a case-by-case basis but now that the number of target products and source file formats has increased, I wrote this function to help managing the opening of files. It is possible to simply use open ImagePathwithout dialogs but that forces Photoshop to work much slower. Some things to note when using this…

  • This uses the GetPathComponentsFromAlias(ImagePath) function found elsewhere on this site and is not included any APIs.
  • Be sure that the resulting document object lands within a Photoshop tellblock or your script could get exciting.
-- Example code
tell application "Adobe Photoshop CS4"

	set theDocument to OpenImageInPhotoShopBasedOnExtension(anImagePath) of me
	(* do something with the document *)
end tell

on OpenImageInPhotoShopBasedOnExtension(ImagePath) -- (string || alias) as (Photoshop Document || nil)

	tell application "Adobe Photoshop CS4"
		set thePathComponents to GetPathComponentsFromAlias(ImagePath as string) of me
		set theFileExtension to fileExtension of thePathComponents

		set knownExtensionFound to false

		ignoring case

			-- Alias PIX

			-- BMP
			if theFileExtension contains "bmp" then
				set knownExtensionFound to true
				try
					open ImagePath as BMP showing dialogs never
				on error the error_message number the error_number
					my WriteLog("Error opening file: " & error_number & ":" & error_message & ": " & ImagePath)
				end try
			end if

			-- Camera RAW, OPTIONS

			-- CompuServe GIF	
			if theFileExtension contains "gif" then
				set knownExtensionFound to true
				try
					open ImagePath as CompuServe GIF showing dialogs never
				on error the error_message number the error_number
					my WriteLog("Error opening file: " & error_number & ":" & error_message & ": " & ImagePath)
				end try
			end if

			-- DICOM, OPTIONS	
			-- Electric Image	

			-- EPS, OPTIONS	
			-- EPS PICT preview, OPTIONS	
			-- EPS TIFF Preview, OPTIONS	
			-- Photoshop EPS	
			if theFileExtension contains "eps" then
				set knownExtensionFound to true
				try
					open ImagePath as EPS with options {constrain proportions:true, mode:RGB, resolution:300.0, use antialias:true} showing dialogs never
				on error the error_message number the error_number
					my WriteLog("Error opening file: " & error_number & ":" & error_message & ": " & ImagePath)
				end try
			end if

			-- Filmstrip	

			-- JPEG	
			if theFileExtension is in {"jpg", "jpeg"} then
				set knownExtensionFound to true
				try
					open ImagePath as JPEG showing dialogs never
				on error the error_message number the error_number
					my WriteLog("Error opening file: " & error_number & ":" & error_message & ": " & ImagePath)
				end try
			end if

			-- PCX	

			-- PDF, OPTIONS
			-- Photoshop PDF	
			if theFileExtension contains "pdf" then
				set knownExtensionFound to true
				try
					open ImagePath as PDF with options {crop page:true, mode:RGB, page:1, resolution:300.0, suppress warnings:true, use antialias:true} showing dialogs never
				on error the error_message number the error_number
					my WriteLog("Error opening file: " & error_number & ":" & error_message & ": " & ImagePath)
				end try
			end if

			-- Photo CD, OPTIONS	
			-- Photoshop DCS 1.0	
			-- Photoshop DCS 2.0	

			-- Photoshop format	
			if theFileExtension contains "psd" then
				set knownExtensionFound to true
				try
					open ImagePath as Photoshop format showing dialogs never
				on error the error_message number the error_number
					my WriteLog("Error opening file: " & error_number & ":" & error_message & ": " & ImagePath)
				end try
			end if

			-- PICT file	
			if theFileExtension is in {"pict", "pct"} then
				set knownExtensionFound to true
				try
					open ImagePath as PICT file showing dialogs never
				on error the error_message number the error_number
					my WriteLog("Error opening file: " & error_number & ":" & error_message & ": " & ImagePath)
				end try
			end if

			-- PICT resource	
			-- Pixar	

			-- PNG	
			if theFileExtension contains "png" then
				set knownExtensionFound to true
				try
					open ImagePath as PNG showing dialogs never
				on error the error_message number the error_number
					my WriteLog("Error opening file: " & error_number & ":" & error_message & ": " & ImagePath)
				end try
			end if

			-- Portable Bitmap	
			-- raw, OPTIONS	
			-- Scitex CT	
			-- SGI RGB	
			-- SoftImage	
			-- Targa	

			-- TIFF	
			if theFileExtension is in {"tif", "tiff"} then
				set knownExtensionFound to true
				try
					open ImagePath as TIFF showing dialogs never
				on error the error_message number the error_number
					my WriteLog("Error opening file: " & error_number & ":" & error_message & ": " & ImagePath)
				end try
			end if

			-- Wavefront RLA	
			-- Wireless Bitmap

		end ignoring

		if knownExtensionFound is false then
			my WriteLog("Extension not recognized: " & ImagePath)
		end if

		try
			set theDocument to document 1
		on error
			-- do nothing
		end try

		return theDocument

	end tell
end OpenImageInPhotoShopBasedOnExtension