Last active
June 10, 2024 04:23
-
-
Save theoknock/62af86e574a2db3aa83c053f7e507801 to your computer and use it in GitHub Desktop.
Connect iPhone USB Hotspot and Disable Wi-Fi on Startup and Connect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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