Unusual automation using my Mac Keyboard

This is out there but here goes.

I have a printer located in a closet about 25ft from my desk and I constantly print using letterhead and other tutors of paper.

This means I constantly load printer paper to go back to my laptop to hit the ENTER button to start printing.

Is there a way in HA that would allow me to simulate the pressing of the enter Key on my Mac keyboard? I can use any smart button to initiate this request so I am not concerned with that part.

I looked around the community and did not see any use case like this.

Also I host HA on a VM on a Synology NAS so the keyboard I need access to is not on the host machine.

It won’t be in HA but if you have a spare apple remote control you could do something with Eventscripts. It can fire a script (e.g. one that presses the enter key) on a system event. I use it write a text file which an HA command line sensor reads to know when a Mac is unlocked. It’s only really good for Mac->HA communication, to go the other way like you want to do I think you’d have to use a key on the Apple remote as your trigger.

If you have an SSH connection between your Home Assistant instance and your Mac you can use that to run Terminal commands on the mac. I used this guide from the forums and this guide from siytek.com (He has a couple typos in his guide… but it is pretty good)

Where the siytek.com guide has you create a new script, in Terminal on your Mac you can use something like:

#!/bin/bash

osascript -e 'tell application "**Your Application***" to activate' -e 'tell application "System Events" to key code 36'

The first “tell” isn’t absolutely necessary, it just brings your chosen application to the front. You can leave it off as long as you remember to leave the print dialog box activate on the desktop when you leave to go to the printer. The second “tell” is emulating pressing the Return key.

For me, the Home Assistant shell command configuration syntax that works is as follows:

#configuration.yaml

shell_command:
  mac_do_my_bidding: ssh -i /config/.ssh/id_rsa -o 'StrictHostKeyChecking=no' [email protected] '~/press_my_button'
3 Likes

I was also thinking about this some more, and came up with a less elegant method which doesn’t require SSH.

  1. Turn on your Mac’s internal web server (loads of instructions on Google, I use MAMP for work so I used that).

  2. I use the same shell script to launch a Mac app (or an applescript to press ‘enter’ in OP’s case). This script is named ‘launchapp.sh’

#!/bin/sh
export DYLD_LIBRARY_PATH=""

osascript -e 'tell application "NewBlockParty" to activate'

Save it somewhere, I just put in a folder named mac-scripts in MAMP’s htdocs folder.

  1. Put a tiny PHP script named launchapp.php in the MAMP htdocs folder:
<?php
$start_app = exec('/Applications/MAMP/htdocs/mac-scripts/launchapp.sh');

You may need to give this script permissions (chmod 755).

  1. Now you can use a command line switch in HA to launch your script:
- platform: command_line
  switches:
    new_block_party_playlist:
      friendly_name: New Block Party
      command_on: "curl -sS http://192.168.0.3:8888/mac-scripts/launchapp.php"
1 Like