View Single Post
Hi,
finally, I've found a solution on Internet and modify it a little to make it work.
Thanks again for everything !!! I appreciate your help !

here is the code if your need it :

Code:
# Saves current document open in foreground app as PDF using the Print dialog "Save as PDF" option

# value to limit how long we wait for a given dialog or menu popup
set maxloops to 100

# Open a log file - only used for debugging
# Log file is written to the root of the hard disk
# set fid to open for access "mylog.txt" with write permission

### Set process name based on the frontmost app ###
# This allows the script to run with whatever app is in foreground
# Note: Some apps use a property sheet and some use a window for the print
#          Dialog so depending on which we need to take different actions
set process_name to displayed name of (info for (path to frontmost application))
# If needed, you can hard-code the app name by uncommenting the following:
# set process_name "Firefox"
# If we hard-code the name, we should active the app
# activate application process_name 

tell application "System Events"
	tell process process_name
		
		# Open the print dialog
		keystroke "p" using command down
		
		# Wait until the Print dialog opens before proceeding
		set prdlg to 0 # Initialize loop variable
		set lcnt to 0 # Counter to prevent infinit loops
		repeat until prdlg is not 0
			# Determine if the app is using a dialog or a sheet for print options and then create reference to dialog or sheet for use later
			if exists window "Print" then
				# Current App uses the Print dialog (not sheet)
				set prdlg to 1
				set prdlgref to a reference to window "Print"
				set wtype to "dlg"
			else if exists sheet 1 of window 1 then
				# Current App uses the Print sheet (not dialog)
				set prdlg to 1
				set prdlgref to a reference to sheet 1 of window 1
				set wtype to "sht"
			end if
			set lcnt to lcnt + 1
			if (lcnt) > maxloops then
				display dialog "Reached Max Loops waiting for print dialog to open."
				return
			end if
		end repeat
		
		# Expand the "PDF" menu button (must be expanded before the menu is referencable)
		click menu button "PDF" of prdlgref
		
		# Wait until the Menu button menu is created before proceeding
		set lcnt to 0
		repeat until exists menu item "Enregistrer au format PDF…" of menu 1 of menu button "PDF" of prdlgref
			set lcnt to lcnt + 1
			if (lcnt) > maxloops then
				display dialog "Reached Max Loops waiting for Save as PDF dropdown to open."
				return
			end if
		end repeat
		
		# Select the "Save as PDF" menu item
		click menu item "Enregistrer au format PDF…" of menu 1 of menu button "PDF" of prdlgref
		
		# Wait until the Save dialog opens before proceeding
		keystroke return	
	end tell
end tell