Skip to content

Instantly share code, notes, and snippets.

@andsens andsens/type.sh
Created Jul 13, 2012

Embed
What would you like to do?
Keyboard typing tool for AppleTV AirControl
#!/bin/bash
url='http://apple-tv.anders.local'
menu="$url/remoteAction=1"
menuhold="$url/remoteAction=2"
up="$url/remoteAction=3"
down="$url/remoteAction=4"
select="$url/remoteAction=5"
left="$url/remoteAction=6"
right="$url/remoteAction=7"
playpause="$url/remoteAction=10"
pause="$url/remoteAction=15"
play="$url/remoteAction=16"
stop="$url/remoteAction=17"
ffwd="$url/remoteAction=18"
rew="$url/remoteAction=19"
chapterfwd="$url/remoteAction=20"
chapterback="$url/remoteAction=21"
selecthold="$url/remoteAction=22"
mode=0
posx=0
posy=0
function send {
curl $1
# sleep 0.1
}
function times {
local amount=$1
local cmd=$2
while [ $amount -gt "0" ]; do
amount=$(($amount-1))
send $cmd
done
}
function abspos {
local ord=`printf '%d' "'$1"`
local abspos=
local setmode=0
if (( 97 <= $ord && $ord <= 122 )); then
abspos=$(($ord-97)) # a-z
fi
if (( 65 <= $ord && $ord <= 90 )); then
abspos=$(($ord-65)) # A-Z
setmode=1
fi
if [ $ord = 48 ]; then
abspos=35 # 0
fi
if (( 49 <= $ord && $ord <= 57 )); then
abspos=$(($ord-23)) # 1-9
fi
if [ -z $abspos ]; then
setmode=2
case $1 in
'.') abspos=36; setmode=0 ;;
'_') abspos=37; setmode=0 ;;
'@') abspos=38; setmode=0 ;;
'!') abspos=0 ;;
'#') abspos=2 ;;
'$') abspos=3 ;;
'%') abspos=4 ;;
'&') abspos=5 ;;
'~') abspos=6 ;;
'*') abspos=7 ;;
'\') abspos=8 ;;
'/') abspos=9 ;;
'?') abspos=10 ;;
'^') abspos=11 ;;
'_') abspos=12 ;;
'`') abspos=13 ;;
';') abspos=14 ;;
':') abspos=15 ;;
'|') abspos=16 ;;
'=') abspos=17 ;;
'é') abspos=18 ;;
'n') abspos=19 ;;
'[') abspos=20 ;;
']') abspos=21 ;;
'{') abspos=22 ;;
'}') abspos=23 ;;
'c') abspos=24 ;;
'ü') abspos=25 ;;
# '.') abspos=26 ;;
',') abspos=27 ;;
'+') abspos=28 ;;
'-') abspos=29 ;;
'<') abspos=30 ;;
'>') abspos=31 ;;
'(') abspos=32 ;;
')') abspos=33 ;;
"'") abspos=34 ;;
'"') abspos=35 ;;
*) ;;
esac
fi
local posx=$(($abspos%6))
local posy=`printf "%.0f" $(($abspos/6))`
printf $posx' '$posy' '$setmode
}
function goto {
if (( $posx > $1 )); then
times $(($posx-$1)) $left
fi
if (( $posx < $1 )); then
times $(($1-$posx)) $right
fi
posx=$1
if (( $posy > $2 )); then
times $(($posy-$2)) $up
fi
if (( $posy < $2 )); then
times $(($2-$posy)) $down
fi
posy=$2
}
function enter {
if (( $mode < $3 )); then
goto $posx 0
send $up
times $(($3-$mode)) $right
send $down
mode=$3
fi
if (( $mode > $3 )); then
goto $posx 0
send $up
times $(($mode-$3)) $left
send $down
mode=$3
fi
goto $1 $2
send $select
}
function calibrate {
times 7 $left
times 7 $down
posx=0
posy=7
}
if [ -n $1 ]; then
str=$1
while [ -n "$str" ]; do
char=`printf "%c\n" "$str"`
enter `abspos $char`
str=${str#?}
done
else
printf "Type remotely:\n"
while read -n 1 char; do
enter `abspos $char`
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.