Last active
March 10, 2026 03:37
-
-
Save megabitsenmzq/a8fea30c895dc259d7419843ceb72a13 to your computer and use it in GitHub Desktop.
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/bash | |
| # Get the latest verification code from emails across all accounts | |
| # Searches only inbox and junk mailboxes in all accounts for emails received today | |
| # Verification code format: 認証コードは XXXXXX です | |
| get_latest_verification_code() { | |
| local quiet_mode="${1:-false}" | |
| # Output debug info to stderr instead of stdout (skip in quiet mode) | |
| if [ "$quiet_mode" != "true" ]; then | |
| echo "Connecting to Mail app..." >&2 | |
| fi | |
| # Use AppleScript to query Mail app | |
| local verification_code=$(osascript << 'EOF' | |
| tell application "Mail" | |
| # Initialize variables - use today's date as starting point | |
| set todayDate to current date | |
| set todayStart to (todayDate - (time of todayDate)) # Start of today (00:00:00) | |
| set latestDate to todayStart | |
| set latestCode to "" | |
| set allMessages to {} | |
| # Collect messages from all accounts, only inbox and junk folders with date filter (only today's messages) | |
| set allAccounts to every account | |
| repeat with eachAccount in allAccounts | |
| try | |
| # Only search inbox and junk mailboxes | |
| set accountMailboxes to {} | |
| try | |
| set inboxMailbox to mailbox "INBOX" of eachAccount | |
| set end of accountMailboxes to inboxMailbox | |
| end try | |
| try | |
| set junkMailbox to mailbox "Junk" of eachAccount | |
| set end of accountMailboxes to junkMailbox | |
| end try | |
| repeat with eachMailbox in accountMailboxes | |
| try | |
| # Get messages received today or later, limited to 10 per folder for performance | |
| set todayMessages to (messages of eachMailbox whose date received ≥ todayStart) | |
| set messageCount to count of todayMessages | |
| if messageCount > 0 then | |
| # Limit to latest 10 messages from today for performance | |
| set messagesToGet to 10 | |
| if messageCount < 10 then | |
| set messagesToGet to messageCount | |
| end if | |
| set folderMessages to items 1 thru messagesToGet of todayMessages | |
| set allMessages to allMessages & folderMessages | |
| end if | |
| on error | |
| -- Skip this mailbox if there's an error | |
| end try | |
| end repeat | |
| on error | |
| -- Skip this account if there's an error | |
| end try | |
| end repeat | |
| # Iterate through all collected emails from all folders | |
| repeat with eachMessage in allMessages | |
| try | |
| set messageSubject to subject of eachMessage | |
| set messageDate to date received of eachMessage | |
| # Check recipient email address (only consider first recipient) | |
| set recipientEmail to "" | |
| try | |
| set recipientsList to to recipients of eachMessage | |
| if (count of recipientsList) > 0 then | |
| set recipientEmail to address of (item 1 of recipientsList) | |
| end if | |
| end try | |
| # Check if email subject contains verification code format | |
| if messageSubject contains "認証コードは" and messageSubject contains "です" and recipientEmail contains "mobile+ios" then | |
| # If this email is newer than the current latest email | |
| if messageDate > latestDate then | |
| set latestDate to messageDate | |
| # Extract verification code (6 digits) | |
| set oldDelims to (get text item delimiters of AppleScript) | |
| set text item delimiters of AppleScript to "認証コードは " | |
| set temp to text items of messageSubject | |
| if (count of temp) > 1 then | |
| set text item delimiters of AppleScript to " です" | |
| set codeText to text items of (item 2 of temp) | |
| if (count of codeText) > 0 then | |
| set potentialCode to item 1 of codeText | |
| # Verify if it's 6 digits (fix issue with codes starting with 0) | |
| if length of potentialCode is 6 then | |
| try | |
| set codeNum to potentialCode as number | |
| set latestCode to potentialCode | |
| on error | |
| # If conversion fails, check if all are digits | |
| set isAllDigits to true | |
| repeat with i from 1 to length of potentialCode | |
| set currentChar to character i of potentialCode | |
| if currentChar < "0" or currentChar > "9" then | |
| set isAllDigits to false | |
| exit repeat | |
| end if | |
| end repeat | |
| if isAllDigits then | |
| set latestCode to potentialCode | |
| end if | |
| end try | |
| end if | |
| end if | |
| end if | |
| set text item delimiters of AppleScript to oldDelims | |
| end if | |
| end if | |
| on error | |
| -- Ignore errors, continue with next email | |
| end try | |
| end repeat | |
| return latestCode | |
| end tell | |
| EOF | |
| ) | |
| # Check AppleScript execution result | |
| local exit_code=$? | |
| if [ $exit_code -ne 0 ]; then | |
| echo "Error: AppleScript execution failed, exit code: $exit_code" >&2 | |
| echo "Possible cause: No permission to access Mail app, or Mail app not running properly" >&2 | |
| echo "Please go to System Preferences > Security & Privacy > Privacy > Automation to authorize Terminal access to Mail" >&2 | |
| return 1 | |
| fi | |
| echo "$verification_code" | |
| } | |
| # Main function | |
| main() { | |
| echo "Getting latest verification code..." | |
| # Check if Mail app is running | |
| if ! pgrep -f "Mail" > /dev/null; then | |
| echo "Error: Mail app is not running, please open Mail app first" | |
| exit 1 | |
| fi | |
| # Get verification code | |
| local code=$(get_latest_verification_code false) | |
| if [ -n "$code" ]; then | |
| echo "Found latest verification code: $code" | |
| return 0 | |
| else | |
| echo "No verification code email found" | |
| return 1 | |
| fi | |
| } | |
| # Show help information | |
| show_help() { | |
| echo "Usage: $0 [options]" | |
| echo "" | |
| echo "Options:" | |
| echo " -h, --help Show this help information" | |
| echo " -q, --quiet Quiet mode, only output verification code" | |
| echo "" | |
| echo "Description:" | |
| echo " Get the latest verification code email from Mail app" | |
| echo " Searches only inbox and junk folders across all accounts for emails received today" | |
| echo " Verification code format: 認証コードは XXXXXX です" | |
| echo "" | |
| } | |
| # Quiet mode | |
| # Exit codes: | |
| # 0: Success (code found) | |
| # 1: Mail app not running (error) | |
| # 2: No verification code found (not an error, just no email) | |
| quiet_mode() { | |
| if ! pgrep -f "Mail" > /dev/null; then | |
| echo "Error: Mail app is not running, please open Mail app first" >&2 | |
| exit 1 | |
| fi | |
| local code | |
| code=$(get_latest_verification_code true) | |
| local result=$? | |
| if [ $result -ne 0 ]; then | |
| # AppleScript execution failed | |
| exit 1 | |
| fi | |
| if [ -n "$code" ]; then | |
| echo "$code" | |
| exit 0 | |
| else | |
| exit 2 | |
| fi | |
| } | |
| # Parse command line arguments | |
| case "${1:-}" in | |
| -h|--help) | |
| show_help | |
| exit 0 | |
| ;; | |
| -q|--quiet) | |
| quiet_mode | |
| ;; | |
| "") | |
| main | |
| ;; | |
| *) | |
| echo "Unknown option: $1" | |
| echo "Use -h or --help to see help information" | |
| exit 1 | |
| ;; | |
| esac |
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/bash | |
| # Verification code monitoring script for Android | |
| # Monitor new verification codes and automatically input to Android emulator | |
| # Color output | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| BLUE='\033[0;34m' | |
| NC='\033[0m' # No Color | |
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
| ADB_PATH="$HOME/Library/Android/sdk/platform-tools/adb" | |
| CHECK_INTERVAL=5 | |
| CURRENT_CODE="" | |
| RUNNING=true | |
| DEVICE_ID="" | |
| DEVICE_NAME="" | |
| show_help() { | |
| echo "Usage: $0 [options]" | |
| echo "" | |
| echo "Options:" | |
| echo " -h, --help Show this help information" | |
| echo "" | |
| echo "Description:" | |
| echo " Continuously monitor new verification codes in Mail app, automatically input to first running Android emulator" | |
| echo " Check interval: 5 seconds" | |
| echo " Use Ctrl+C to stop monitoring" | |
| echo "" | |
| echo "Example:" | |
| echo " $0 # Monitor using first running emulator" | |
| echo "" | |
| } | |
| log_info() { | |
| echo -e "${BLUE}[$(date '+%H:%M:%S')] INFO:${NC} $1" >&2 | |
| } | |
| log_success() { | |
| echo -e "${GREEN}[$(date '+%H:%M:%S')] SUCCESS:${NC} $1" >&2 | |
| } | |
| log_warning() { | |
| echo -e "${YELLOW}[$(date '+%H:%M:%S')] WARNING:${NC} $1" >&2 | |
| } | |
| log_error() { | |
| echo -e "${RED}[$(date '+%H:%M:%S')] ERROR:${NC} $1" >&2 | |
| } | |
| cleanup() { | |
| RUNNING=false | |
| log_info "Received stop signal, exiting..." | |
| exit 0 | |
| } | |
| trap cleanup SIGINT SIGTERM | |
| check_dependencies() { | |
| if [ ! -f "$ADB_PATH" ]; then | |
| log_error "adb tool not found at $ADB_PATH, please check Android SDK installation" | |
| exit 1 | |
| fi | |
| } | |
| # Get verification code | |
| # Return codes: | |
| # 0: Success (code found) | |
| # 1: Failed to get verification code (Mail app issue) | |
| # 2: No verification code found (Mail app working, but no email) | |
| get_verification_code() { | |
| local output | |
| local exit_code | |
| output=$( { "$SCRIPT_DIR/get_verification_code.sh" -q; } 2>&1 ) | |
| exit_code=$? | |
| if [ $exit_code -eq 0 ]; then | |
| # Success - code found | |
| echo "$output" | |
| return 0 | |
| elif [ $exit_code -eq 2 ]; then | |
| # No verification code found (not an error) | |
| return 2 | |
| else | |
| # Mail app not running or other errors | |
| echo "$output" | |
| return 1 | |
| fi | |
| } | |
| get_first_running_emulator() { | |
| local device_list=$("$ADB_PATH" devices -l | tail -n +2 | grep -v "^$" | grep "device ") | |
| if [ -z "$device_list" ]; then | |
| log_error "No running emulator found" | |
| return 1 | |
| fi | |
| local device_id=$(echo "$device_list" | head -n1 | awk '{print $1}') | |
| local device_name=$(echo "$device_list" | head -n1 | sed -E 's/.*model:([^ ]+).*/\1/' 2>/dev/null || echo "Unknown") | |
| # Fallback: if model extraction failed, try to get device name from model field | |
| if [ "$device_name" = "Unknown" ] || [ -z "$device_name" ]; then | |
| device_name=$(echo "$device_list" | head -n1 | awk '{for(i=1;i<=NF;i++){if($i~/model:/){print $i}}}' | cut -d':' -f2) | |
| if [ -z "$device_name" ]; then | |
| device_name="$device_id" | |
| fi | |
| fi | |
| log_success "Found running emulator: $device_name (ID: $device_id)" | |
| echo "$device_id|$device_name" | |
| return 0 | |
| } | |
| input_to_emulator() { | |
| local code="$1" | |
| local device_id="$2" | |
| local output | |
| local exit_code | |
| output=$("$ADB_PATH" -s "$device_id" shell input text "$code" 2>&1) | |
| exit_code=$? | |
| if [ $exit_code -eq 0 ]; then | |
| return 0 | |
| else | |
| log_error "adb input command failed with exit code $exit_code" | |
| if [ -n "$output" ]; then | |
| log_error "Error output: $output" | |
| fi | |
| return 1 | |
| fi | |
| } | |
| monitor_verification_codes() { | |
| log_info "Starting verification code monitoring..." | |
| log_info "Device: $DEVICE_NAME (ID: $DEVICE_ID)" | |
| log_info "Check interval: ${CHECK_INTERVAL} seconds" | |
| log_info "Press Ctrl+C to stop monitoring" | |
| echo "" | |
| log_info "Getting current verification code as baseline..." | |
| CURRENT_CODE=$(get_verification_code) | |
| local result=$? | |
| if [ "$result" -eq 0 ]; then | |
| log_info "Current verification code: $CURRENT_CODE" | |
| elif [ "$result" -eq 2 ]; then | |
| log_warning "No verification code found yet" | |
| CURRENT_CODE="" | |
| else | |
| log_warning "Failed to get verification code, using empty value as baseline" | |
| if [ -n "$CURRENT_CODE" ]; then | |
| log_warning "Error output: $CURRENT_CODE" | |
| fi | |
| CURRENT_CODE="" | |
| fi | |
| echo "" | |
| local check_count=0 | |
| while [ "$RUNNING" = true ]; do | |
| check_count=$((check_count + 1)) | |
| log_info "Check #$check_count..." | |
| local new_code | |
| new_code=$(get_verification_code) | |
| local result=$? | |
| if [ "$result" -eq 0 ]; then | |
| if [ "$new_code" != "$CURRENT_CODE" ]; then | |
| log_success "Found new verification code: $new_code (old: ${CURRENT_CODE:-none})" | |
| log_info "Inputting verification code to emulator..." | |
| if input_to_emulator "$new_code" "$DEVICE_ID"; then | |
| log_success "Verification code successfully input to emulator" | |
| if command -v pbcopy > /dev/null; then | |
| echo -n "$new_code" | pbcopy | |
| log_info "Verification code copied to clipboard" | |
| fi | |
| else | |
| log_error "Failed to input verification code to emulator" | |
| fi | |
| CURRENT_CODE="$new_code" | |
| echo "" | |
| else | |
| log_info "Verification code unchanged: $new_code" | |
| fi | |
| elif [ "$result" -eq 2 ]; then | |
| log_warning "No verification code found yet" | |
| else | |
| log_error "Failed to get verification code" | |
| if [ -n "$new_code" ]; then | |
| log_error "Error output: $new_code" | |
| fi | |
| fi | |
| sleep "$CHECK_INTERVAL" | |
| done | |
| } | |
| main() { | |
| while [[ $# -gt 0 ]]; do | |
| case $1 in | |
| -h|--help) | |
| show_help | |
| exit 0 | |
| ;; | |
| *) | |
| log_error "Unknown option: $1" | |
| echo "Use -h or --help to see help information" | |
| exit 1 | |
| ;; | |
| esac | |
| done | |
| check_dependencies | |
| log_info "Looking for first running emulator..." | |
| local result | |
| result=$(get_first_running_emulator) | |
| local exit_code=$? | |
| if [ $exit_code -ne 0 ]; then | |
| echo "" | |
| echo "Available devices:" | |
| "$ADB_PATH" devices -l | |
| exit 1 | |
| fi | |
| DEVICE_ID=$(echo "$result" | cut -d'|' -f1) | |
| DEVICE_NAME=$(echo "$result" | cut -d'|' -f2) | |
| monitor_verification_codes | |
| } | |
| if [ "${BASH_SOURCE[0]}" == "${0}" ]; then | |
| main "$@" | |
| fi |
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/bash | |
| # Verification code monitoring script | |
| # Monitor new verification codes and automatically input to iOS simulator | |
| # Color output | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| BLUE='\033[0;34m' | |
| NC='\033[0m' # No Color | |
| # Get script directory | |
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
| # Global variables | |
| CHECK_INTERVAL=5 | |
| CURRENT_CODE="" | |
| RUNNING=true | |
| UDID="" | |
| DEVICE_NAME="" | |
| # Show help information | |
| show_help() { | |
| echo "Usage: $0 [options]" | |
| echo "" | |
| echo "Options:" | |
| echo " -h, --help Show this help information" | |
| echo "" | |
| echo "Description:" | |
| echo " Continuously monitor new verification codes in Mail app, automatically input to first booted simulator" | |
| echo " Check interval: 5 seconds" | |
| echo " Use Ctrl+C to stop monitoring" | |
| echo "" | |
| echo "Example:" | |
| echo " $0 # Monitor using first booted simulator" | |
| echo "" | |
| } | |
| # Log output functions | |
| log_info() { | |
| echo -e "${BLUE}[$(date '+%H:%M:%S')] INFO:${NC} $1" >&2 | |
| } | |
| log_success() { | |
| echo -e "${GREEN}[$(date '+%H:%M:%S')] SUCCESS:${NC} $1" >&2 | |
| } | |
| log_warning() { | |
| echo -e "${YELLOW}[$(date '+%H:%M:%S')] WARNING:${NC} $1" >&2 | |
| } | |
| log_error() { | |
| echo -e "${RED}[$(date '+%H:%M:%S')] ERROR:${NC} $1" >&2 | |
| } | |
| # Signal handler function | |
| cleanup() { | |
| RUNNING=false | |
| log_info "Received stop signal, exiting..." | |
| exit 0 | |
| } | |
| # Set signal handler | |
| trap cleanup SIGINT SIGTERM | |
| # Check dependency scripts | |
| check_dependencies() { | |
| if ! command -v axe > /dev/null; then | |
| log_error "AXe tool not found, please install it first" | |
| echo "Installation: brew install cameroncooke/tap/axe" | |
| exit 1 | |
| fi | |
| } | |
| # Get verification code | |
| # Return codes: | |
| # 0: Success (code found) | |
| # 1: Failed to get verification code (Mail app issue) | |
| # 2: No verification code found (Mail app working, but no email) | |
| get_verification_code() { | |
| local output | |
| local exit_code | |
| output=$( { "$SCRIPT_DIR/get_verification_code.sh" -q; } 2>&1 ) | |
| exit_code=$? | |
| if [ $exit_code -eq 0 ]; then | |
| # Success - code found | |
| echo "$output" | |
| return 0 | |
| elif [ $exit_code -eq 2 ]; then | |
| # No verification code found (not an error) | |
| return 2 | |
| else | |
| # Mail app not running or other errors | |
| echo "$output" | |
| return 1 | |
| fi | |
| } | |
| # Get first booted simulator | |
| get_first_booted_simulator() { | |
| local simulator_info=$(axe list-simulators | grep -F "Booted" | head -n1) | |
| if [ -z "$simulator_info" ]; then | |
| log_error "No booted simulator found" | |
| return 1 | |
| fi | |
| local udid=$(echo "$simulator_info" | cut -d' ' -f1) | |
| local device_name=$(echo "$simulator_info" | sed -E 's/^[A-F0-9-]+ ([^(]+) .*/\1/' | xargs) | |
| log_success "Found booted simulator: $device_name (UDID: $udid)" | |
| echo "$udid|$device_name" | |
| return 0 | |
| } | |
| # Input verification code to simulator | |
| input_to_simulator() { | |
| local code="$1" | |
| local udid="$2" | |
| local output | |
| local exit_code | |
| output=$(echo "$code" | axe type --stdin --udid "$udid" 2>&1) | |
| exit_code=$? | |
| if [ $exit_code -eq 0 ]; then | |
| return 0 | |
| else | |
| log_error "axe type command failed with exit code $exit_code" | |
| if [ -n "$output" ]; then | |
| log_error "Error output: $output" | |
| fi | |
| return 1 | |
| fi | |
| } | |
| # Main monitoring loop | |
| monitor_verification_codes() { | |
| log_info "Starting verification code monitoring..." | |
| log_info "Device: $DEVICE_NAME (UDID: $UDID)" | |
| log_info "Check interval: ${CHECK_INTERVAL} seconds" | |
| log_info "Press Ctrl+C to stop monitoring" | |
| echo "" | |
| # Get initial verification code | |
| log_info "Getting current verification code as baseline..." | |
| CURRENT_CODE=$(get_verification_code) | |
| local result=$? | |
| if [ "$result" -eq 0 ]; then | |
| log_info "Current verification code: $CURRENT_CODE" | |
| elif [ "$result" -eq 2 ]; then | |
| log_warning "No verification code found yet" | |
| CURRENT_CODE="" | |
| else | |
| log_warning "Failed to get verification code, using empty value as baseline" | |
| if [ -n "$CURRENT_CODE" ]; then | |
| log_warning "Error output: $CURRENT_CODE" | |
| fi | |
| CURRENT_CODE="" | |
| fi | |
| echo "" | |
| # Start monitoring loop | |
| local check_count=0 | |
| while [ "$RUNNING" = true ]; do | |
| check_count=$((check_count + 1)) | |
| log_info "Check #$check_count..." | |
| # Get latest verification code | |
| local new_code | |
| new_code=$(get_verification_code) | |
| local result=$? | |
| if [ "$result" -eq 0 ]; then | |
| # Compare verification codes | |
| if [ "$new_code" != "$CURRENT_CODE" ]; then | |
| log_success "Found new verification code: $new_code (old: ${CURRENT_CODE:-none})" | |
| # Input to simulator | |
| log_info "Inputting verification code to simulator..." | |
| if input_to_simulator "$new_code" "$UDID"; then | |
| log_success "Verification code successfully input to simulator" | |
| # Copy to clipboard | |
| if command -v pbcopy > /dev/null; then | |
| echo -n "$new_code" | pbcopy | |
| log_info "Verification code copied to clipboard" | |
| fi | |
| else | |
| log_error "Failed to input verification code to simulator" | |
| fi | |
| # Update current verification code | |
| CURRENT_CODE="$new_code" | |
| echo "" | |
| else | |
| log_info "Verification code unchanged: $new_code" | |
| fi | |
| elif [ "$result" -eq 2 ]; then | |
| log_warning "No verification code found yet" | |
| else | |
| log_error "Failed to get verification code" | |
| if [ -n "$new_code" ]; then | |
| log_error "Error output: $new_code" | |
| fi | |
| fi | |
| # Wait for next check | |
| sleep "$CHECK_INTERVAL" | |
| done | |
| } | |
| # Main function | |
| main() { | |
| # Parse command line arguments | |
| while [[ $# -gt 0 ]]; do | |
| case $1 in | |
| -h|--help) | |
| show_help | |
| exit 0 | |
| ;; | |
| *) | |
| log_error "Unknown option: $1" | |
| echo "Use -h or --help to see help information" | |
| exit 1 | |
| ;; | |
| esac | |
| done | |
| # Check dependencies | |
| check_dependencies | |
| # Get first booted simulator | |
| log_info "Looking for first booted simulator..." | |
| local result | |
| result=$(get_first_booted_simulator) | |
| local exit_code=$? | |
| if [ $exit_code -ne 0 ]; then | |
| echo "" | |
| echo "Available simulators:" | |
| axe list-simulators | |
| exit 1 | |
| fi | |
| UDID=$(echo "$result" | cut -d'|' -f1) | |
| DEVICE_NAME=$(echo "$result" | cut -d'|' -f2) | |
| # Start monitoring | |
| monitor_verification_codes | |
| } | |
| # If running this script directly | |
| if [ "${BASH_SOURCE[0]}" == "${0}" ]; then | |
| main "$@" | |
| fi |
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/bash | |
| # Get verification code and input to Android emulator | |
| # Dependencies: get_verification_code.sh and adb tool | |
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
| ADB_PATH="$HOME/Library/Android/sdk/platform-tools/adb" | |
| # Color output | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| NC='\033[0m' # No Color | |
| show_help() { | |
| echo "Usage: $0 [options]" | |
| echo "" | |
| echo "Options:" | |
| echo " -l, --list List available Android emulators" | |
| echo " -h, --help Show this help information" | |
| echo "" | |
| echo "Description:" | |
| echo " Get latest verification code from Mail app and automatically input to first running Android emulator" | |
| echo "" | |
| echo "Examples:" | |
| echo " $0 # Input verification code to first running emulator" | |
| echo " $0 -l # List available emulators" | |
| echo "" | |
| } | |
| list_emulators() { | |
| echo -e "${YELLOW}Available Android devices:${NC}" | |
| "$ADB_PATH" devices -l | |
| } | |
| get_first_running_emulator() { | |
| local device_list=$("$ADB_PATH" devices -l | tail -n +2 | grep -v "^$" | grep "device ") | |
| if [ -z "$device_list" ]; then | |
| echo -e "${RED}Error: No running emulator found${NC}" >&2 | |
| return 1 | |
| fi | |
| local device_id=$(echo "$device_list" | head -n1 | awk '{print $1}') | |
| local device_name=$(echo "$device_list" | head -n1 | sed -E 's/.*model:([^ ]+).*/\1/' 2>/dev/null || echo "Unknown") | |
| # Fallback: if model extraction failed, try to get device name from model field | |
| if [ "$device_name" = "Unknown" ] || [ -z "$device_name" ]; then | |
| device_name=$(echo "$device_list" | head -n1 | awk '{for(i=1;i<=NF;i++){if($i~/model:/){print $i}}}' | cut -d':' -f2) | |
| if [ -z "$device_name" ]; then | |
| device_name="$device_id" | |
| fi | |
| fi | |
| echo -e "${GREEN}Found running emulator: $device_name${NC}" >&2 | |
| echo -e "${GREEN}Device ID: $device_id${NC}" >&2 | |
| echo "$device_id" | |
| } | |
| check_adb() { | |
| if [ ! -f "$ADB_PATH" ]; then | |
| echo -e "${RED}Error: adb tool not found at $ADB_PATH${NC}" | |
| echo "Please check Android SDK installation" | |
| exit 1 | |
| fi | |
| } | |
| input_verification_code() { | |
| local device_id="$1" | |
| echo -e "${YELLOW}Getting latest verification code...${NC}" | |
| local output | |
| local exit_code | |
| output=$( { "$SCRIPT_DIR/get_verification_code.sh" -q; } 2>&1 ) | |
| exit_code=$? | |
| if [ $exit_code -eq 0 ]; then | |
| local code="$output" | |
| echo -e "${GREEN}Found verification code: $code${NC}" | |
| elif [ $exit_code -eq 2 ]; then | |
| echo -e "${YELLOW}No verification code found in recent emails${NC}" | |
| exit 1 | |
| else | |
| echo -e "${RED}Failed to get verification code${NC}" | |
| if [ -n "$output" ]; then | |
| echo -e "${RED}Error: $output${NC}" | |
| fi | |
| exit 1 | |
| fi | |
| echo -e "${YELLOW}Inputting verification code to emulator...${NC}" | |
| if "$ADB_PATH" -s "$device_id" shell input text "$code"; then | |
| echo -e "${GREEN}Verification code successfully input to emulator${NC}" | |
| else | |
| echo -e "${RED}Failed to input verification code to emulator${NC}" | |
| echo "Please check if emulator is running and accessible" | |
| exit 1 | |
| fi | |
| } | |
| main() { | |
| while [[ $# -gt 0 ]]; do | |
| case $1 in | |
| -l|--list) | |
| list_emulators | |
| exit 0 | |
| ;; | |
| -h|--help) | |
| show_help | |
| exit 0 | |
| ;; | |
| *) | |
| echo -e "${RED}Unknown option: $1${NC}" | |
| echo "Use -h or --help to see help information" | |
| exit 1 | |
| ;; | |
| esac | |
| done | |
| check_adb | |
| echo -e "${YELLOW}Looking for first running emulator...${NC}" | |
| local device_id | |
| device_id=$(get_first_running_emulator) | |
| local exit_code=$? | |
| if [ $exit_code -ne 0 ]; then | |
| echo "" | |
| echo "Available devices:" | |
| "$ADB_PATH" devices -l | |
| exit 1 | |
| fi | |
| input_verification_code "$device_id" | |
| } | |
| if [ "${BASH_SOURCE[0]}" == "${0}" ]; then | |
| main "$@" | |
| fi |
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/bash | |
| # Get verification code and input to iOS simulator | |
| # Dependencies: get_verification_code.sh and AXe tool | |
| # Get script directory | |
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
| # Color output | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| NC='\033[0m' # No Color | |
| # Show help information | |
| show_help() { | |
| echo "Usage: $0 [options]" | |
| echo "" | |
| echo "Options:" | |
| echo " -l, --list List available simulators" | |
| echo " -h, --help Show this help information" | |
| echo "" | |
| echo "Description:" | |
| echo " Get latest verification code from Mail app and automatically input to first booted iOS simulator" | |
| echo "" | |
| echo "Examples:" | |
| echo " $0 # Input verification code to first booted simulator" | |
| echo " $0 -l # List available simulators" | |
| echo "" | |
| } | |
| # List available simulators | |
| list_simulators() { | |
| echo -e "${YELLOW}Available iOS simulators:${NC}" | |
| axe list-simulators | |
| } | |
| # Get first booted simulator | |
| get_first_booted_simulator() { | |
| local simulator_info=$(axe list-simulators | grep -F "Booted" | head -n1) | |
| if [ -z "$simulator_info" ]; then | |
| echo -e "${RED}Error: No booted simulator found${NC}" >&2 | |
| return 1 | |
| fi | |
| local udid=$(echo "$simulator_info" | cut -d' ' -f1) | |
| local device_name=$(echo "$simulator_info" | sed -E 's/^[A-F0-9-]+ ([^(]+) .*/\1/' | xargs) | |
| echo -e "${GREEN}Found booted simulator: $device_name${NC}" >&2 | |
| echo -e "${GREEN}UDID: $udid${NC}" >&2 | |
| # Only output UDID to stdout | |
| echo "$udid" | |
| } | |
| # Check if AXe is installed | |
| check_axe() { | |
| if ! command -v axe > /dev/null; then | |
| echo -e "${RED}Error: AXe tool not found${NC}" | |
| echo "Please install AXe first: brew install cameroncooke/tap/axe" | |
| exit 1 | |
| fi | |
| } | |
| # Get verification code and input to simulator | |
| input_verification_code() { | |
| local udid="$1" | |
| echo -e "${YELLOW}Getting latest verification code...${NC}" | |
| # Use quiet mode to get verification code | |
| local output | |
| local exit_code | |
| output=$( { "$SCRIPT_DIR/get_verification_code.sh" -q; } 2>&1 ) | |
| exit_code=$? | |
| if [ $exit_code -eq 0 ]; then | |
| local code="$output" | |
| echo -e "${GREEN}Found verification code: $code${NC}" | |
| elif [ $exit_code -eq 2 ]; then | |
| echo -e "${YELLOW}No verification code found in recent emails${NC}" | |
| exit 1 | |
| else | |
| echo -e "${RED}Failed to get verification code${NC}" | |
| if [ -n "$output" ]; then | |
| echo -e "${RED}Error: $output${NC}" | |
| fi | |
| exit 1 | |
| fi | |
| # Input verification code to simulator | |
| echo -e "${YELLOW}Inputting verification code to simulator...${NC}" | |
| if echo "$code" | axe type --stdin --udid "$udid"; then | |
| echo -e "${GREEN}Verification code successfully input to simulator${NC}" | |
| else | |
| echo -e "${RED}Failed to input verification code to simulator${NC}" | |
| echo "Please check if simulator is running and accessible" | |
| exit 1 | |
| fi | |
| } | |
| # Main function | |
| main() { | |
| # Parse command line arguments | |
| while [[ $# -gt 0 ]]; do | |
| case $1 in | |
| -l|--list) | |
| list_simulators | |
| exit 0 | |
| ;; | |
| -h|--help) | |
| show_help | |
| exit 0 | |
| ;; | |
| *) | |
| echo -e "${RED}Unknown option: $1${NC}" | |
| echo "Use -h or --help to see help information" | |
| exit 1 | |
| ;; | |
| esac | |
| done | |
| # Check necessary tools | |
| check_axe | |
| # Get first booted simulator | |
| echo -e "${YELLOW}Looking for first booted simulator...${NC}" | |
| local udid | |
| udid=$(get_first_booted_simulator) | |
| local exit_code=$? | |
| if [ $exit_code -ne 0 ]; then | |
| echo "" | |
| echo "Available simulators:" | |
| axe list-simulators | |
| exit 1 | |
| fi | |
| # Execute main functionality | |
| input_verification_code "$udid" | |
| } | |
| # If running this script directly | |
| if [ "${BASH_SOURCE[0]}" == "${0}" ]; then | |
| main "$@" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment