Not Knowing Your Screen Time Code
The average American spends 4h30m on their phone each day.[1] I've done some things to curb this in the past (see my post on Grayscale Lock which you can now do on stock iOS with Shortcuts!) but the number one thing I've found to work is using the built-in Screen Time tools. You may have tried this and found yourself just typing in the code to bypass the limit, but here's the twist — I don't know my code.
The trivial way to do this is to have someone else set the code and write it down somewhere, and that's honestly what I recommend. But in WWDC 2024 Apple announced "iPhone Mirroring", a new app that allows you to control your iPhone from your Mac, and I wanted to see if I could automate this.
First of all you're going to need a Mac running macOS Sequoia 15, and an iPhone running iOS 18 to be compatible (there's some other requirements too). With that you should have everything you need. Make sure that you can open the iPhone Mirroring app and connect and click around on your phone first. If that's working, let's jump into it!
Setting It Up
Open up Terminal. If you don't know how to find this, you can press Cmd ⌘ + Space to open Spotlight and search for "Terminal". You'll want to make sure that you're in a place where you can keep a file with the password you're not going to know, I recommend running cd ~/Desktop
to save it to your desktop. Then you should copy and paste the following command into Terminal.
osascript -e 'tell application "System Events" to keystroke "clear; echo \"\\033[32;1;4mReady.\"\n"'
If it says "Ready" at the top of your screen then you're good to go.
However it may say
36:83: execution error: System Events got an error: osascript is not allowed to send keystrokes. (1002)
or display a popup like this.
In both cases you'll need to give Terminal permission to type. You can click the "Open System Settings" button if you have it, or open the Settings app normally and navigate to Privacy & Security > Accessibility. There should be an option for Terminal, make sure the toggle is ticked and blue.
Once this is setup, you will need to open the iPhone Mirroring application, and connect it to the iPhone you want to set the code for. Once it's open, navigate to the Screen Time section (either through Settings or through searching "Screen Time" in Spotlight on the phone using Cmd ⌘ + 3 or the View menu. Scroll down to "Change Screen Time Passcode" and enter your current screen time passcode. You should now be at the "Enter New Screen Time Passcode" screen.
Open up your Terminal window and copy and paste the following command.
RANDOM=$(od -An -N4 -tu4 < /dev/urandom); code=$(printf "%04d" $((RANDOM % 10000))); echo "$code" > screen_time_passcode.txt; osascript -e 'tell application "System Events" to tell process "iPhone Mirroring" to set frontmost to true' -e 'tell application "System Events" to tell process "iPhone Mirroring" to set visible to true' -e 'delay 1' -e 'tell application "System Events" to keystroke "'"$code"'"' -e 'delay 1' -e 'tell application "System Events" to keystroke "'"$code"'"'; clear; echo "\033[32;1;4mScreen time code successfully saved.\n";
Here's a video of me doing this in action.
How Does This Work?
So what's this code doing? Let's add back in the newlines, swap the osascript
from multiple -e
tags back to a string literal version, and go line by line.
First we seed the RANDOM
number with random data from /dev/urandom
. od -An -N4 -tu4
displays the data in Octal, or Base 8 (od
), strip the line numbers (-An
), takes the first 4 bytes (-N4
) and reads them as one 32-bit number (-tu4
). This ensures that if you run the same command multiple times in the same Terminal window that it will generate different passwords (otherwise zsh will return the same output).
RANDOM=$(od -An -N4 -tu4 < /dev/urandom);
Then we take a random number using RANDOM
, mod it down to 4 digits, and print it with leading 0s to generate the new code. We echo
, or print it, to a file called "screen_time_code.txt", which is where you'll look if you want to see the code you just generated.
code=$(printf "%04d" $((RANDOM % 10000)));
echo "$code" > screen_time_code.txt;
Then we do the real meat of it, which is with osascript
, a MacOS command line for running AppleScript. AppleScript has a lot of useful pieces to it that interact with Mac's operating system, and this tells it to focus the iPhone Mirroring app and enter the newly generated code twice.
osascript << END
tell application "System Events"
tell process "iPhone Mirroring"
set frontmost to true
set visible to true
end tell
delay 1
keystroke "$code"
delay 1
keystroke "$code"
end tell
END
And voila! We've successfully entered the new code, so we clear the Terminal output with clear
and print a message letting the user know that it's done. \033[32;1;4m
just makes the output text green, bold, and underlined (\033[
to start the code, 32
for green, 1
for bold, 4
for underline, and m
to end the code).
clear;
echo "\033[32;1;4mScreen time code successfully saved.\n";