Questions about Safari AppleScript

Which script should I use to turn the safari web page translation function added to Ventura on and off with Apple script?

Thank you in advance :slight_smile:

Model: MacBook Pro 14
AppleScript: 2.11
Browser: Safari 605.1.15
Operating System: macOS 12

The way I do it doesn’t involve AppleScript at all.
I used System Preferences->Keybord->Shortcuts->App Shortcuts

  1. New entry → point to Safari.app
  2. Menu Title: Translation to English
  3. Keyboard Shortcuts: ^t (that is control + t)

App Shortcuts work for any Application and menu bar item. It use keyboards events on frontmost application. So if you like to make a script you should make sure the target app (Safari) is frontmost.

So in my case it looks like this.

tell application "System Events" to tell process "Safari"
	set frontmost to true
	key code 17 using {control down} --> control + t
end tell

Thank you for telling me in detail Fredrik71 :slight_smile:

I do not know if your language is English but here is example with GUI scripting.
If your language is different you could change the property to your like.

First run turn on the second run turn off…

Or

You could make other keyboard shortcuts to target “View Original” so you could switch the translation off. ex. control + option + t

property setOn : "Translate to English"
property setOff : "View Original"
property menuItem : "Translation"
property menuBarItem : "View"

property appName : "Safari"

tell application "System Events" to tell process appName
	set frontmost to true
	tell menu bar 1 to tell menu bar item menuBarItem to tell menu 1
		tell menu item menuItem to tell menu 1
			tell menu item setOff to click
			tell menu item setOn to click
		end tell
	end tell
end tell