Scripting Print Dialogue Box Options in PhotoShop

Okay, I’m a newbie here and am blown away by the depth of knowledge, dedication and generosity shown on this site. It’s an incredible resource for even a “code-adverse” creative director type like myself.

I’ve been trying to script a workflow and have been having a devil of a time getting past one step where it consistently hangs. In a nutshell:

  1. RAW photo images from a DSLR camera tethered to a MacBook Pro are automatically downloaded to a “Watch Folder”
  2. Every new image added to the folder invokes a script which opens Photoshop, activates an action that applies some adjustments, then prints to a specified printer with specific page setup options
  3. Image file is then saved as a jpeg to a designated folder

I’ve tried using Automator as a simple solution, bypassing Photoshop and opening files in Preview just to see if I could get something to work on my own, but the “Print” action will not allow me to specify which printer and paper handling so it hangs.

I have also tried recording the print dialogue selections in Photoshop but get the warning that not all features and selections are recordable as an action and that has shown to be proven true when I run it.

I’ve seen some other posts that allude to Applescripting the Print Dialogue options to print as pdf’s but nothing on selecting some other specific external printer.

Any suggestions as to what a script might look like to achieve what I’ve described? My attempt is too embarrassing to post.

Any help would be greatly appreciated. Thanks in advance.

-Kevin

Model: MacBook Pro 2.66 OSX10.6.4
AppleScript: 2.1.2
Browser: Safari 533.16
Operating System: Mac OS X (10.6)

Hello.
If you manage to save the file in a format that suits you for printing, then this thread may help.

Hello

I looked at the pointed thread and tried to use the grabbed infos.

When I issue this command :


do shell script "lp -d Dell_Laser_Printer_3100cn-1 -o number-up=2 '/Users/yvan_koenig/Desktop/4merge.pages/'"

I get the error message :
do shell script “lp -d Dell_Laser_Printer_3100cn-1 -o number-up=2 ‘/Users/yvan_koenig/Desktop/4merge.pages/’”
“lp: Is a directory”

I assumes that it’s linked to the fact that my Pages document is a package.

If I try to apply it to a Pages flatfile document, I get garbage and more annoying, the part of the command supposed to ask the printer to print two pages per sheet of paper doesn’t do its duty.

Are you sure that such a tool may be used to print a Photoshop document ?

Yvan KOENIG (VALLAURIS, France) jeudi 29 juillet 2010 16:48:28

Hello Yvan.

And no, that is not the proper way to print a photoshop document. I think I just wanted to address a method of finding a current printer or setting a printer, or just addressing a specific printer by pointing to that thread. Which the thread covers fairly well. -Which was what I perceived as the OP’s problem. :slight_smile:

Thank you for your responses. They were much appreciated. After reviewing many posts on trying to script the Page Setup dialogue box I have found there seems to be no easy solution and no real consensus on the best workaround. In desperation I came up with this script which relies on my having set the printer and custom paper size I need to use for my particular process as a default preset in the Page Setup dialogue box so my script needs only accept the default values. Here’s what I have so far that works when an image is downloaded from a tethered camera to the watch folder that has the Folder Action associated with it.

Unfortunately, I have found that if a sequence of images have been taken by the camera in rapid succession the script, as it exists now, only processes the first image and ignores the remaining files the camera has downloaded in the folder. I think I need to insert a looping segment that will move the first processed file to a designated folder then has Photoshop check to see if there are additional files left in the watch folder and process and move each one until the folder is empty. Could use some help with that as I’m not sure what that code would look like and where it needs to go in my script.

Anyway, for what it’s worth, here’s the script I cobbled together, however inelegant it may be, it does work when photos are taken with enough of a pause between them for the script to execute and the image printed… about 45 seconds from clicking the shutter to final print.

Thanks again for everyone’s help.

-Kevin

-- Folder Action that works on a watch folder and activates when an image is downloaded to it from a tethered camera
on adding folder items to thisFolder after receiving thisItem
	-- Script opens the image file in Photoshop, runs an action that adjusts gamma and prints the image to a specified printer then saves and closes file back to the watch folder 
	tell application "Adobe Photoshop CS4"
		activate
		open thisItem
		tell application "Adobe Photoshop CS4"
			do action "Photo Process" from "Photo Actions"
		end tell
	end tell
	tell application "System Events"
		activate
		tell process "Photoshop"
			set frontmost to true
			keystroke "p" using {command down}
			key code 36
			delay 2
			key code 36
		end tell
	end tell
	-- After file has been processed, printed and saved this part of the script moves the processed file from the watch folder to a folder called "Processed_Images". Unfortunately this part does not work - still working on why not.
	tell application "Finder"
		move thisItem to (folder "Volumes:Macintosh HD:Processed_Images")
	end tell
end adding folder items to

It’s really easy to select a printer to print to. First you need to know the printer names that you have available so run this script and look at the results for the names…

set printerNames to {}
tell application "Printer Setup Utility"
	launch
	repeat with i from 1 to count of printers
		set end of printerNames to name of printer i
	end repeat
	quit
end tell
return printerNames

Now that you have the names you can use this script to select one of those names. This way when you go to print you know that named printer will be selected…

set myPrinterName to "Canon MP540 series"

tell application "Printer Setup Utility"
	launch
	set initialPrinterName to the name of current printer
	if initialPrinterName is not myPrinterName then
		repeat with i from 1 to count of printers
			if name of printer i is myPrinterName then
				set current printer to printer i
				exit repeat
			end if
		end repeat
	end if
	quit
end tell

Now to print in photoshop is easy. You run the above code first to make sure the appropriate printer is selected, then run this code to actually perform the print…

tell application "Adobe Photoshop CS4"
	tell document 1
		-- modify the document however you want
		print
	end tell
end tell

If you look in photoshop’s applescript dictionary there are some printer settings that you can make however I’m not sure you’ll be able to make all of the settings that you want. You might google a little to find examples from others because maybe someone figured something out. Your other option is to use gui scripting to open the printer dialog manually and click/press the options you’re interested in.

Maybe this script written to set the document orientation in Pages may be used as a starting point.


--{code}
--[SCRIPT set_document_orientation]
(*
Enregistrer le script en tant que Script : set_document_orientation.scpt
déplacer le fichier ainsi créé dans le dossier
<VolumeDeDémarrage>:Users:<votreCompte>:Library:Scripts:Applications:Pages:
Il vous faudra peut-être créer le dossier Pages et peut-être même le dossier Applications.

Ouvrir un document Pages

aller au menu Scripts , choisir Pages puis choisir  set_document_orientation

Passer la chaîne "Portrait" pour adopter le format 'portrait'.
Toute autre chaîne activera le mode paysage.
Je me suis amusé à passer le titre d'une œuvre de Charles MINGUS.

Vous avez sans doute compris que ce sont surtout les routines qui sont utiles.
N'hésitez pas à les intégrer à un ou plusieurs scripts plus élaborés.

--=====

L'aide du Finder explique:
L'Utilitaire AppleScript permet d'activer le Menu des scripts :
Ouvrez l'Utilitaire AppleScript situé dans le dossier Applications/AppleScript.
Cochez la case "Afficher le menu des scripts dans la barre de menus".

--=====

Save the script as a Script: set_document_orientation.scpt

Move the newly created file into the folder:
<startup Volume>:Users:<yourAccount>:Library:Scripts:Applications:Pages:
Maybe you would have to create the folder Pages and even the folder Applications by yourself.

Open a Pages document

go to the Scripts Menu, choose Pages, then choose "set_document_orientation"

Pass the string "portrait" if you want to use the portrait orientation.
Pass any other string to use the landscape one.
Here I play the fool passing the title of a Charles MINGUS composition.

In fact the script is just a demo.
A really useful script would embed the handlers so that you may rule the document orientaion from it.

--=====

The Finder's Help explains:
To make the Script menu appear:
Open the AppleScript utility located in Applications/AppleScript.
Select the "Show Script Menu in menu bar" checkbox.

--=====

Yvan KOENIG (VALLAURIS, France)
2010/07/20
2010/07/21 -- splitted the original script in two ones.
2010/07/22 -- corrected a stupid typo
*)

--=====

(* I define the target application in a property so that I change only this single value 
when I want to run Pages '08 which is named Pages08 on my machines
*)
property theApp : "Pages"

--=====

on run
	local wName
	
	my activateGUIscripting()
	
	if theApp does not contain "Pages" then -- Oops, I forgot an odd underscore in the variable name
		if my parleAnglais() then
			error "Script dedicated to Pages !"
		else
			error "Script dédié à Pages !"
		end if -- parleAnglais
	end if
	
	tell application theApp
		try
			set wName to name of window 1
			tell document 1
				--
			end tell
		on error (* Can't get name of window 1 *)
			if my parleAnglais() then
				error "There is no "" & theApp & "" window open !"
			else
				error "il n'y a pas de fenêtre « " & theApp & "» ouverte !"
			end if -- parleAnglais
		end try
	end tell -- theApp
	
	my setPagesDocumentOrientation(theApp, "All the things you could be by now if Sigmund Freud's wife was your mother", wName)
end run

--=====

on parleAnglais()
	local z
	try
		tell application theApp to set z to localized string "Cancel"
	on error
		set z to "Cancel"
	end try
	return (z is not "Annuler")
end parleAnglais

--=====

on getLocalizedString(a, x)
	tell application a to return localized string x
end getLocalizedString

--=====

on activateGUIscripting()
	(* to be sure than GUI scripting will be active *)
	tell application "System Events"
		if not (UI elements enabled) then set (UI elements enabled) to true
	end tell
end activateGUIscripting

--=====

on setPagesDocumentOrientation(the_app, whichOne, w_name)
	
	if 5 > (system attribute "sys2") then
		(* 10.4.11 *)
		set Cancel_loc to my getLocalizedString("Finder", "AL1") -- Annuler
		set Continue_loc to my getLocalizedString("Finder", "AL3") -- Continuer
		if my parleAnglais() then
			display dialog "Script unable to rule orientation" & return & "under Mac Os X 10.4.x" buttons {Cancel_loc, Continue_loc} default button 2
		else
			display dialog "Script incapable de régir l'orientation" & return & "sous Mac Os X 10.4.x" buttons {Cancel_loc, Continue_loc} default button 2
		end if -- parleAnglais
	end if --  5 > (system attribute.
	
	tell application the_app to activate
	
	tell application "System Events" to tell application process the_app
		(* trigger the menu item  File > Page Setup. *)
		keystroke "p" using {command down, shift down}
		(* Wait for the "Page Setup" sheet *)
		repeat until exists sheet 1 of window w_name
			delay 0.1
		end repeat
		(*
Now, the sheet is available, we may trigger an UI element *)
		tell sheet 1 of window w_name
			if whichOne is "portrait" then
				click UI element 1 of radio group 1 (* radio button if 10.5.x, checkbox if 10.6.x *)
			else
				click UI element 2 of radio group 1 (* radio button if 10.5.x, checkbox if 10.6.x *)
			end if
			-- title of button 1 (* "OK" *)
			click button 1
		end tell --sheet 1 of window
	end tell -- System Events
end setPagesDocumentOrientation

(*
If it's macOs X 10.5.x,
get properties of UI element 1 of radio group 1
returns :
{size:{38, 38}, focused:false, description:"case à cocher", subrole:missing value, minimum value:missing value, enabled:true, role:"AXCheckBox", name:missing value, value:1, selected:missing value, class:checkbox, title:"", help:missing value, position:{1249, 319}, orientation:missing value, entire contents:{}, maximum value:missing value}

if it's macOs X 10.6.x,
get properties of UI element 2 of radio group 1
returns :
{minimum value:missing value, orientation:missing value, position:{1353, 389}, class:radio button, role description:"bouton radio", accessibility description:"Paysage", focused:false, title:"", size:{38, 38}, value:1, help:missing value, enabled:true, maximum value:missing value, role:"AXRadioButton", entire contents:{}, subrole:missing value, selected:missing value, name:missing value, description:"Paysage"}

I use the generic "UI element" name which apply to radio button as well as to checkbox.
Under 10.4 these iteems are of a third kind : button. Maybe they will use a fourth one in 10.7 ;-)
*)
--=====
--[/SCRIPT]
--{code}

Yvan KOENIG (VALLAURIS, France) jeudi 29 juillet 2010 20:19:35