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.
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.