net.sh
 1  #!/usr/bin/env sh
 2  #
 3  
 4  # Full network name
 5  #LABEL="$(networksetup -listallhardwareports | awk '/Wi-Fi/{getline; print $2}' | xargs networksetup -getairportnetwork | sed "s/Current Wi-Fi Network: //")"
 6  
 7  # If first word is longer than 10 characters, print first 7 characters
 8  # If first word is shorter than 10 characters, print first word and first 2 characters of second word if any
 9  # LABEL="$(networksetup -listallhardwareports | awk '/Wi-Fi/{getline; print $2}' | xargs networksetup -getairportnetwork | sed "s/Current Wi-Fi Network: //" | 
10  #   awk '{ 
11  #     if ($0 ~ /You are not associated with an AirPort network/) {
12  #       print "N/A";
13  #       exit;
14  #     }
15  #     else if (length($1) > 10) { 
16  #       label=substr($1, 1, 7); 
17  #       gsub(/ $/, "", label); 
18  #       print label "..."; 
19  #     } 
20  #     else {
21  #       print $1; 
22  #       if (NF > 1) { 
23  #         print(" ");
24  #         printf substr($2, 1, 2) "..."; 
25  #       } 
26  #     } 
27  #   }')"
28  #
29  # Get Wi-Fi network information
30  WI_FI_INFO=$(networksetup -listallhardwareports | awk '/Wi-Fi/{getline; print $2}')
31  
32  # Get the current Wi-Fi network
33  #CURRENT_NETWORK=$(echo "$WI_FI_INFO" | xargs networksetup -getairportnetwork | sed "s/Current Wi-Fi Network: //")
34  
35  CURRENT_NETWORK=$(echo "$WI_FI_INFO" | ipconfig getsummary en0 | awk -F ' SSID : '  '/ SSID : / {print $2}')
36  
37  # Check if the current network is associated with an airport network
38  if echo "$CURRENT_NETWORK" | grep -q "You are not associated with an AirPort network"; then
39      LABEL="N/A"
40      sketchybar --set net_logo background.color=0xff3C3E4F --set net label.color=0xff1e1d2e
41  else
42      sketchybar --set net_logo background.color=0xffE0A3AD --set net label.color=0xffECEFF4 
43      # Truncate the current network name if longer than 10 characters
44      if [ "$(echo "$CURRENT_NETWORK" | awk '{ print length($1) }')" -gt 10 ]; then
45          label=$(echo "$CURRENT_NETWORK" | awk '{ print substr($0, 1, 7) }')
46          label=$(echo "$label" | sed 's/ *$//')
47          LABEL="$label"...
48      else
49          LABEL=$(echo "$CURRENT_NETWORK" | awk '{ printf "%s", $1 }')
50          # If there is a second word, print the first two characters followed by ...
51          if [ "$(echo "$CURRENT_NETWORK" | awk '{ print NF }')" -gt 1 ]; then
52              SECOND_WORD=$(echo "$CURRENT_NETWORK" | awk '{ printf "%s", substr($2, 1, 2) }')
53              LABEL="$LABEL $SECOND_WORD..."
54          fi
55      fi
56  fi
57  
58  sketchybar --set "$NAME" label="$LABEL"