martedì 11 ottobre 2022

MacOS - Running Scripts at Login

Issue


In October 2018 I wrote a short post (in Italian) on how to "Remapping Keys in MacOS".
In the post I also wrote that I would publish a second one on how to make this change permanent. For reasons of time (few time available), work, family, etc. I never managed to write it until I forgot about it. Then thanks to Paolo's comment I remembered that I have not posted it anymore. So, I do it now.

Solution


To solve this issue, and make the fix permanent every time I login into my account I decided to use the LaunchAgent features.
More information about LaunchAgent can be found in "Daemons and Services Programming Guide" and in "Script management with launchd in Terminal on Mac" as well.

Let's see below how to run the script when our user logon.
  1. Let's start, creating the folder where to place the script to run. In my case I decide to create a new ".lm_scripts" (hidden folder) under my own directory.
  2. Create a script similar to the followingg, and place it under the new directory (in my case .lm_scrpts).
    For a complete reading on how to find the various parameters, refer to my original post
    #!/bin/bash 
        
     hidutil property --matching '{"ProductID":0x221,"VendorID":0x5ac}' --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000063,"HIDKeyboardModifierMappingDst":0x700000037}]}'
  3. Give the script executable permissions
    chmod +x remapping.keys.sh
  4. Create with your favourite editor a new .plist file. I called it com.remapping.keys.plist
    As shown below
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
        <dict>
            <key>Label</key>
            <string>com.remapping.keys.app</string>
            <key>Program</key>
            <string>/Users/lorenzo/.lm_scripts/remapping.keys.sh</string>
            <key>RunAtLoad</key>
            <true/>
        </dict>
    </plist>
    Note that, the program string above must reflect your user and the path where the script remapping.keys.sh is present.

  5. Then, place the file, in my case com.remapping.keys.plist (just created) under the following directory ~/Library/LaunchAgents/

Now, at your next login the keys of the keyboard keys will be re-mapped as desired.

That's it.

Nessun commento:

Posta un commento