I needed a quick way to remotely perform system operations: adjusting & muting the system volume, screen brightness, and putting the display to sleep. After exploring several options, I found none of them to be viable for me, and thus, I set out to develop my own solution.

My requirements were simple, but if I wanted this to be truly remote and accessible anywhere, my only options are the Web or a bot I can send commands to. I decided on the latter because it’s easier to secure than a web app, and I decided on Telegram because it’s widely available and probably the most straightforward.

The full code is available on Github. If you run into any troubles with the steps below, look to the repository as a reference.

First things first,

We’ll need to create a Telegram bot by speaking to @BotFather. Follow the on-screen instructions: name your bot and choose a username for it, and at the end of the steps, you will be given an API access token. We’ll use this token to interact with Telegram’s API.

Tip: If you don’t want your bot to be discovered, choose a random username or prepend some random characters to it.

Next, create a new Python project and install python-telegram-bot:

$ mkdir tg-mac-remote && cd tg-mac-remote

$ virtualenv .venv

$ source .venv/bin/activate

$ pip install python-telegram-bot

$ touch bot.py

Open bot.py in your editor of choice, and we’re ready to get started. Let’s check and see if our bot is working by having it respond to the “/hello” command with our Telegram first name and user ID:

bot.py, “/hello” command

Note: If you’re unsure how python-telegram-bot works, check out the “Getting started” section of their Github repo.

Run python bot.py and hit your bot up on Telegram with “/hello”. It should greet you with your Telegram first name and UID. Great! Now our bot is alive and responding. Make a note of your Telegram UID, we’ll be using this to ensure the bot only responds to our commands.

Before we proceed further…

Let’s take a step back and think about how we can programatically adjust the system volume, display brightness, and turn the display off. How do we do all of that programatically, and with Python?

PyObjC will be the first answer people usually jump to, but there’s an easier way: command line utilities. Instead of trying to manipulate the system natively through PyObjC, we will be calling several command line utilities with Python’s subprocess .

osascript, brightness, and pmset

We will use osascript to execute AppleScript. Turns out, there’s a ton of stuff we can accomplish with AppleScript. This article (links to osxdaily.com) gives a nice round-up of what we can do. To set the system volume, we can run:

$ osascript -e "set Volume n"

in a Terminal window to set the system volume to n . Handy, isn’t it? I’d think everything we want to accomplish can be done through AppleScript one way or another, and that is true to a certain extent, but most of that involves opening other applications and interacting with the UI through AppleScript. This is rather slow, so we’ll use other CLI utilities more suitable to the operation we wish to perform.

To turn off the display immediately, we can use $ pmset displaysleepnow . To adjust screen brightness, we’re going to install brightness and verify that it works:

$ brew install brightness

$ brightness 0.3 # set display brightness to 30%

Now that we have everything thought through, let’s write wrappers for the above utilities.