Works Script Editor not Script Debugger

A very simple script or rather part thereof works in SE but not in SDB

tell application "Safari"
	activate
	tell application "System Events" to tell process "Safari"
		click menu 1 of menu bar item "file" of menu bar 1
	end tell
end tell

It works when I try the code here. I had to grant Script Debugger permission to use Accessibility features.

Please describe the error or problem you are experiencing.

The script also works for me in both Script Editor and Script Debugger. One possible issue is that a delay is needed.

tell application "Safari" to activate
delay 0.3 -- test different values
tell application "System Events" to tell process "Safari"
	click menu 1 of menu bar item "file" of menu bar 1
end tell

Hi I had already granted accessibility privileges .

I opened my Mac in safe mode ran diagnostics everything appears to be OK. Still same problem.

Peavine I 1st ran with a 5 second delay then I took out the activate Safari line still no luck.

Mark don’t know what else I can tell you. I am still on Mac 10.15.7 and SB version 7

thanks

The original script worked for me to click menu item 1, but failed with any other number.

This works for me (in Script Debugger and Script Editor):

tell application "Safari"
   activate
   tell application "System Events"
      tell its process "Safari"
         tell its menu bar 1
            tell its menu bar item "File"
               tell its menu "File"
                  click menu item "New Window"
                  click menu item 1
                  click menu item 2
                  click menu item 3
               end tell
            end tell
         end tell
      end tell
   end tell
end tell

FWIW, I built this script using Script Debugger’ss dictionary explorer. To do this I opened the System Events dictionary, selected processes, found the Safari process (I had to click Show More a few times). Double clicking the Safari process opened it full in the window. I then clicked:
Menu bars> menu bar 1 > menu bar items>menu bar item file>menu items
(this would be much easier using UI Browser!)

I find it’s more reliable to use the names than the index, but both work.

You may need to add delays before the commands, but I didn’t need to.

HTH
→ Script Debugger 8.0.4 (8A50)
→ Mac OS 11.6.3 (20G415)

I suspect this comes down to timing. Script Debugger, unlike the Script Editor, re-activates its self as you step through code and when the script ends. This can interfere with scripts like yours.

There are Scrpt Debugger pretences to control this behaviour. You can also use the Mini Debugger window which prevents the activations and may allow scripts like yours which are sensitive to window/application layering to operate correctly.

At a technical level, the activate command is not blocking which means it returns before the target application is fully active. Delays can sometimes help but its a guessing game to know how long is enough.

Ed , afraid your suggestion just opened successive windows.

Mark running in mini debugger does work but this is part of a long (for me script) is this the only way as I cannot of course step through my script in MDB which is what SB is all about at least for me

Thanks again

Peter

The mini debugger window does support stepping - just enable debugging while Script Debugger is the active application with the Script > Enable Debugging (Command-Shift-D) command. From there, you’ll see the step-wise execution buttons appear in the mini-debugging window. There are other approaches: set a breakpoint after the troubling part of your script and step normally from there.

When I read your script it seems that it is intended to open a window.

The version I posted does that and illustrates how to access the next couple menu items (new tab, new private window).

This script shows one way to make that command work:


tell application "Safari"
   activate
   tell application "System Events"
      tell its process "Safari"
         tell its menu bar 1
            tell its menu bar item "File"
               tell its menu "File"

                  click menu item 1
      
               end tell
            end tell
         end tell
      end tell
   end tell
end tell

Got it should have tried that before responding.

1 Like