How do I enter Data into TextEdit's print sheets 'to' text field

G’day scripters

I’m trying the following to enter data in the last page ‘from to’ box in the TextEdit print window.

Trouble is, it’s proving unreliable, and sometimes enters the data into the ‘from’ box instead of the ‘to’ box.

I’ve played with the timing, but it doesn’t make any difference.

The text field is part of an unknown element, UI element 1 of UI element 5 of sheet “Print” of window “Untitled 88.rtf”, and I can’t work out how to directly address it.

Can someone guide me in the right direction please.

Regards

Santa

	
tell application "System Events" to tell process "TextEdit"
							keystroke "p" using command down
							delay 6
							tell window 1
								repeat 4 times
									keystroke tab
									delay 0.5
								end repeat
								delay 2
								keystroke MaxAttachmentPages as string
								delay 0.4
							end tell
							tell sheet "print"
								keystroke return
							end tell
						end tell

Hi Santa.

first open System Preferences > Keyboard & Mouse > Keyboard Shortcuts
and make sure that “Text boxes and lists only” is chosen at the bottom of the window,
then try this

property MaxAttachmentPages : "4"

activate application "TextEdit"
tell application "System Events" to tell process "TextEdit"
	keystroke "p" using command down
	tell window 1
		repeat until sheet 1 exists
			delay 0.5
		end repeat
		keystroke tab
		keystroke tab
		keystroke MaxAttachmentPages
		click button "Print" of UI element 4 of sheet 1
	end tell
end tell

I use a very similar script to do both from and to:

-- assuming text boxes and lists only (else two more tabs)
property fromPage : "2"
property toPage : "4"

activate application "TextEdit"
tell application "System Events" to tell process "TextEdit"
	keystroke "p" using {command down}
	repeat until exists sheet 1 of window 1
		delay 0.5
	end repeat
	keystroke tab & fromPage & tab & toPage
	click button "Print" of UI element 4 of sheet 1 of window 1
end tell

Hi.

Here’s an attempt at a version that’ll work whatever the “Keyboard Shortcuts” setting. It’s OK on my machine, but isn’t guaranteed .

set MaxAttachmentPages to 4

tell application "TextEdit" to activate
tell application "System Events"
	tell process "TextEdit"
		--set frontmost to true
		tell sheet 1 of window 1
			-- Cancel any currently open sheet.
			if (it exists) then
				keystroke "." using command down
				repeat while (it exists)
					delay 0.2
				end repeat
			end if
			-- Open the "Print" sheet.
			keystroke "p" using command down
			repeat until (it exists)
				delay 0.2
			end repeat
			-- Set the focus to the "Copies & Pages" pop up button.
			set value of attribute "AXFocused" of pop up button 1 to true
			if (value of attribute "AXFocused" of pop up button 1) then
				-- If the pop up admits to being focused, the "Keyboard Shortcuts" tab preference is "All controls".
				-- It'll take five tabs to get from the pop up to the "To:" field.
				set tabs to 5
			else
				-- Otherwise the tab preference is "Text boxes and lists only".
				-- It'll take three tabs to get from the pop up to the "to:" field.
				set tabs to 3
			end if
			repeat tabs times
				keystroke tab
			end repeat
			keystroke MaxAttachmentPages as string
			click button "Print" of UI element 4
		end tell
	end tell
end tell

Thanks fellas, the best bit is the

repeat until sheet 1 exists
delay 0.5
end repeat

which seems to have fixed my problem.

However, if anyone knows how, this inquisitive ol’ bastard would like to know how to directly address that field, please.

Just for the hell of it.

Regards

Santa

Works on mm for both Keyboard & Mouse settings, Nigel.

According to UI Browser, the from and to fields are defective – they don’t have text field numbers.

btw: according to Text Edit’s dictionary this should work actually, but doesn’t

tell application "TextEdit"
	print document 1 with properties {starting page:1, ending page:4} without print dialog
end tell

I hope, Apple will fix these little bugs in Leopard :rolleyes:

G’day again

According to my UI browser, the text field ‘to’ has the following details.

PATH TO ELEMENT
application “TextEdit”
standard window “untitled 88.rtf” (window 1)
sheet “Print” (sheet 1)
text field (text field -2147483648)

As well as ELEMENT:, ACTIONS (2):, ATTRIBUTES: (26)

Santa

Model: G5 1.8 GHz
AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Thanks for the confirmation, Adam.

And thanks for that information too, Stefan. I’d just come back on line to ask about it. I’m not able to test it myself as I have a legacy printer that can only be used from OS 9!

But that negative number is the largest possible negative integer - gotta tell you something is screwed up with the labeling of that text field; but the real kicker is that the other text box has the same huge number.

This may be the reason that starting number, etc. doesn’t work either.