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 @@
@@ -142,7 +142,7 @@
- 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.