MacOS – Run an AppleScript that requires keystrokes while the screen is locked

applescriptmacosscreensaver

I've got a very simple AppleScript whose job it is to type some words into a text box.

tell application "TextEdit"
  activate
end tell
delay 0.2
tell application "System Events"
  keystroke "Hello World!"
  keystroke return
end tell

I want this script to run every time my screen saver activates, and again every time my screen saver deactivates. I installed ScriptSaver and it works perfectly. So far, so good.

Except my screen locks when the screen saver activates. This means that when my script tells TextEdit to activate, the computer pops up its login window and thinks I'm typing "Hello World!" as a password. Not ideal.

I tried making the screen saver require a password after 5 seconds, instead of immediately, but that was no good: the process of activating TextEdit just wakes up the screen again.

Is there a way to activate TextEdit and type words into it in the background, without making my computer think I'm trying to unlock the screen?

Best Answer

No i'don't think you can do this the way you want.

  1. Every Human interaction on a PC during Screensaver tries to deactivate it. (Like moving the Mouse, pressing a Key e.g.)

  2. Applescript most time is used to Script some GUI (like in your case --> tell application TextEdit -> does nothing other than opening Textedit like you would do it by double clicking)

So to bring you a way which should work:

do a applescript which writes the text to a file directly and then open the file with the text in it if needed.

something like this:

tell application System Events
    do shell script "echo 'your text that you wanna type' > /path/to/the/file/where/the/text/stands/in"
end tell

tell application TextEdit
    open /path/to/the/file/where/the/text/stands/in
end tell