Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save theoknock/62af86e574a2db3aa83c053f7e507801 to your computer and use it in GitHub Desktop.
Save theoknock/62af86e574a2db3aa83c053f7e507801 to your computer and use it in GitHub Desktop.
Connect iPhone USB Hotspot and Disable Wi-Fi on Startup and Connect
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.user.connecthotspot</string>
<key>ProgramArguments</key>
<array>
<string>/Users/YourUsername/scripts/connect_hotspot.zsh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WatchPaths</key>
<array>
<string>/dev</string>
</array>
</dict>
</plist>
sudo pico ~/scripts/connect_hotspot.applescript
sudo pico ~/scripts/connect_hotspot.zsh
chmod +x ~/scripts / connect_hotspot.zsh
sudo pico ~/Library/LaunchAgents/com.user.connecthotspot.plist
networksetup -listallnetworkservices
tail -f ~/connect_hotspot.log
launchctl unload ~/Library / LaunchAgents / com.user.connecthotspot.plist
launchctl load ~/Library / LaunchAgents / com.user.connecthotspot.plist
-- AppleScript to connect to iPhone USB hotspot
tell application "System Events"
-- Log start message
do shell script "echo 'Starting AppleScript to connect to iPhone USB hotspot' >> ~/connect_hotspot.log"
tell current location of network preferences
set serviceList to the name of every service
if "iPhone USB USB" is in serviceList then
set myService to service "iPhone USB USB"
if current configuration of myService is not connected then
connect myService
-- Log connection attempt
do shell script "echo 'Attempted to connect to iPhone USB hotspot' >> ~/connect_hotspot.log"
else
-- Log already connected
do shell script "echo 'iPhone USB hotspot already connected' >> ~/connect_hotspot.log"
end if
else
-- Log service not found
do shell script "echo 'iPhone USB USB service not found' >> ~/connect_hotspot.log"
end if
end tell
end tell
-- Log end message
do shell script "echo 'Finished AppleScript' >> ~/connect_hotspot.log"
#!/bin/zsh
# Log start message
echo "Starting connect_hotspot.zsh script" >> ~/connect_hotspot.log
# Run the AppleScript to connect to the iPhone USB hotspot
osascript ~/scripts/connect_hotspot.applescript
# Check if the iPhone USB hotspot is connected
hotspot_status=$(networksetup -getinfo "iPhone USB USB" | grep "IP address")
if [[ -n "$hotspot_status" ]]; then
echo "iPhone USB hotspot connected. Turning off Wi-Fi..." >> ~/connect_hotspot.log
networksetup -setairportpower airport off
echo "Wi-Fi turned off" >> ~/connect_hotspot.log
else
echo "iPhone USB hotspot not connected." >> ~/connect_hotspot.log
fi
# Log end message
echo "Finished connect_hotspot.zsh script" >> ~/connect_hotspot.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment