Set Tags in Safari's > File > Print > Save as PDF

I want to set the tags in the second sheet using
Safari > File > Print > Save as PDF

When I do it manually it works, with AS, visually it seems to
set value of text field 2 to "orange"
But when I CMD + I test.pdf no tags are written.

tell application "System Events" to tell application process "Safari"
set frontmost to true

# window 1
tell window 1
	
	# PRINT - using keystrokes is a lot faster
	keystroke "p" using {command down}
	
	# check for PRINT sheet
	repeat until exists sheet 1
		delay 0.2
	end repeat

	# PRINT sheet
	tell sheet 1
		
		# > PDF > Save as PDF (in a splitter group)
		tell splitter group 1
			click menu button "PDF"
			repeat until exists menu 1 of menu button "PDF"
				delay 0.2
			end repeat
			click menu item "Save as PDF" of menu 1 of menu button "PDF"
		end tell -- splitter group 1
		
		# check for "Save as PDF" sheet
		repeat until exists sheet 1
			delay 0.2
		end repeat

		# "Save as PDF" sheet
		tell sheet 1
			
			# change the filename of the PDF
			set value of text field 1 to "test.pdf"
			delay 3
			# set tags
			set value of text field 2 to "orange"
			delay 3
			# set author
			set value of text field 7 to "John Doe"
			delay 3
			
			# return keystroke is faster
			# saves the file into the last used directory
			keystroke return
			
			# if the file esists, replace it
			# check if "Repace Existing" sheet appears
			delay 0.2
			if exists sheet 1 then
				log "replace"
				tell sheet 1 to click button "Replace"
			else
				log "save"
			end if
		end tell -- "Save as PDF" sheet
		
	end tell -- # PRINT sheet
	
end tell -- window 1

# check Print Processing sheet
repeat until exists sheet 1 of window 1
	delay 0.2
end repeat
# if the Print Processing sheet is no more
repeat until (exists sheet 1 of window 1) = false
	delay 0.2
end repeat

log ("DONE processing!")

end tell – application “System Events” > application process “Safari”

What’s happening is that the tags aren’t getting a chance to be accepted in the field before you move on to saving the file. I’m guessing that it’s because tags aren’t text and need to be ‘set’. Consider that when you type a tag into a field, you typically follow up with a return or tab and that converts the text “blue” into the tag “Blue”. So I’ve added a tab — not a return since that also triggers the save — and a brief delay before proceeding to the save.

tell application "Safari"
	activate
	tell application "System Events" to tell application process "Safari"
		
		keystroke "p" using {command down}
		delay 3 -- this is where the longest delay is required 
		set sh1 to sheet 1 of window 1 -- print sheet
		
		perform action "AXShowMenu" of menu button "PDF" of sh1
		perform action "AXPress" of menu item "Save as PDF…" of menu "PDF" of menu button "PDF" of sh1
		
		set sh11 to sheet 1 of sheet 1 of window 1 -- save as sheet
		
		set value of text field "Tags:" of sh11 to "Yellow	Red" -- space between is a tab
		-- set value of text field "Tags:" of sh11 to "Yellow"
		key code 48 -- tab out of Tags field affirms tag
		delay 0.5
		perform action "AXPress" of button "Save" of sh11
		
		
	end tell
end tell

As an aside, Safari has an export to pdf menu item that might be easier to trigger.

Also, it is possible to set up a keyboard shortcut for the Save as PDF… sheet menu item which lets you type command-p command-p and save to a PDF. If you use it a lot, you might look into that.

OK I got it.
I my case (?) I still needed to use the splitter group.
Than you used Save as PDF… with three dots, which my menu does not have.
My tabs needed to be separated by a comma, not a space.
Lastly I had to use 2 times the tab to register the tags.

tell application "Safari"
activate
tell application "System Events" to tell application process "Safari"
	
	keystroke "p" using {command down}
	delay 1 -- this is where the longest delay is required 
	
	set sh1 to sheet 1 of window 1 -- print sheet
	set sg1 to splitter group 1 of sh1 -- splitter group of print sheet
	
	perform action "AXShowMenu" of menu button "PDF" of sg1
	perform action "AXPress" of menu item "Save as PDF" of menu "PDF" of menu button "PDF" of sg1
	
	set sh11 to sheet 1 of sheet 1 of window 1 -- save as sheet
	
	# filename
	set value of text field "Save As:" of sh11 to "test.pdf"
	# tags
	set value of text field "Tags:" of sh11 to "Green, Red, foobar"
	keystroke tab -- tab out of Tags field affirms tag
	keystroke tab -- yes 2 times seems necessary
	# author
	-- set value of text field "Author:" of sh11 to "John Doe" -- does not work
	set value of text field 7 of sh11 to "John Doe"
	
	delay 0.5
	perform action "AXPress" of button "Save" of sh11
	-- keystroke return
	
end tell

end tell

Weird and unpleasant but that’s UI scripting.

The author field for me is:

set value of text field 4 of group 1 of sh11 to "John Doe"

Oddly, the field above it is field 3, and that below is field 2. I am unable to modify the keywords field at all — keywords is field 1 and at the bottom. It’s pretty haphazard.

As to the tags field, I can only wonder if someone at apple didn’t ask why they were relying on a tab inside a text field and someone else could only reply, “Because.” Oh well.

Glad you got it working.