diff --git a/PNPMacInstaller.sh b/PNPMacInstaller.sh index 5749b9d..2db2585 100755 --- a/PNPMacInstaller.sh +++ b/PNPMacInstaller.sh @@ -72,7 +72,7 @@ if [ ! -e "/usr/local/bin/PlugNPlayMac/bclm" ]; then # echo "Selected Display: $selectedDisplay" newVariable="listDisplayNames=(${selectedDisplay} )" - sed -i '' -e "/^listDisplayNames=/s/.*/$newVariable/" ./PNPMacParam.sh + sed -i '' -e "/^listDisplayNames=/s/.*/$newVariable/" ./PlugNPlayMac/PNPMacParam.sh clear @@ -119,7 +119,7 @@ if [ ! -e "/usr/local/bin/PlugNPlayMac/bclm" ]; then # sed -i '' -e "/^listWifiNames=/s/.*/$newVariable/" ./PNPMacParam.sh newVariable="listWifiNames=(${selectedWifi} )" - sed -i '' -e "/^listWifiNames=/s/.*/$newVariable/" ./PNPMacParam.sh + sed -i '' -e "/^listWifiNames=/s/.*/$newVariable/" ./PlugNPlayMac/PNPMacParam.sh clear @@ -145,7 +145,7 @@ if [ ! -e "/usr/local/bin/PlugNPlayMac/bclm" ]; then fi done - sed -i '' -e "/^listAppToOpen=/s/.*/$selectedApp/" ./PNPMacParam.sh + sed -i '' -e "/^listAppToOpen=/s/.*/$selectedApp/" ./PlugNPlayMac/PNPMacParam.sh clear @@ -176,7 +176,7 @@ if [ ! -e "/usr/local/bin/PlugNPlayMac/bclm" ]; then # echo "Selected Battery Value: $selectedBatteryValue" newVariable="batteryValue=${selectedBatteryValue}" - sed -i '' -e "/^batteryValue=/s/.*/$newVariable/" ./PNPMacParam.sh + sed -i '' -e "/^batteryValue=/s/.*/$newVariable/" ./PlugNPlayMac/PNPMacParam.sh # # Start coping the files @@ -191,19 +191,19 @@ if [ ! -e "/usr/local/bin/PlugNPlayMac/bclm" ]; then fi # Move files to /usr/local/bin/PlugNPlayMac - sudo mv PlugNPlayMac.sh /usr/local/bin/PlugNPlayMac + sudo mv ./PlugNPlayMac/PlugNPlayMac.sh /usr/local/bin/PlugNPlayMac if [ $? -ne 0 ]; then display_error "Error moving PlugNPlayMac.sh" exit 1 fi - sudo mv PNPMacParam.sh /usr/local/bin/PlugNPlayMac + sudo mv ./PlugNPlayMac/PNPMacParam.sh /usr/local/bin/PlugNPlayMac if [ $? -ne 0 ]; then display_error "Error moving PNPMacParam.sh" exit 1 fi - sudo mv bclm /usr/local/bin/PlugNPlayMac + sudo mv ./PlugNPlayMac/bclm /usr/local/bin/PlugNPlayMac if [ $? -ne 0 ]; then display_error "Error moving bclm" exit 1 @@ -232,14 +232,14 @@ if [ ! -e "/usr/local/bin/PlugNPlayMac/com.launch.plug.and.play.mac.plist" ]; th fi # Copy the plist file to /Library/LaunchAgents - sudo cp com.launch.plug.and.play.mac.plist /Library/LaunchAgents + sudo cp ./PlugNPlayMac/com.launch.plug.and.play.mac.plist /Library/LaunchAgents if [ $? -ne 0 ]; then display_error "Error copying com.launch.plug.and.play.mac.plist" exit 1 fi # Move the plist file to /usr/local/bin/PlugNPlayMac - sudo mv com.launch.plug.and.play.mac.plist /usr/local/bin/PlugNPlayMac + sudo mv ./PlugNPlayMac/com.launch.plug.and.play.mac.plist /usr/local/bin/PlugNPlayMac if [ $? -ne 0 ]; then display_error "Error moving com.launch.plug.and.play.mac.plist" exit 1 @@ -259,20 +259,20 @@ if [ ! -e "/usr/local/bin/PlugNPlayMac/com.launch.plug.and.play.mac.plist" ]; th \n" else - - # Define the path to the shell script - shell_script="/usr/local/bin/PlugNPlayMac/PlugNPlayMac.sh" - - # Check if the script exists and is executable - if [ -x "$shell_script" ]; then - # Start the shell script - bash "$shell_script" - else - display_error "The shell script '$shell_script' does not exist or is not executable." - exit 1 - fi - - pkill caffeinate + + # # Define the path to the shell script + # shell_script="/usr/local/bin/PlugNPlayMac/PlugNPlayMac.sh" + + # # Check if the script exists and is executable + # if [ -x "$shell_script" ]; then + # # Start the shell script + # bash "$shell_script" + # else + # display_error "The shell script '$shell_script' does not exist or is not executable." + # exit 1 + # fi + + # pkill caffeinate launchctl load /Library/LaunchAgents/com.launch.plug.and.play.mac.plist echo "Done." diff --git a/PNPMacParam.sh b/PNPMacParam.sh index f797cc8..52405cc 100644 --- a/PNPMacParam.sh +++ b/PNPMacParam.sh @@ -30,8 +30,7 @@ isCaffeinate=false isBclm=false # Apps areAppsOpen=false -# Get the full name of the user -accountUser=$(getUSR) + # ID of the last active caffeinate process (0 at start of the script) PMSETPID=0 # main directory @@ -40,6 +39,17 @@ parentPath=/usr/local/bin/PlugNPlayMac notFoundedApp=() +getUSR() { + # Take the full name of the user + + local my_var + my_var=$(id -F) + echo "$my_var" +} + +# Get the full name of the user +accountUser=$(getUSR) + getPW() { # Take the password from Apple Keychain for operating the sudo operation @@ -54,14 +64,6 @@ getPW() { echo "$my_var" } -getUSR() { - # Take the full name of the user - - local my_var - my_var=$(id -F) - echo "$my_var" -} - detect_cpu_architecture() { architecture=$(uname -m) @@ -79,3 +81,16 @@ detect_cpu_architecture() { echo "$my_var" } + +log_error() { + local msg_type="$1" + local isAppleSilicon="$2" + local msg_text="$3" + local date_string=$(date +"%b %d %Y - %H:%M") + + if [ -n "$isAppleSilicon" ]; then + echo "$date_string: (I$msg_type): $msg_text" + else + echo "$date_string: (A$msg_type): $msg_text" + fi +} \ No newline at end of file diff --git a/PNPMacParam2.sh b/PNPMacParam2.sh deleted file mode 100644 index 1223236..0000000 --- a/PNPMacParam2.sh +++ /dev/null @@ -1,73 +0,0 @@ -# List of Wi-Fi names -listWifiNames=("Vodafone-A45682606" "Other Wifi Name") -# List of Display names -listDisplayNames=("LG IPS FULLHD" "Other Display Name") -# List of Apps to open -listAppToOpen=("MonitorControl" "Elgato Stream Deck" "Other App Name") -# Username of the account to use for sudo operation -accountUser="Andrea Pietrobon" -# Max battery level when connected -batteryValue=77 - - - - - - - - -# security add-generic-password -s 'CLI Test' -a 'armin' -w 'password123' - - - - -########################################################################## -# -# DO NOT MODIFY THE CODE BELOW THIS LINE -# -########################################################################## - -# Delay in seconds (default 60 seconds) -seconds4Delay=60 - -isRunning=false -# Caffeinate -isCaffeinate=false -# BCLM -isBclm=false -# Apps -areAppsOpen=false - -# ID of the last active caffeinate process (0 at start of the script) -PMSETPID=0 -# main directory -parentPath=/usr/local/bin/PlugNPlayMac -# App not founded in the Application folder -notFoundedApp=() - - -getPW() { - # Take the password from apple Keychain for operate the sudo operation - - local my_var - my_var=$(security find-generic-password -w -s "PlugNPlayMac" -a "$accountUser") - echo "$my_var" -} - -detect_cpu_architecture() { - - architecture=$(uname -m) - local my_var - - if [[ "$architecture" == *"arm64"* ]]; then - my_var=true - elif [[ "$architecture" == *"x86_64"* ]]; then - my_var=false - else - date_string=$(date +"%b %d %Y - %H:%M") - echo "$date_string: Problem during the detection of the CPU architecture" - exit 1 - fi - - echo "$my_var" -} diff --git a/PlugNPlayMac.sh b/PlugNPlayMac.sh index 2316d09..b14278f 100755 --- a/PlugNPlayMac.sh +++ b/PlugNPlayMac.sh @@ -5,16 +5,14 @@ # launchctl load /Library/LaunchAgents/com.launch.plug.and.play.mac.plist # launchctl unload /Library/LaunchAgents/com.launch.plug.and.play.mac.plist -# source /usr/local/bin/PlugNPlayMac/PNPMacParam.sh -source /Users/pietrobon/Documents/Developer/GitHub/PlugNPlayMac/personalParam.sh +source /usr/local/bin/PlugNPlayMac/PNPMacParam.sh # Password for usce bclm myPassword=$(getPW) # CPU Architecture (Intel: x86_64 --- Apple Silicon: arm64) isAppleSilicon=$(detect_cpu_architecture) -date_string=$(date +"%b %d %Y - %H:%M") -echo "$date_string: Start the PlugNPlayMac script" +log_error "" "$isAppleSilicon" "Start the PlugNPlayMac script" while true; do @@ -79,8 +77,7 @@ while true; do # save the caffeinate process ID PMSETPID=$! - date_string=$(date +"%b %d %Y - %H:%M") - echo "$date_string: (S1) Starting caffeinate with ID: $PMSETPID" + log_error "S1" "$isAppleSilicon" "Starting caffeinate with ID: $PMSETPID" isRunning=true isCaffeinate=true @@ -88,12 +85,11 @@ while true; do # Open each application in the list for applicationName in "${listAppToOpen[@]}"; do - date_string=$(date +"%b %d %Y - %H:%M") if ! open -a "$applicationName" 2>&1 | grep -q "Unable to find application named '$applicationName'"; then - echo "$date_string: (S1) Starting the APP: $applicationName" + log_error "S2" "$isAppleSilicon" "Starting the APP: $applicationName" else notFoundedApp+=("$applicationName") - echo "$date_string: (E1) Unable to find the APP: $applicationName" + log_error "E2" "$isAppleSilicon" "Unable to find the APP: $applicationName" fi done @@ -104,34 +100,30 @@ while true; do # At this moment the battery doesn't set the new charge limit # on Apple Silicon. This is because the BCLM script doesn't # work on Apple Silicon. - date_string=$(date +"%b %d %Y - %H:%M") - echo "$date_string: (E1) Not done yet for Apple Silicon" + log_error "E3" "$isAppleSilicon" "Not done yet for Apple Silicon" else - date_string=$(date +"%b %d %Y - %H:%M") - # More info on BCLM here: https://github.com/zackelia/bclm # Overwrite battery value and set the new value for the battery limit chmod +x "$parentPath/bclm" - writtenResult=$(echo $myPassword | sudo -S "$parentPath/bclm" write "$batteryValue") + # Prompt for the password and provide it to sudo without displaying it + writtenResult=$(echo "$myPassword" | sudo -S "$parentPath/bclm" write "$batteryValue" 2>&1) if [ $? -eq 0 ]; then - echo "$date_string: (S1) Value $batteryValue written successfully" + log_error "S4" "$isAppleSilicon" "Value $batteryValue written successfully" fi - date_string=$(date +"%b %d %Y - %H:%M") # Apply the persistence for the new battery limit error_message=$(echo "$myPassword" | sudo -S "$parentPath/bclm" persist 2>&1) if [ $? -eq 0 ]; then - echo "" - echo "$date_string: (S1) Persistence has bean activte" + log_error "S5" "$isAppleSilicon" "Persistence has bean activte" fi # Read the current battery value batteryResult="$("$parentPath/bclm" read)" - echo "$date_string: (S1) Result of bclm read: $batteryResult" + log_error "S6" "$isAppleSilicon" "Result of bclm read: $batteryResult" fi if [ "$batteryResult" = "$batteryValue" ]; then @@ -148,11 +140,10 @@ while true; do # Check if any instances were killed if [ $? -eq 0 ]; then - echo "$date_string: (E1) All caffeinate process killed successfully" + log_error "S7" "$isAppleSilicon" "All caffeinate process killed successfully" else - echo "$date_string: (E1) No caffeinate process found to kill" + log_error "E7" "$isAppleSilicon" "No caffeinate process found to kill" fi - isCaffeinate=false fi @@ -167,9 +158,7 @@ while true; do # save the caffeinate process ID PMSETPID=$! - date_string=$(date +"%b %d %Y - %H:%M") - echo "$date_string: (S2) Starting caffeinate with ID: $PMSETPID" - + log_error "S8" "$isAppleSilicon" "Starting caffeinate with ID: $PMSETPID" isCaffeinate=true fi fi @@ -181,13 +170,11 @@ while true; do # Kill all the caffeinate process pkill caffeinate - date_string=$(date +"%b %d %Y - %H:%M") - # Check if any instances were killed if [ $? -eq 0 ]; then - echo "$date_string: (E2) All caffeinate process killed successfully" + log_error "S9" "$isAppleSilicon" "All caffeinate process killed successfully" else - echo "$date_string: (E2) No caffeinate process found to kill" + log_error "E9" "$isAppleSilicon" "No caffeinate process found to kill" fi isRunning=false @@ -197,12 +184,11 @@ while true; do for applicationName in "${listAppToOpen[@]}"; do if [[ "${notFoundedApp[*]}" == *"$applicationName"* ]]; then notFoundedApp=("${notFoundedApp[@]/$applicationName}") - date_string=$(date +"%b %d %Y - %H:%M") echo "$date_string: $applicationName (E1) not found" + log_error "E10" "$isAppleSilicon" "$applicationName not found" else (osascript -e "tell application \"$applicationName\" to quit" & wait) 2>/dev/null - date_string=$(date +"%b %d %Y - %H:%M") - echo "$date_string: $applicationName (E1) closed correctly" + log_error "S10" "$isAppleSilicon" "$applicationName closed correctly" fi done @@ -210,8 +196,7 @@ while true; do batteryResult=0 if $isAppleSilicon; then - date_string=$(date +"%b %d %Y - %H:%M") - echo "$date_string: (E2) Not done yet for Apple Silicon" + log_error "E11" "$isAppleSilicon" "Not done yet for Apple Silicon" else # Remove persistence on the battery for set the default value @@ -219,7 +204,7 @@ while true; do date_string=$(date +"%b %d %Y - %H:%M") if [ $? -eq 0 ]; then - echo "$date_string: (E1) Persistence has bean disabled" + log_error "S12" "$isAppleSilicon" "Persistence has bean disabled" fi # Write the original value @@ -228,16 +213,15 @@ while true; do rmFile=$(echo $myPassword | sudo -S rm /Library/LaunchDaemons/com.launch.plug.and.play.mac.bclm.plist) batteryResult="$("$parentPath/bclm" read)" - echo "$date_string: (S2) Result of bclm read: $batteryResult" + log_error "S13" "$isAppleSilicon" "Result of bclm read: $batteryResult" fi if [ "$batteryResult" = "100" ]; then isBclm=false fi - fi fi - + sleep $seconds4Delay done \ No newline at end of file diff --git a/README.md b/README.md index aee33e4..b9553f1 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@


- +chmod +x /Users/pietrobon/PlugNPlayMac/PNPMacInstaller.sh

@@ -142,7 +142,7 @@


⚙️ How to Start

- Below is an example of how you can instruct your audience on installing and setting up your app. This template doesn't rely on any external dependencies or services. + Here you can find a step by step guide to install and run the script. At the end of this section you can find a link to a Q&A page that provide some extra information like change the password, how to add more displays, download the shortcut, etc.


@@ -154,47 +154,25 @@ 2. Open the terminal and run the installer: ```sh - /Users/YOUR-NAME/Downloads/PlugNPlayMac-main/PNPMacInstaller.sh + ./PlugNPlayMac/PNPMacInstaller.sh ``` - > Where `YOUR-NAME` is the name of your user account on your Mac. -3. Give the script the Full Disk Access (Mandatory for run it correctly): - - Open System Settings > Privacy & Security > Full Disk Access +3. Give the script the Full Disk Access (Mandatory for run it correctly): + - Open `System Settings > Privacy & Security > Full Disk Access` - Add `/bin/bash` and `/usr/local/bin/PlugNPlayMac/blcm` - - This is the final result: - - + > **A step by step guide can be found here.** 4. Reboot your Mac 5. Open the terminal and run again the installer: ```sh - /Users/YOUR-NAME/Downloads/PlugNPlayMac-main/PNPMacInstaller.sh + ./PlugNPlayMac/PNPMacInstaller.sh ``` - > Where `YOUR-NAME` is the name of your user account on your Mac. - -***Done!*** Now the script will run automatically when you connect your Mac to the monitor and will close when you disconnect it. Additionally, you can use the shortcut to enable/disable various functions from the Mac's menu bar. - -👉 You can get the PlagNPlayMac shortcut from this link or you can create it manually. - -
-If you want to stop the automation of the script, you can run the following command on the terminal: - -
- -```sh -launchctl unload /Library/LaunchAgents/com.launch.plug.and.play.mac.plist -``` -
-for restart it: +**Done!** Now the script will run automatically when you connect your Mac to the monitor and will close when you disconnect it. Additionally, you can use the shortcut to enable/disable various functions from the Mac's menu bar. -```sh -launchctl load /Library/LaunchAgents/com.launch.plug.and.play.mac.plist -``` +### 👉 Here you can find the Q&A page with some extra information. -

@@ -207,6 +185,7 @@ launchctl load /Library/LaunchAgents/com.launch.plug.and.play.mac.plist - [x] Switch from AlDente to bclm - [x] Bug Fixing - [x] Reduce the procedure for the installation +- [ ] Add automation based on time for the bclm - [ ] Add AutoUpdate - [ ] Add bclm support for Apple Silicon (whenever I have access to a Mac with an Apple Silicon processor) - [ ] Switch to Swift diff --git a/markdown/QandA.md b/markdown/QandA.md new file mode 100644 index 0000000..5e85c1b --- /dev/null +++ b/markdown/QandA.md @@ -0,0 +1,109 @@ +# Q&A + +
+1) Where can I find the Shortcut for control the script and what can I do with it ? + +
+ +> 👉 You can get the PlagNPlayMac shortcut from this link or you can create it manually. + +
+
+
+ +
+2) How can I start and stop the automation of the script ? +
+ +You can easilly stop the automation of the script by running the following command on the terminal: +```sh + launchctl unload /Library/LaunchAgents/com.launch.plug.and.play.mac.plist +``` + +for restart it: + +```sh +launchctl load /Library/LaunchAgents/com.launch.plug.and.play.mac.plist +``` +
+
+
+ +
+3) Accidentally I have insert the wrong password how can I change it? +
+ +> If you have insert the wrong password and you want to change it + +
+
+
+ +
+4) How can I add more displays to the script ? I need to run the script when I am home and when I am at work +
+ +> For add more displays to the script here + +
+
+
+ +
+5) How can I change the battery level ? +
+ +1. Open the `PNPMacParam.sh` file and edit the following variables: +2. Change the value of batteryValue with the one in range 50-95 (100 only if you don't want to have the battery limit) +3. Save exit and restart the script with the command: +```sh +launchctl unload /Library/LaunchAgents/com.launch.plug.and.play.mac.plist +launchctl load /Library/LaunchAgents/com.launch.plug.and.play.mac.plist +``` + +**NOTE:** The `batteryValue` of bclm as mentioned it will be 3 point greather then the value setted in the script. So if you set `batteryValue=77` the battery limit will be 80. + +
+
+
+ +
+ +6) Somthing don't work what I have to do ? +
+ +If somthing you can try to find a solution by checking the log file `/private/tmp/plug.and.play.mac.log` + +If the problem is not correlated to a wrong password or a wrong name of the display you can open an issue here and post the result of the log file. + +
+
+
+ +
+7) How can I completelly erease the script and all the file correlated ? + +### You Can't. +> Just kidding 😂 of corse you can delete it. + +You can do it by deleting the following files: +- `/Library/LaunchAgents/com.launch.plug.and.play.mac.plist` + +- `/usr/local/bin/PlugNPlayMac` (DELETE THE ENTIRE FOLDER) + +- Remember to remove bash and bclm from `System Settings > Privacy & Security > Full Disk Access` + +
+
+
+ + + diff --git a/markdown/addDisplays.md b/markdown/addDisplays.md new file mode 100644 index 0000000..94e5610 --- /dev/null +++ b/markdown/addDisplays.md @@ -0,0 +1,49 @@ +# Add more displays to the script + +
+ +By adding more displays to the script you will be able to automate this task also when you plag other monitors to your Mac. If when you plug the second monitor you are under the same wifi network con can just add the display name as is explaned below and skip the part of the wifi name. If you are not under the same wifi network you have to add also the wifi name as is explaned below. + +## Add displays +1. Connect the macbook to your monitor open the terminal and run the following commands: + - ```sh + ioreg -lw0 | grep 'IODisplayEDID' | sed '/[^<]* - - ```sh - /Sy*/L*/Priv*/Apple8*/V*/C*/R*/airport -I | awk '/ SSID:/ {print $2}' - ``` - It will return the name of the wifi seen by the Mac. In my case, it is `Home-Wifi-Name`. - -3. Open the `PNPMacParam.sh` file and edit the following variables: - - ```sh - listWifiNames=("Wifi Name" "Other Wifi Name") - ``` - - ```sh - listDisplayNames=("Display Name" "Other Display Name") - ``` - - ```sh - listAppToOpen=("App Name" "Other App Name") - ``` - - ```sh - batteryValue=77 - ``` - - ```sh - accountUser="User Name" - ``` - Where `listWifiNames` is the list of Wi-Fi networks and `listDisplayNames` are the list of monitors that trigger the script, `listAppToOpen` is the list of apps to open, `batteryValue` is the battery limit for `bclm` (must be less or equal to 100), and `accountUser` is the name of the user account on your Mac. So in my case, the values are: - ```sh - listWifiNames=("Home-Wifi-Name") - listDisplayNames=("LG IPS FULLHD") - listAppToOpen=("MonitorControl" "Elgato Stream Deck") - batteryValue=77 - accountUser="MY USERNAME" - ``` - Where `Home-Wifi-Name`and `LG IPS FULLHD` are the values founded before, `MonitorControl` and `Elgato Stream Deck` are the apps to open, `77` is the battery limit for `bclm`, and `MY USERNAME` is the name of the user account on my Mac. You can add as mach wifi, monitor, and app you want. - - - **NOTE:** The `batteryValue` of bclm as mentioned it will be 3 point greather then the value setted in the script. So if you set `batteryValue=77` the battery limit will be 80. - - - - -3. Give the script the Full Disk Access (Mandatory for run it correctly): - - Open System Settings > Privacy & Security > Full Disk Access - - Add /bin/bash and /usr/local/bin/PlugNPlayMac/blcm to the list - - Click on the plus icon at the bottom of the list and digit your password - - - - - Press cmd + shift + G and digit /bin - - - - - Select bash and click open - - Click again on the plus icon at the bottom of the list and digit your password - - Press cmd + shift + G and digit /usr/local/bin/PlugNPlayMac - - - - - Select blcm and click open - - - This is the final result: - - \ No newline at end of file diff --git a/markdown/fullDiskAccess.md b/markdown/fullDiskAccess.md new file mode 100644 index 0000000..db5e0b7 --- /dev/null +++ b/markdown/fullDiskAccess.md @@ -0,0 +1,34 @@ +# Give the script the Full Disk Access + +For give the Full Disk Access (Mandatory for run it correctly) to the script you have to: + +- Open `System Settings > Privacy & Security > Full Disk Access` + +- Click on the ` + ` icon at the bottom of the list and digit your password + + + +- Press `cmd + shift + G ` and digit `/bin` + + + +- Select `bash` and click open +- Click again on the ` + ` icon at the bottom of the list and digit your password +- Press `cmd + shift + G ` and digit `/usr/local/bin/PlugNPlayMac` + + + +- Select `blcm` and click open + +
+
+
+ + + + + +### This is the final result: + +
+ \ No newline at end of file