[Release] Macos/Linux scripts to interface with Android

Discussion in 'Computer Software and Operating Systems' started by notimp, Feb 12, 2019 at 6:19 PM.

  1. notimp
    OP

    notimp GBAtemp Advanced Maniac

    Member
    8
    Sep 18, 2007
    Recently updated to a new machine on Macos Mojave, so I'm currently rewriting my scripts to interface my laptop with whatever IoT device running Android I want to address within the network.

    The "crownjewel" of it is a script that takes any video link in the clipboard (the thing text gets stored at when you do the copy part of copy and paste ;) ), and starts to play it on any Kodi installation on any Android device within the network. But there are a few interesting others amongst them as well. :)
    --

    Prerequisites:

    Have adb installed.
    This installer should do it for you:
    http://forum.xda-developers.com/android/general/adb-fastboot-binaries-os-x-inclusive-t2941205

    Enable adb debugging on the Android device you want to control remotely.

    Get familiar with basic adb commands and play with them in the terminal.
    - adb connect IPADDRESS
    - adb devices
    - adb disconnect

    Basically make sure that you can adb connect to your Android IoT device(s) before you try any of the stuff that follows.

    Store the scripts under the given name in the ~/Documents/FireRemote/ folder, both names and folder paths usually were hardcoded within the scripts. I was lazy. ;)

    In terminal cd into ~/Documents/FireRemote/ and run
    chmod +x *.sh
    once, after you've created all the shell scripts there, to make them executable.
    //

    Scripts:

    Most of the scripts hold a Script-Editor (applescript) part and an shellscript (.sh) part. The applescript part you can simply copy/paste into Script-Editor and then save as an .app - this usually serves the part of 'getting a program to launch the shell script'. The shell scripts usually were written separately when more than a few lines of shell code were involved. This also makes most of this at least partly applicable to Linux systems - where you could at least salvage most of the shell code.

    Lets start.

    1. Scripts to switch between Android IoT devices to address:
    Script-Editor code:
    Code:
    tell application "Terminal"
        do script ("adb connect 192.168.1.111")
    end tell
    tell application "Terminal"
        do script ("/bin/sleep 0.7 &&
        adb disconnect &&
        pkill -a Terminal")
    end tell
    
    Save them as Livingroom.app or Bedroom.app for example change the IP address from 192.168.1.111 to the IP address of the android device you want to address. This is not optional. :)

    What the script does is, it quickly connects you via adb to the android box of your choice, then disconnects. Its used to leave a 'trace' of that IP address in the terminal history. The terminal history is then used by all of the following scripts to get the 'last adb device you've connected to' and connect to that again. So by using those small 1. scripts, you can switch between IoT devices for all other scripts.

    1a. Connection Helper
    shellscript code, save as connecth.sh in the ~/Documents/FireRemote/ folder
    Code:
    eval $(history | cut -c 8- | grep "adb connect [0-9]" | tail -1)
    Small little helper script that may be called by later scripts sometimes, to connect to the last adb device you were connected to.

    Now lets get into the fun stuff. :)

    2. Tell Kodi on the Android device to play the video link in your clipboard on the Mac
    Script-Editor code, save as an app with a name of your choosing:
    Code:
    tell application "Terminal"
        do script "eval $(history | cut -c 8- | grep 'adb connect [0-9]' | tail -1)"
        do script "~/Documents/FireRemote/acopyh.sh > /dev/null 2>&1 &"
    end tell
    shellscript code, save as acopyh.sh in the ~/Documents/FireRemote/ folder
    Code:
    # adb devices
    
    sleep 2.5
    
    touch ~/Documents/FireRemote/zwischenablage.m3u
    set eof ~/Documents/FireRemote/zwischenablage.m3u to 0
    
    pbpaste | sed 's,http://www.youtube.com/watch?v=,plugin://plugin.video.youtube/play/?video_id=,g'| pbcopy
    pbpaste | sed 's,http://www.youtube.com/watch?v=,plugin://plugin.video.youtube/play/?video_id=,g'| pbcopy
    pbpaste | sed 's,http://youtube.com/watch?v=,plugin://plugin.video.youtube/play/?video_id=,g'| pbcopy
    pbpaste | sed 's,http://youtube.com/watch?v=,plugin://plugin.video.youtube/play/?video_id=,g'| pbcopy
    
    pbpaste | sed 's,http://www.vimeo.com/,plugin://plugin.video.vimeo/play/?video_id=,g'| pbcopy
    pbpaste | sed 's,http://www.vimeo.com/,plugin://plugin.video.vimeo/play/?video_id=,g'| pbcopy
    pbpaste | sed 's,http://vimeo.com/,plugin://plugin.video.vimeo/play/?video_id=,g'| pbcopy
    pbpaste | sed 's,http://vimeo.com/,plugin://plugin.video.vimeo/play/?video_id=,g'| pbcopy
    
    # pbpaste | sed 's,http://,http://,g'| pbcopy
    # pbpaste | sed 's,&start=0,,g'| pbcopy
    
    pbpaste | sed 's,&export=download,/preview,g'| pbcopy
    pbpaste | sed 's,http://docs.google.com/uc?id=,plugin://plugin.video.gdrive?mode=streamURL&url=http://docs.google.com/file/d/,g'| pbcopy
    
    pbpaste > ~/Documents/FireRemote/zwischenablage.m3u
    
    sleep 0.5
    
    adb push ~/Documents/FireRemote/zwischenablage.m3u /sdcard/
    
    sleep 0.5
    
    adb shell am start -n org.xbmc.kodi/.Splash -a android.intent.action.VIEW
    
    #!/bin/bash
    #set url and port to the xbmc box webservice
    XBMC_HOST="http://$(history | cut -c 8- | grep "adb connect [0-9]" | tail -1 | sed "s/[^0-9|\.]*//g"):8087"
    echo ${XBMC_HOST}
    curl --header "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"Player.Stop","params":{"playerid":1},"id":1}' "${XBMC_HOST}/jsonrpc"
    sleep 1
    curl --header "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"Player.Open","params":{"item":{"file":"/sdcard/zwischenablage.m3u"}},"id":1}' "${XBMC_HOST}/jsonrpc"
    
    # adb devices
    
    adb disconnect
    
    killall Terminal
    This copies the videolink in your clipboard into a m3u file named zwischenablage.m3u, then adb pushes the file to /sdcard/ on your android device, then tells Kodi to play it - thereby playing the videolink.

    There is an additional prerequisite for this one:
    In Kodi on the Android device(s) - go to the settings (enable expert settings), services and webserver. Enable "allow control of Kodi via HTTP", set the port to 8087 (I've changed it from the default 8080, in case other apps would like to use the port, the script looks for 8087), dont set a password.

    3. Get the Kodi log file onto your Macos Desktop
    Script-Editor code, save as an app with a name of your choosing:
    Code:
    tell application "Terminal"
        do script "~/Documents/FireRemote/kologh.sh"
    end tell
    shellscript code, save as kologh.sh in the ~/Documents/FireRemote/ folder
    Code:
    eval $(history | cut -c 8- | grep "adb connect [0-9]" | tail -1)
    
    # adb devices
    
    sleep 2
    
    adb pull /sdcard/Android/data/org.xbmc.kodi/files/.kodi/temp/kodi.log ~/Desktop/
    
    # adb devices
    
    adb disconnect
    
    killall Terminal
    4. Control Kodi on the Android device from the Macos laptop.
    Script-Editor code, save as an app with a name of your choosing:
    Code:
    set x to isAppLoaded("Python")
    if x then
        tell application "System Events"
        end tell
        do shell script "pkill -a Python"
        do shell script "adb disconnect"
        do shell script "pkill -a Terminal"
    else
        tell application "Terminal"
            do script "eval python ~/Documents/FireRemote/xbev/xbev.py $(history | cut -c 8- | grep 'adb connect [0-9]' | tail -1 | sed 's/adb connect //g' | grep [0-9])"
        end tell
     
        tell application "Terminal"
            try
                get window 1
            end try
        end tell
     
        tell application "System Events"
            set visible of process "Terminal" to false
        end tell
     
        do shell script "sleep 1"
     
        tell application "System Events"
            set visible of process "Python" to false
        end tell
     
        repeat
            tell application "System Events"
                if (exists (application process "Python")) then
                    exit repeat
                end if
            end tell
        end repeat
     
        repeat
            tell application "System Events"
                if visible of process "Python" is false then
                    set visible of process "Python" to true
                end if
            end tell
            tell application "System Events"
                if visible of process "Python" is true then
                    exit repeat
                end if
            end tell
        end repeat
     
        repeat
            tell application "System Events"
                if application process "Python" is frontmost then
                    exit repeat
                else
                    tell application process "Python"
                        set frontmost to true
                    end tell
                end if
            end tell
        end repeat
     
    end if
    
    on isAppLoaded(app_name)
        tell application "System Events"
            set app_list to every application process whose name contains app_name
            if the (count of app_list) > 0 then
                return true
            else
                return false
            end if
        end tell
    end isAppLoaded
    This one has two prerequisites, because its based on an actual app called xbev.

    Download xbev from here:
    http://github.com/bobo1on1/xbev
    Place the files in a folder named ~/Documents/FireRemote/xbev/

    Download and install brew from here:
    http://brew.sh/
    Once installed run
    brew install pygtk
    in a terminal window to install pygtk

    Follow the xbev readme to set up Kodi to be controlled from a remote device.

    After that running the script above will open the xbev program connected to the last android device you were connected. The xbev program is then used to control Kodi remotely (same controls as a PC installation of Kodi, try the arrowkeys.. ;)). If you run the script again, it will exit xbev automatically (because cmd+q doesnt work on this one. :) )

    5. Move files from and to an Android IoT device (full file browser)
    Script-Editor code, save as an app with a name of your choosing:
    Code:
    set x to isAppLoaded("Python")
    if x then
        do shell script "pkill -a Python"
        do shell script "pkill -a Terminal"
        do shell script "adb disconnect"
    else
        tell application "Terminal"
            do script "~/Documents/FireRemote/aafmtriggerh.sh"
        end tell
     
        do shell script "sleep 3"
     
        tell application "System Events"
            set visible of process "Python" to false
        end tell
     
        repeat
            tell application "System Events"
                if (exists (application process "Python")) then
                    exit repeat
                end if
            end tell
        end repeat
     
        repeat
            tell application "System Events"
                if visible of process "Python" is false then
                    set visible of process "Python" to true
                end if
            end tell
            tell application "System Events"
                if visible of process "Python" is true then
                    exit repeat
                end if
            end tell
        end repeat
     
        repeat
            tell application "System Events"
                if application process "Python" is frontmost then
                    exit repeat
                else
                    tell application process "Python"
                        set frontmost to true
                    end tell
                end if
            end tell
        end repeat
     
    end if
    
    on isAppLoaded(app_name)
        tell application "System Events"
            set app_list to every application process whose name contains app_name
            if the (count of app_list) > 0 then
                return true
            else
                return false
            end if
        end tell
    end isAppLoaded
    shellscript code, save as aafmtriggerh.sh in the ~/Documents/FireRemote/ folder
    Code:
    eval $(history | cut -c 8- | grep "adb connect [0-9]" | tail -1)
    sleep 1
    python2 ~/Documents/FireRemote/aafm/aafm-gui.py
    This one has similar prerequisites as 4. so (also running it a second time will close it as well):

    Download aafm from here:
    http://github.com/sole/aafm/releases
    Place the files in a folder named ~/Documents/FireRemote/aafm/

    (if you havent already done for 4.) Download and install brew from here:
    http://brew.sh/
    Once installed run
    brew install pygtk
    in a terminal window to install pygtk

    6. Force stop apps on the Android IoT device, then start (same/other) apps remotely.
    Script-Editor code, save as an app with a name of your choosing:
    Code:
    tell application "Terminal"
        do script "~/Documents/FireRemote/connecth.sh"
    end tell
    
    do shell script "/bin/sleep 1.7"
    tell application "Terminal" to quit
    
    do shell script "adb shell am force-stop org.xbmc.kodi"
    do shell script "adb shell am force-stop org.xbmc.ftmc"
    do shell script "adb shell am force-stop com.webkey"
    
    repeat
        tell application "Finder"
     
            set optionList to {"acopy", "kodi"}
     
            set chosenItem to choose from list optionList with prompt "Cleared: Start apps" default items "acopy"
     
            if chosenItem is false then
                do shell script "adb disconnect"
                error number -128 (* user cancelled *)
            else
                set chosenItem to chosenItem's item 1 (* extract choice from list *)
            end if
     
            if chosenItem is "fcopy" then
                do shell script "open ~/Documents/FireRemote/fcopy.app"
                error number -128 -- user canceled
            end if
            if chosenItem is "acopy" then
                do shell script "open ~/Documents/FireRemote/acopy.app"
                error number -128 -- user canceled
            end if
            if chosenItem is "ftmc" then
                do shell script "adb shell monkey -p org.xbmc.ftmc 1"
                do shell script "adb disconnect"
                error number -128 -- user canceled
            end if
            if chosenItem is "kodi" then
                do shell script "adb shell monkey -p org.xbmc.kodi 1"
                do shell script "adb disconnect"
                error number -128 -- user canceled
            end if
            if chosenItem is "zdmc" then
                do shell script "adb shell monkey -p org.zdmc.ftmc 1"
                do shell script "adb disconnect"
                error number -128 -- user canceled
            end if
            if chosenItem is "firestarter" then
                do shell script "adb shell monkey -p de.belu.firestarter 1"
                do shell script "adb disconnect"
                error number -128 -- user canceled
            end if
        end tell
    end repeat
    This is intended to be edited by you as you see fit. The script basically force-closes all the apps you define with force-stop at the beginning of it on the Android device, then launches one of whatever apps you can define later in a list. Useful for f.e. when Kodi crashes and you have to relaunch it remotely.

    Might add a few more scripts in, as I fix them for Macos Mojave in the future.

    Have fun. :)

    n.

    If you are asking yourself what the heck you should do with the "play videolink in clipboard with Kodi on a remote Android IoT device script", I'll leave you these links: ;)

    http://chrome.google.com/webstore/detail/video-downloadhelper/lmjnegcaeklhafolokijcfjliaokphfk
    http://addons.mozilla.org/en/firefox/addon/video-downloadhelper/

    This extension allows you to copy the direct video link of almost any online video to your clipboard. It all starts there. :)
     
    Last edited by notimp, Feb 13, 2019 at 7:56 AM
  2. notimp
    OP

    notimp GBAtemp Advanced Maniac

    Member
    8
    Sep 18, 2007
    Posted the wrong code for 2. for recent Kodi installations (well, tested with up to Kodi 17 ;) ), fixed now. :)
     
    Last edited by notimp, Feb 12, 2019 at 6:58 PM
Loading...