Help with this simple script

Hi guys, I have a bunch of text file I want to open one at a time in TextEdit, print as PDF and, as soon as the PDF is made on the Desktop, get “Preview” to open it. I have a hard time getting preview to only try to open the PDF when it’s actually ready. Right now I’m getting “Preview got an error: Can’t get file "width_9.1_height_6.07_e.txt.pdf".”"

Thanks for your help.

tell application "Finder"
	activate
	set my_folder to (choose folder)
	set my_text_files to (every item in my_folder)
	repeat with this_text_file in my_text_files
		tell application "TextEdit"
			activate
			open this_text_file
			set my_name to get (characters 1 thru -5 of (get the name of this_text_file) as string)
			ignoring application responses
				print this_text_file with properties {target printer:"Adobe PDF 8.0"} without print dialog
				delay 1
			end ignoring
			set file_to_open to my_name & ".txt.pdf" as string
			tell application "Preview"
				activate
				repeat until file file_to_open of desktop exists
				end repeat
				open file file_to_open of desktop
			end tell
		end tell
	end repeat
end tell

Hi,

first of all:

NO NESTED APPLICATION TELL BLOCKS !!

Preview doesn’t open the file because Preview has no idea what a desktop is


set my_folder to (choose folder)

tell application "Finder"
	set my_text_files to every item in my_folder
end tell

repeat with this_text_file in my_text_files
	tell application "TextEdit"
		open (this_text_file as alias)
		set my_name to text 1 thru -5 of (get the name of this_text_file)
		ignoring application responses
			print this_text_file with properties {target printer:"Adobe PDF 8.0"} without print dialog
			delay 1
			close front document
		end ignoring
	end tell
	set file_to_open to (path to desktop as text) & my_name & ".txt.pdf"
	tell application "Finder"
		repeat until exists file file_to_open
			delay 0.2
		end repeat
	end tell
	tell application "Preview"
		open alias file_to_open
	end tell
end repeat

I knew you’d be the first one to jump in Stefan :slight_smile:
Thank you!