Avoid warning when doing shell script in the middle of a tell-statement?

Do you get any errors/warnings for the last two examples?

You donā€™t tell document 1 in your example.

No. But it may just be that they happen to work in Safari on my Mojave system. Have you tried giving the document and the ā€˜logā€™ command their own reference contexts within the repeat?

tell application "Safari"
	repeat
		tell document 1
			-- document 1 code here
		end tell
		log "Loops: - " & (time string of (current date))
	end repeat
end tell

FWIW, I tested Nigelā€™s first variant on my Ventura computer, and it worked as expected but issued the privilege violation error. Normally this would not be an issue, but it could be of some significance, because the OPā€™s script is an endless loop that works without any delay. The second variant works as expected and does not return the privilege violation error. I also tested Nigelā€™s script in post 23, and it issued the privilege violation error. So, the second variant seems a good solution IMO.

The OP has indicated that the Apple 2-line solution is not acceptable, but it does work and does not return the privilege violation error. Just for the record:

tell application "Safari"
	tell document 1 to repeat
		tell current application to set theTime to time string of (current date)
		log "Loops: - " & theTime
	end repeat
end tell

BTW, Nigelā€™s and my script suggestions run OK in Script Debugger, but in Script Editor they lock up the app because of the endless loop. So, just for testing, itā€™s probably best to use a delay or to limit the repeat to a specified number of times or to use Script Debugger.

I did some testing on this, and Mockmanā€™s understanding seems entirely correct. Me (as in tell me) is always the current script, but current application varies depending on how the script is run:

HOW RUN - NAME OF CURRENT APPLICATION
Script Debugger - Script Debugger
Script Editor - Script Editor
FastScripts - FastScript Script Runner
Script Menu - osascript
AppleScript App - the name of the script
Shortcuts App - ShortcutsMacHelper.xpc

In the end, I suspect everything is run by the current appllication, but it makes sense to use tell me rather than tell current application.

1 Like

The thing is that I am doing a lot of logging and donā€™t want to clog up my code with two log lines for every ā€œrealā€ line of code.

Is all your logging related to Safari? Then you could perhaps use do JavaScript "console.log(...)" in your tell "Safari" block. The logging goes to Safariā€™s developer toolā€™s console, though.

It is in Safari but not Safari specific. Thanks for the suggestion though.

The standard way to make a call to a command outwith the scope in which the call originates would be to use continue, e.g.

tell application id "com.apple.Safari"
        continue do shell script "echo Hello World!"
end tell

I donā€™t know whether it will prevent a privilege violation, as Iā€™m a version behind with macOS, and I donā€™t receive such an error. However, it will prevent other silent errors being thrown in the background as the inheritance chain is ascended and the call is sent out at each level until a receiver understands.

1 Like