2

I am new to using Automator. I'm trying to make my desktop wallpaper change according to my screen brightness which is automatically adjusting to the light in my room (basically an automatic bright/dark mode for the desktop).

Is there anything like Folder Action that is triggered by a custom event instead of by adding files to a folder? I need it to trigger when the screen brightness changes, and then depending on the brightness decide if the wallpaper needs to be changed.

What I have so far

The following AppleScript does everything I need:

set brightness to do shell script "nvram backlight-level | awk '{print $2}'"
if brightness is equal to "8%00" or brightness is equal to "%16%00" or brightness is equal to "%25%00" or brightness is equal to "%00%00" then
    setWallpaper("dark")
else
    setWallpaper("bright")
end if

on setWallpaper(imageName)
    tell application "System Events"
        tell every desktop
            set picture to "/Users/Ryn/Desktop/wallpapers/" & imageName & ".png"
        end tell
    end tell
end setWallpaper

The only thing left is to figure out how to run it every time the screen brightness changes.

1 Answer 1

0

This works for me using the latest version of macOS Mojave.

You can use Automator but is not necessary for this situation. Paste this following AppleScript code directly into Script Editor app, then save it as a "stay open application" in Script Editor. Now all you need to do is launch your new app (which stays opened until you actually choose to quit it) and every 180 seconds (3 minutes), your shell script command will run. The 180 seconds value can be changed in the code to whatever you desire.

checkBrightness() -- runs once on opening this app then the idle handler takes over

on idle
    checkBrightness()
    return 180 -- in seconds (runs the shell script command every 3 min.)
end idle

on checkBrightness()
    set brightness to do shell script "nvram backlight-level | awk '{print $2}'"
    if brightness is equal to "8%00" or brightness is equal to "%16%00" or brightness is equal to "%25%00" or brightness is equal to "%00%00" then
        setWallpaper("dark")
    else
        setWallpaper("bright")
    end if
end checkBrightness

on setWallpaper(imageName)
    tell application "System Events"
        --tell every desktop (couldnt get this to work)
        tell current desktop
            set picture to "/Users/Ryn/Desktop/wallpapers/" & imageName & ".png"
        end tell
    end tell
end setWallpaper

There is another option if you don't want this application to continuously run in the background. For example, if you want this app to only run for 4 hours, you can use this following idle handler instead.

on idle
    repeat 16 times
        delay (15 * minutes) --(waits to run the shell script command every 15 min.)
        checkBrightness()
    end repeat
end idle

The only disadvantage to using this idle handler is that the only way to quit the app while it is running, would be to "Force Quit" the app because the normal "Quit" command will not work.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .