This script allows running an adb
command (deeplinks, specifically) dynamically - i.e. ask for which device to send a command to if there are multiple devices found. Companion blog post for usage is 🔗 here.
-
-
Save snijsure/7ebc51c675705d7fe78252a47bc74303 to your computer and use it in GitHub Desktop.
This file contains 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 | |
function get_devices() { | |
all_devices=$(command adb devices) | |
all_devices=${all_devices#"List of devices attached"} | |
# Find how many devices we have | |
num_matches=$(echo $all_devices | egrep -o "([[:alnum:]-]+[[:space:]]+device$)" | wc -l) | |
# If there are multiple, ask for which device to send the command to | |
if [[ $num_matches -gt 1 ]]; then | |
deeplink_with_multiple | |
# Otherwise just send the ADB command | |
else | |
command adb shell am start -W -a android.intent.action.VIEW -d \"$URL\" | |
fi | |
} | |
function deeplink_with_multiple { | |
# Display device serial numbers | |
find_matches=$(echo $all_devices | egrep -io "([[:alnum:]-]+[[:space:]]+device$)" | awk '{print NR " - " $1}') | |
printf "Multiple devices found:\n%s\n" "$find_matches" | |
# Present chooser | |
echo -n "Select device: " | |
read -r selected_device | |
# Send the ADB command with the serial number | |
serial_number=$(echo $find_matches | egrep "${selected_device} - (.*)" | awk '{print $3}') | |
command adb -s $serial_number shell am start -W -a android.intent.action.VIEW -d \"$URL\" | |
} | |
URL=$1 | |
get_devices |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment