1. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Index:

    General setup (Start here)

    1. Connecting to an Android (TV) box over adb
    1a. Connection helper script
    2. url2kodi - 'n play, stream video urls from the web

    2a. how to get direct video urls from web videos
    3. copy the kodi.log file to your macOS Desktop
    4. xbev - control Kodi on the Android box from macOS

    4a. xbev - fixes
    5. aafm - adb filebrowser, move files between Android and macOS
    6. multilauncher - force stop and then launch apps on the Android box from macOS


    -

    Android smartphone scripts (requires Magisk (root)):

    -

    Miscellaneous:
    --


    What it all looks like:

    Most Script Editor Apps in here dont look like anything out of the ordinary, because they basically run in the background once launched, and then quit on their own, once they have done their job. So for all intents and purposes, they look like this:

    [​IMG]
    - when launched via Quicksilver (macOS app seen in the image above), or Spotlight.

    - Cetus Play

    (Universal Android Remote) running in Bluestacks (Android Emulator for Mac or PC) looks like this:

    [​IMG]

    Usually it also has embedded google ads, but you can get rid of those using f.e. DNS66 ( https://forum.xda-developers.com/android/apps-games/app-dns66-source-host-ad-blocker-root-t3487497 ).

    Cetus play relies on a companion app on your android device ('server' part), that needs to be in memory (get launched) on the Android device, for it to work, for this you can use simply a Script Editor script (saved as .app) a well - or you use the multilauncher approach from [6. Force stop apps on the Android IoT device, then start (same/other) apps remotely], which then looks like this:

    - Multi launcher

    [​IMG]

    When launching this Script Editor app it will first force stop the Android applications you've specified in the Script Editor code, and then launch any of the apps you pick - on your android device. (Or another script in the case of fcopy ;) ), and then close out.

    - xbev

    is a little python scriptlet that allows you to control any Kodi installation using your Macs (or PCs) keyboard and simply looks like this:

    [​IMG]

    - aafm (an adb file manager)

    is an Android/to Mac/PC filemanager that works over adb and looks like this:

    [​IMG]

    Some of the scripts (like the youtube playlist m3u generator) also temporarily open a terminal window (- but usually in the background).

    And thats it. I hope you now have a good understanding of what 'controlling your Android box under your TV' from a Macbook, or linux laptop is all about. :)

    Have fun.

    n.
     
    Last edited by notimp, Jan 17, 2020
    Ricken likes this.
  2. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    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.

    An easy way to install it would be via brew - a MacOS command line package manager.

    Download and install brew from here:
    https://brew.sh/
    Once installed run
    brew install homebrew/cask/android-platform-tools

    (Alternatively you could install it via the installer found here:
    https://forum.xda-developers.com/android/general/adb-fastboot-binaries-os-x-inclusive-t2941205
    but we are using brew for other elements (dependencies) of the scripts in the future, so you might as well get familiar with it. :) )

    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.

    (If your Android box doesnt keep the adb debugging status enabled after a reboot, but can be rooted, you can use WiFi ADB to automatically enable it after every boot. Small detail. :) )

    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 ("history -s adb connect 192.168.1.111 && adb disconnect")
    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. (edit: new version just adds the connect line to the bottom of the terminal history and then (just in case) adb disconnects in case old connections were open).

    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,https://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,https://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,https://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,https://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,https://,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=https://docs.google.com/file/d/,g'| pbcopy
    
    pbpaste > ~/Documents/FireRemote/zwischenablage.m3u
    
    sleep 0.7
    
    adb push ~/Documents/FireRemote/zwischenablage.m3u /sdcard/
    
    sleep 1.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 "/usr/local/bin/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:
    https://github.com/bobo1on1/xbev
    Place the files in a folder named ~/Documents/FireRemote/xbev/

    Download and install brew from here:
    https://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")
    set y to isAppLoaded("Terminal")
    if x then
        do shell script "pkill -a Python"
        do shell script "pkill -a Terminal"
        do shell script "/usr/local/bin/adb disconnect"
    else
        if y then
            do shell script "pkill -a Terminal"
        end if
        tell application "System Events"
            tell process "Terminal"
                activate
                try
                    set theWindow to (first window whose name contains "Terminal")
                    perform action "AXRaise" of theWindow
                    do shell script "open -a Terminal"
                end try
            end tell
        end tell
    
        do shell script "sleep 1.5"
     
        tell application "Terminal"
            set mainID to id of front window
            -- insert your code
            do script "~/Documents/FireRemote/aafmtriggerh.sh" in window 1
            close (every window whose id ≠ mainID)
        end tell
     
        do shell script "sleep 3.5"
     
        tell application "System Events"
            set visible of process "Python" to false
        end tell
    
        do shell script "sleep 1"
     
        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:
    https://github.com/xerus2000/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:
    https://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 "/usr/local/bin/adb shell am force-stop org.xbmc.kodi"
    do shell script "/usr/local/bin/adb shell am force-stop org.xbmc.ftmc"
    do shell script "/usr/local/bin/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 "/usr/local/bin/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 "/usr/local/bin/adb shell monkey -p org.xbmc.ftmc 1"
                do shell script "/usr/local/bin/adb disconnect"
                error number -128 -- user canceled
            end if
            if chosenItem is "kodi" then
                do shell script "/usr/local/bin/adb shell monkey -p org.xbmc.kodi 1"
                do shell script "/usr/local/bin/adb disconnect"
                error number -128 -- user canceled
            end if
            if chosenItem is "zdmc" then
                do shell script "/usr/local/bin/adb shell monkey -p org.zdmc.ftmc 1"
                do shell script "/usr/local/bin/adb disconnect"
                error number -128 -- user canceled
            end if
            if chosenItem is "firestarter" then
                do shell script "/usr/local/bin/adb shell monkey -p de.belu.firestarter 1"
                do shell script "/usr/local/bin/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: ;)

    https://chrome.google.com/webstore/detail/video-downloadhelper/lmjnegcaeklhafolokijcfjliaokphfk
    https://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, Apr 12, 2020
  3. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Here is a small applescript (macOS specific) to select any file in the download folder, transfer it over ftp to a NAS and then ask if the file should be deleted locally.

    Code:
    set downloadsFolder to (path to home folder as text) & "Downloads"
    set targetfile to (choose file default location alias downloadsFolder)
    set targetposix to quoted form of POSIX path of targetfile
    
    tell application "Finder"
        with timeout of 7200 seconds
            tell application "Terminal"
                activate
                do script "clear && curl -T " & targetposix & " ftp://192.168.1.1/folder/subfolder/ --user username:password" in window 1
             
                set isBusy to true
                repeat until isBusy is false
                    tell application "Terminal"
                        tell window 1
                            set isBusy to busy as boolean --> Test if busy
                        end tell
                    end tell
                    delay 1 --> Check every second
                end repeat
             
                display dialog "Delete uploaded file?" with title "Delete file" buttons {"Yes", "No"} default button "Yes"
                if the button returned of the result is "Yes" then
                    do script "mv " & targetposix & " ~/.Trash" in window 1
                    do shell script "afplay '/System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/dock/drag to trash.aif'"
                end if
                do script "pkill -a Terminal" in window 1
            end tell
        end timeout
    end tell
    You have to change the ftp server IP, its folders and username and password in the script to match yours.

    You can save it as an app with Script-Editor, once you've done so.

    I've also tweaked the timeouts in the send link to Kodi script so they work reliably with a current mac on my network. :) (As the macs become faster, the timeouts have to become longer, so adb push finishes successfully, before the m3u file is played.)

    edit: Fixed the ftp script, so it also handles files with spaces in their name. :)
     
    Last edited by notimp, Feb 24, 2019
  4. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Script to ask youtube for the videolinks to a youtube playlist (the one whose url you have in the clipboard (= copy part of copy/paste)) via youtube-dl (widely used command line program).

    The script then uses jq ( https://stedolan.github.io/jq/ ) to grab certain parts of the result file (youtube video name and youtube video id), to then generate a well formed .m3u file (playlist file Kodi can read), push the file to Kodi via adb, and then tell Kodi to play it.

    The code is horrible, and relies on 10 temp files (that get created and deleted in the process), but it works. :)

    (MacOS) Script-Editor code, save as an app with a name of your choosing:
    Code:
    tell application "Terminal"
        do script "~/Documents/FireRemote/playlisthelper.sh"
    end tell
    tell application "System Events"
        tell application process "Terminal"
            set frontmost to true
        end tell
    end tell
    
    shellscript code, save as playlisthelper.sh in the ~/Documents/FireRemote/ folder
    Code:
    cd ~/Documents/FireRemote/
    sleep 0.5
    cd ~/Documents/FireRemote/ &&
    pbpaste > PLtemp.sh &&
    chmod +x PLtemp.sh &&
    sed -i -e '1s/^/youtube-dl -j --flat-playlist /' PLtemp.sh &&
    sed -i -e '1s/$/ | jq -r '.id' > \~\/Documents\/FireRemote\/PLin.txt/' PLtemp.sh &&
    sleep 0.5 &&
    ./PLtemp.sh &&
    sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\n&https:\/\/www.youtube.com\/watch\?v\=/g' PLin.txt > PLouth1.txt &&
    sed '$! { P; D; }; s/\(.$\)/\1\1/' PLouth1.txt > PLouth2.txt &&
    sed 's/.$//' < PLouth2.txt > PLout.txt &&
    
    rm ~/Documents/FireRemote/PLouth1.txt &&
    rm ~/Documents/FireRemote/PLouth2.txt &&
    
    sed -i -e '1s/^/https:\/\/www.youtube.com\/watch\?v\=/' PLout.txt &&
    
    rm ~/Documents/FireRemote/PLtemp.sh &&
    rm ~/Documents/FireRemote/PLtemp.sh-e &&
    
    pbpaste > PLtemp.sh &&
    chmod +x PLtemp.sh &&
    sed -i -e '1s/^/youtube-dl -j --flat-playlist /' PLtemp.sh &&
    sed -i -e '1s/$/ | jq -r '.title' | sed 's_^_#EXTINF:0,_' > \~\/Documents\/FireRemote\/PLname.txt/' PLtemp.sh &&
    sleep 0.5 &&
    ./PLtemp.sh &&
    paste -d" " PLname.txt PLout.txt > PLmerge.txt &&
    
    lf=$'\n'; cat PLmerge.txt | sed "s/ https/\\$lf&https/g" | sed "s/ https/https/g" > PLout.txt &&
    
    lf=$'\n'; cat PLout.txt | sed "1s/^/#EXTM3U\\$lf/g" | sed "s/\s*httpshttps/https/g" > PLfinal.txt &&
    
    cat PLfinal.txt | pbcopy &&
    
    pbpaste | sed 's,https://www.youtube.com/watch?v=,plugin://plugin.video.youtube/play/?video_id=,g'| pbcopy
    
    sleep 0.5
    rm ~/Documents/FireRemote/PLtemp.sh
    rm ~/Documents/FireRemote/PLtemp.sh-e
    rm ~/Documents/FireRemote/PLin.txt
    rm ~/Documents/FireRemote/PLout.txt
    rm ~/Documents/FireRemote/PLout.txt-e
    rm ~/Documents/FireRemote/PLname.txt
    rm ~/Documents/FireRemote/PLmerge.txt
    rm ~/Documents/FireRemote/PLfinal.txt
    
    open ./pcopy.app
    This does all the asking youtube for links, grabing the title and id from the result file, and creating the content of a well formed .m3u file (which it copies to the clipboard in the end, so you can easily use or verify that on the PC side as well). In the last line it opens another app, thats then responsible for just creating and pushing the .m3u file to Kodi, the same way as in script 2. above. The code for that is the following:

    (MacOS) Script-Editor code, save as pcopy.app in the ~/Documents/FireRemote/ folder
    Code:
    tell application "Terminal"
        do script "~/Documents/FireRemote/pcopyh.sh > /dev/null 2>&1 &" in window 1
    end tell
    shellscript code, save as pcopyh.sh in the ~/Documents/FireRemote/ folder:
    Code:
    eval $(history | cut -c 8- | grep "adb connect [0-9]" | tail -1)
    
    # adb devices
    
    sleep 0.5
    
    set eof ~/Documents/FireRemote/zwischenablagep.m3u to 0
    
    pbpaste | sed 's,https://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,https://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,https://,http://,g'| pbcopy
    pbpaste | sed 's,&start=0,,g'| pbcopy
    
    pbpaste > ~/Documents/FireRemote/zwischenablagep.m3u
    adb push ~/Documents/FireRemote/zwischenablagep.m3u /sdcard/
    
    sleep 2
    
    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/zwischenablagep.m3u"}},"id":1}' "${XBMC_HOST}/jsonrpc"
    
    # adb devices
    
    adb disconnect
    
    killall Terminal
    This one has two prerequisites, because as stated before it is based on youtube-dl and jq

    On MacOS download and install brew from here:
    https://brew.sh/
    Once installed run

    brew install jq
    in a terminal window to install jq

    and

    brew install youtube-dl
    in a terminal window to install youtube-dl

    As with the normal "push video link to Kodi" script, 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.

    Thats it. It works on my end. ;) Theres nothing MacOS specific in the shell scripts except for frequent use of pbcopy and pbpaste (and one killall Terminal in the end), which are MacOS command line tools for copying and pasting stuff into and out of the clipboard. On Linux you'd have to replace those. The rest should be platform agnostic. :)
     
    Last edited by notimp, Jan 11, 2020
  5. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Edited in a small fix to the aafmtrigger script code.

    If you install adb using the brew command above scripts that get run via "do shell script" without addressing terminal first (tell terminal), get run under a different user, that doesnt have access to adb. This impacted an adb disconnect call. Took a while to catch that. (Most Android Boxes allow unlimited adb connections, some only one. To always exit an adb session is only important for the latter one.)

    Another solution to this is to copy (or symlink) the adb binary to both /usr/bin and /usr/local/bin

    I mention it separately, because there is one other script in here (force close and launch android apps from the Mac), where this might conflict. And thanks to Apples new security policy (Mojave and newer), calling Terminal from a script first is another Security nag popup. So I've written them with an eye on preventing them if possible... I'll eventually fix the mentioned script as well.
    --

    And a new one:

    Script to connect to an Android box, have it capture a screenshot, transfer the screenshot back to your desktop, rename them in case there are others there already, and then delete the image on the android box.

    (MacOS) Script-Editor code, save as an app with a name of your choosing:
    Code:
    tell application "Terminal"
        do script "~/Documents/FireRemote/connecth.sh && /bin/sleep 2.2 && adb shell screencap -p /sdcard/screencap.png && adb pull /sdcard/screencap.png ~/Desktop/screencap.png && adb shell rm /sdcard/screencap.png && adb disconnect && /bin/sleep 0.5 && pkill -a Terminal"
    end tell
    set theFolder to alias ((path to desktop as text))
    tell application "Finder"
        delay 6
        set k to 0
        repeat with thisFile in (get document files of theFolder that name contains "screencap" and name extension is "png")
            set k to k + 1
            get name extension of thisFile
            if name of thisFile contains "screencap" then
                set name of thisFile to "screencap " & k & "." & result
            end if
        end repeat
    end tell
    Needs the connecth.sh helper script (bash script) from the first posting in the ~/Documents/FireRemote/ folder (made executable with chmod +x ). :)
     
    Last edited by notimp, Mar 22, 2019
  6. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Quick fixed the "if terminal is not addressed in a Script editor (Apple script) script first, adb will get run by a user that doesnt have it available as a global variable" thing by selectively replacing adb with /usr/local/bin/adb in some isolated cases.

    This means, that all scripts now run in macOS 10.14.5 - doing just the brew install of

    brew install homebrew/cask/android-platform-tools

    (and the respective install of other dependencies, as stated for specific scripts)

    Which means, that I've done everything to make everyone of those scripts work out of the box for every Mac user on an up to date version of macOS. :)
    -


    If you have a Mac (a Macbook preferably just for ergonomics ;) ) and you use it while watching TV/movies on an Android box - I highly urge you to try to get into this and get some of the scripts in here running.

    Its now really as simple as following the instructions in this thread.

    Script Editor is a program you already have on your Mac.

    Shell Scripts can be written (/ copy/pasted and saved) in TextEdit - or any text editor on your mac (I prefer TextWrangler).

    To make shell scripts executable, all that is needed is chmod +x filename.sh in the terminal once you've navigated into the folder you saved the scripts in ( ~/Documents/FireRemote/ usually. You navigate into that folder by typing cd ~/Documents/FireRemote/ into terminal. ;) ). Thats a one time thing for each script.

    The only prerequisite of stuff you have to have installed on your Mac to start out is brew and adb (android-plattform-tools), through brew. And I've already written step by step directions for you on how to do that in the postings in this thread.

    All other dependancies can be installed through brew - or are one time downloads from github you have to simply put into a specific folder.

    This stuff isn't complicated to get running - even if it might look like it - is what I'm hinting at. ;)

    Again - its fun to surf by websites - look at a video stream, and if you want to - throw it onto your big screen device running an android box - within two seconds. (Which the android box then plays independently from whatever you choose to do on your Mac afterwards.) I'm just watching Giant Bombs live E3 press conferences that way f.e. - once its set up, two seconds, bam jam, thank you mam. ;) But even thats shortselling it - because I use those scripts every day. With all different kinds of content. They just work - and they are reliable.

    macOS 10.14.5 (latest) is the only compatibility prerequisite you have to think about before starting out in here currently. (Would work with some rewrites on versions as low as 10.6 - which is what I was using them on before switching over to a new Macbook and rewriting them to the versions you see in here.)

    When running the scripts, you'll get a bunch of permission check messages in new versions of macOS - which is part of macOS' new security concept. If they dont go away over time - you can stop them from popping up by adding them manually to Systemsettings > Security > privacy >automation - as seen in this screenshot.
    [​IMG]

    Some of the ones also using finder permissions to change filenames for example might also need Accessibility permissions:
    [​IMG]
    But start without that - and only give it to them, if the permission popups dont stop on their own. This permission thing on scripts is a system that apple introduced, and refined with the most current updates of 10.14.5 - and by now they've got it working - so if you grant permissions, the popups will stop. ;)
     
    Last edited by notimp, Jan 1, 2020
  7. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Direct video links of movies you see in Chrome or Firefox on your Mac you can get with a simple extension like

    Video DownloadHelper

    f.e. - which can copy the video link into your clipboard - at which point the scripts can take over (be run by you).

    For people to get into this easily - I have to mention that as well. ;)

    Usually all video links work (with script 2.). (About 90% of them.) As video on the internet by now is still largely based on non DRMed mp4 or HLS streams. And Kodi can take and run with those no problem. :)
     
    Last edited by notimp, Jun 11, 2019
  8. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    IP address switcher scripts (the one listed as 1.) would not have the adb command picked up by terminal history reliably.

    Fixed by doubling down on the entry scheme.

    Now they work reliably.
     
    Last edited by notimp, Jun 13, 2019
  9. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Lets say Kodi isnt capable of playing back 4K youtube videos due to API constraints, here is a small Applescript to take any link in your clipboard (where the copy/paste stuff resides) on the Mac and push it to the Youtube TV app on your android box.

    Which then starts playing it in the youtube app. :)

    Code:
    set link to the clipboard
    
    tell application "Terminal"
    do script "eval $(history | cut -c 8- | grep 'adb connect [0-9]' | tail -1) && adb shell am start -n com.google.android.youtube.tv/com.google.android.apps.youtube.tv.activity.MainActivity '" & link & "' && adb disconnect && killall Terminal"
    end tell
    Code can just be saved as an .app with Script-Editor.

    adb has to be set up first, like with any of the other scripts. Your terminal history has to be populated with a adb connect ip.add.re.ss input in the recent past (connects to the last ip it sees that you have connected to via adb).

    This specifically launches the leanback version of the youtube app, which you might have to install onto your device first ( https://www.apkmirror.com/apk/google-inc/youtube-for-android-tv-android-tv/ )

    If you want to launch any other app via adb - and feed it launch parameters (in this case just the link) - you cant do that using
    monkey -p your.app.package.name -c android.intent.category.LAUNCHER 1 - you have to use

    start am

    with the main activity for that app (as you see in the example above), to get the main activity of any app quickly, simply open that app on your android device - and then type the following adb command into your command line.

    adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'

    (Use adb connect ip.add.re.ss to connect first of course.)
    This will show you the main activity of the app that is open and active on your android device.

    I bet there are many apps that would instantly play/load a link they might recognize, if that is fed to them as the only launch parameter. :) You could easily modify the script above for that as well. :)
     
    Last edited by notimp, Aug 10, 2019
  10. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Have become even more lazy and am now automating my frequently used TV actions with MacOS. :)

    Bought a Program from the App store that can interface with LG TVs. The Program left out labels for some buttons, so cant be easily automated using automator and record action - so I had to find another way. At least it always starts at the same position on screen, and autoconnects to my TV.

    Script-Editor code (save as an .app with a name of your liking):
    Code:
    tell application "Smart Remote" to activate
    delay 6
    tell application "System Events"
        tell process "Smart Remote"
            -- range is from 0.0 to 1.0, so to scroll halfway you would use 0.5
            set value of scroll bar 1 of scroll area 1 of front window to 0.5
        end tell
    end tell
    do shell script "MouseTools -x 873 -y 433 && MouseTools -leftClick && MouseTools -x 543 -y 253 && MouseTools -leftClick && sleep 1 && MouseTools -x 765 -y 144 && MouseTools -leftClick && MouseTools -x 765 -y 210 && MouseTools -leftClick && sleep 4 && MouseTools -x 836 -y 216 && MouseTools -leftClick && sleep 0.5 && MouseTools -x 765 -y 210 && MouseTools -leftClick && sleep 4 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.8 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.8 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.8 && MouseTools -x 765 -y 210 && MouseTools -leftClick && sleep 0.8 && MouseTools -x 765 -y 210 && MouseTools -leftClick && MouseTools -x 890 -y 325"
    
    Prerequisites: Install a third party app called MouseTools you can get from here:
    http://www.hamsoftengineering.com/codeSharing/MouseTools/MouseTools.html (place it in /usr/bin and /usr/local/bin to have command line access to it by simply writing its name into a command line, or shell script - which the script above presumes.)

    And voila - I'm in the brightness settings menu of my TV and have the slider selected, which I now can control with left/right. On my Mac. ;)

    (Have a Logitech Harmony remote as well, but this is better. ;) )
     
    Last edited by notimp, Aug 24, 2019
  11. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Here is the App I'm using to interface with my LG TV (give the dev a few bucks, if you are interested - its good enough. :) Not ugly either.. :)): https://apps.apple.com/us/app/smart-remote-for-lg-smart-tv/id1266719569?mt=12

    And here are some other menus the workflow opens on an LG TV:

    (In the example above it was the brightness submenu.)

    Expertssettings Menu:
    Code:
    tell application "Smart Remote" to activate
    delay 6
    tell application "System Events"
        tell process "Smart Remote"
            -- range is from 0.0 to 1.0, so to scroll halfway you would use 0.5
            set value of scroll bar 1 of scroll area 1 of front window to 0.5
        end tell
    end tell
    do shell script "MouseTools -x 873 -y 433 && MouseTools -leftClick && MouseTools -x 543 -y 253 && MouseTools -leftClick && sleep 1 && MouseTools -x 765 -y 144 && MouseTools -leftClick && MouseTools -x 765 -y 210 && MouseTools -leftClick && sleep 4 && MouseTools -x 836 -y 216 && MouseTools -leftClick && sleep 0.5 && MouseTools -x 765 -y 210 && MouseTools -leftClick && sleep 4 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.7 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.7 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.7 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.7 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.7 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.8 && MouseTools -x 768 -y 283 && sleep 0.8 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.8 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.8 && MouseTools -x 765 -y 210 && MouseTools -leftClick && MouseTools -x 890 -y 325"
    Picturesettings Menu:
    Code:
    tell application "Smart Remote" to activate
    delay 6
    tell application "System Events"
        tell process "Smart Remote"
            -- range is from 0.0 to 1.0, so to scroll halfway you would use 0.5
            set value of scroll bar 1 of scroll area 1 of front window to 0.5
        end tell
    end tell
    do shell script "MouseTools -x 873 -y 433 && MouseTools -leftClick && MouseTools -x 543 -y 253 && MouseTools -leftClick && sleep 1 && MouseTools -x 765 -y 144 && MouseTools -leftClick && MouseTools -x 765 -y 210 && MouseTools -leftClick && sleep 4 && MouseTools -x 836 -y 216 && MouseTools -leftClick && sleep 0.5 && MouseTools -x 765 -y 210 && MouseTools -leftClick && sleep 4 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.7 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.7 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.7 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.7 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.7 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.7 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.8 && MouseTools -x 768 -y 283 && sleep 0.8 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.8 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.8 && MouseTools -x 765 -y 210 && MouseTools -leftClick && MouseTools -x 890 -y 325"
    Energysavings Menu - turn the screen off, or low while listening to music, then turn it back to where you want it after:
    Code:
    tell application "Smart Remote" to activate
    delay 6
    tell application "System Events"
        tell process "Smart Remote"
            -- range is from 0.0 to 1.0, so to scroll halfway you would use 0.5
            set value of scroll bar 1 of scroll area 1 of front window to 0.5
        end tell
    end tell
    do shell script "MouseTools -x 873 -y 433 && MouseTools -leftClick && MouseTools -x 543 -y 253 && MouseTools -leftClick && sleep 1 && MouseTools -x 765 -y 144 && MouseTools -leftClick && MouseTools -x 765 -y 210 && MouseTools -leftClick && sleep 4 && MouseTools -x 836 -y 216 && MouseTools -leftClick && sleep 0.8 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.8 && MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.8 && MouseTools -x 765 -y 210 && MouseTools -leftClick && MouseTools -x 890 -y 325"
    Just to get your inspiration going. :)

    (The click coordinates are for a 13" Macbook (/Air) with retina Display. The LG remote app always launches in the same position. :) )

    edit: replaced the examples in this and the previous posting with fully functional Script-Editor code. Added a tiny bit more delay to make them work reliably.
     
    Last edited by notimp, Aug 24, 2019
  12. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Play any direct videolink from an Android smartphone, to any other Android device running Kodi.

    (Hand the link over, and let Kodi on the Android TV box play it - to be exact. :))

    Clarification, when using Android TV in the text below, I mean any device running Android, that is connected to your TV. Not an 'AndroidTV' device specifically. (Its a thing.)

    Prerequisites:

    On the phone:
    - Android Smartphone rooted with Magisk
    - Magisk module 'Curl' installed
    - Automate installed ( https://play.google.com/store/apps/details?id=com.llamalab.automate )
    - Terminal emulator installed ( https://play.google.com/store/apps/details?id=jackpal.androidterm )

    To get direct videolinks:
    - Kiwi browser (which allows you to use real chrome plugins on your Android device) installed ( https://play.google.com/store/apps/details?id=com.kiwibrowser.browser )
    within Kiwi browser Video Download Helper installed (surf to https://chrome.google.com/webstore/detail/video-downloadhelper/lmjnegcaeklhafolokijcfjliaokphfk and install from there)
    - Video Dowload helper set up to copy video url as default behavior. (Visit any site with a video embedded in it (f.e. any kickstarter.com project site), then scroll down on the sites setting and click on Video Download Helper at the bottom), there you should see the video name and its size. Long click on the rightmost part of that infobox line and an arrow should appear, click it again and it opens a menu with more options for the embedded file, scroll down to the bottom, tick 'use this action as standard' then scroll up and press on 'copy url'. You only have to do that once.)

    On the Android TV Box:
    - Any Android ftp server you like and would want to use installed.
    - Kodi installed and set up. ( https://play.google.com/store/apps/details?id=org.xbmc.kodi )

    Launch each app once, so that they are set up, before we begin.

    Kodi setup on the Android TV Box is the same as in the Mac/Linux script:
    -

    Now - this is less sophisticated than the adb variant above (because android cant run adb natively), so we are relying on ftp to push our playlist file to the Android TV device (has to have an ftp server running), and we rely on a fixed IP address for the Android TV device running Kodi, which we enter in manually. Also Kodi on the TV has to be opened already, since we dont use adb, we cant open it easily from the phone.
    -

    The main app we use is Automate. The workflow looks as follows:

    [​IMG]

    To get the workflow going, lets modify an existing one. :) Download this one https://llamalab.com/automate/community/flows/2892

    Then delete all steps below the ones you see in the screenshot above - those will make the workflow loop, which we dont need. :)

    Also make sure, that in the first AND second 'Write file' step you untick 'append' (we want the file to be overwritten every time), and set the action trigger in 'Get Clipboard' from 'when changed' to 'immediately'.

    Then create a file named

    zwischenablage.m3u

    on the internal storage of your phone (top level which is called /sdcard in the Android naming scheme), and chose that file to be written to in all three 'Write file' actions. In the third 'Write file' action, append needs to remain checked.

    Now we can continue setting up the automation flow.

    [​IMG]

    We add all actions you see in the second screenshot one after the other, to the workflow we've already created.

    Also notice, that there are small blue lines connecting the actions. you have to create those with your finger - connecting every new action you add to the previous one.

    The first one we add is a start shortcut action, which is where we rely on Terminal emulator first. Add the start shortcut action, choose Terminal Emulator, and the shortcut box for Terminal Emulator will pop up.

    Leave the first line empty. Copy the following code into the second line:
    Code:
    sed -i -e 's,https://www.youtube.com/watch?v=,plugin://plugin.video.youtube/play/?video_id=,g' /sdcard/zwischenablage.m3u && sed -i -e 's,https://youtube.com/watch?v=,plugin://plugin.video.youtube/play/?video_id=,g' /sdcard/zwischenablage.m3u && sed -i -e 's,https://youtu.be/,plugin://plugin.video.youtube/play/?video_id=,g' /sdcard/zwischenablage.m3u && sed -i -e 's,&export=download,/preview,g' /sdcard/zwischenablage.m3u && exit
    Then touch the third line - which will automatically populate it with the title 'sed' you can change that, but you can also leave it as 'sed'. This is the step, where we replace some parts of the link in case it is a youtube link - which for Kodi has to be formated with a plugin://plugin.video.youtube prefix so it will play. So this step is simply search and replace in the file our clipboard (where stuff you copy paste resides :) ) got saved to.
    -

    The next action we add is the ftp upload action, where we upload the file on our phone from /sdcard/zwischenablage.m3u to the Android TV device, also at /sdcard/zwischenablage.m3u - this is simply entering the Android TV devices IP, the ftp username and password, and pointing the action to the source file (zwischenablage.m3u), and the destination path.

    Also - at this point you will have to grant Automate some more permissions. In my case - I just gave it superuser rights, and allowed all, in addition to granting it network access permission.

    In any case - save the workflow (checkmark at the very top) at this step, and try it out with something in your clipboard (f.e. write testtest in a browser searchbar on your phone, and copy that - so you have it in the clipboard), and make sure, that a file called zwischenablage.m3u lands at /sdcard/zwischenablage.m3u on your Android TV (and that it contains testtest).

    If not - doublecheck steps, until this happens. :)
    --

    The next action we add simply is a 1 second delay.
    -

    The next action we add is the curl command to stop any movie playing in Kodi already.

    We do that by adding another start shortcut action, we point to Terminal emulator again. Same as above, leave the first line in the Terminal Emulator popup empty, then add the following into the second line:

    Code:
    curl --header "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"Player.Stop","params":{"playerid":1},"id":1}' "192.168.1.108:8087/jsonrpc" && exit
    Attention!

    Change the 192.168.1.108 in the example above to the IP address of your Android TV device running Kodi, or it wont work.

    Then click on the third line in the Terminal Emulator popup, and it will be populated with curl - which I changed to 'kodi stop', but again - this is just the name for that action and optional.
    -

    The next action we add is another simple 1 second delay.
    -

    And the final action we add is the curl command that tells Kodi to start playing the playlist file at /sdcard/zwischenablage.m3u on the Android TV device.

    We do that by adding another start shortcut action, we point to Terminal emulator again.

    We leave the first line of the popup empty, and enter the following in the second line:
    Code:
    curl --header "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"Player.Open","params":{"item":{"file":"/sdcard/zwischenablage.m3u"}},"id":1}' "192.168.1.108:8087/jsonrpc" && exit
    Attention!

    Change the 192.168.1.108 in the example above to the IP address of your Android TV device running Kodi, or it wont work.

    Then click on the third line in the Terminal Emulator popup, and it will be populated with curl - which I changed to 'kodi zwischen', but again - this is just the name for that action and optional.

    And thats it. Name the first action (Flow beginning) with something descriptive like url2kodi (as seen in the first screenshot). Save the entire Automation flow (checkmark at the top), and leave Automate.

    Create an app shortcut anywhere on your smartphones Desktop, chose Automate, and it will then allow you to pick your flow, and the starting tile (we named url2kodi).
    --

    Get any direct video link from the Video Download Helper Plugin in Kiwi Browser, or any youtube link from the youtube app (share > copy link), then click on the Automate shortcut we've just created, and the video will start playing in Kodi, on another Android device - on our TV.

    Thanks to Video Download Helper and Kodi, this works with pretty much any webvideo (that isnt DRM encrypted).

    Have fun. :)

    (The shell code execution option in Automate cant handle multiple instances of quotation marks, which is why we have to use Terminal Emulator in addition to Automate, btw. There is a reason. ;) )

    edit: Your smartphone and the Android TV box should be connected to the same Network (unless you are able to set up your ftp server via DNS entry. :)), so that transferring the playlist via ftp and the curl commands work. :)

    edit2: Here is what the final process then looks like on the phone -

    - on the Android TVs side - well, the video just starts playing. :)

    edit3: If your phone isn't as fast as mine, you might have to increase the second delay. Or not. ;)
     
    Last edited by notimp, Sep 27, 2019
  13. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Replaced Yandex with Kiwi Browser in the tutorial above. Its more light weight, cleaner, and also allows you to use real chrome browser plugins. :)

    edit: Fixed an error in which line to put the third codeboxes content.
    Rewrote the tutorial in the previous posting slightly for clarity. :)

    edit2: Added a video in the posting above to showcase how the process looks on the phones side, once it set up. :)
     
    Last edited by notimp, Sep 27, 2019
  14. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Finally also created some HDMI switching scripts for Mac to switch inputs on an LG TV using the Smart Remote App mentioned above, as well as MouseTools (also mentioned above).

    Here is an example for 'switch to HDMI4'
    Code:
    tell application "Smart Remote" to activate
    delay 6
    tell application "System Events"
    tell process "Smart Remote"
    -- range is from 0.0 to 1.0, so to scroll halfway you would use 0.5
    set value of scroll bar 1 of scroll area 1 of front window to 0.5
    end tell
    end tell
    do shell script "MouseTools -x 773 -y 433 && MouseTools -leftClick && MouseTools -x 543 -y 253 && MouseTools -leftClick && sleep 3 &&
    MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.5 &&
    MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.5 &&
    MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.5 &&
    MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.5 &&
    MouseTools -x 765 -y 215 && MouseTools -leftClick && sleep 1 && MouseTools -x 840 -y 215 && MouseTools -leftClick && sleep 1 && MouseTools -x 765 -y 215 && MouseTools -leftClick && MouseTools -x 890 -y 325
    "
    delay 1
    tell application "Smart Remote"
    quit
    end tell
    for HDMI3 remove one line of
    MouseTools -x 768 -y 283 && MouseTools -leftClick && sleep 0.5 &&

    for HDMI2 two lines of that, for HDMI1 three lines of that.

    Those just correspond to down inputs, so you are removing one, by removing one of those lines.

    First time you run the Script Editor app written above, it needs accessibily permissions (System Settings/Security/Accessibility), since it wants to control the mouse cursor. :)
     
    Last edited by notimp, Dec 31, 2019
  15. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    To control the entire android box from Macos, I currently use Cetus Play ( https://play.google.com/store/apps/details?id=com.cetusplay.remotephone&hl=en_US ), on Bluestacks Android emulator (on Macos).

    I've installed Bluestacks for TTS purposes, so it was easy for me to fall back on that as an impromptu solution when needed (Kodi I still control via xbev (more info on that at the end of this posting). To lauch bluestacks and Cetus Play takes a few dozen seconds, so its not something for daily use - but in my case I dont need that as a usecase daily (I basically puzzled the solution together, while my bluetooth remote was charging... ;) ).

    For Cetus play to connect - it needs to be in ram on the Android box, so to make sure thats guaranteed, when you need it - I also wrote a small Macos (Script Editor) script, to launch it via adb.

    Code:
    tell application "Terminal"
    do script "eval $(history | cut -c 8- | grep 'adb connect [0-9]' | tail -1) && adb shell am start -n com.cetusplay.remoteservice/com.wukongtv.wkhelper.MainActivity && adb disconnect && killall Terminal"
    end tell
    
    edit: I've also personally added it to my multilauncher script (see above: [6. Force stop apps on the Android IoT device, then start (same/other) apps remotely.]).

    Additional information on using xbev (I've already documented how to set it up in one of the threads above). On current Kodi builds, the enter and back key (enter and esc) dont work as supposed anymore - out of the box because of a control conflict in Kodi, but thats easily fixed, by editing Kodis keyboard.xml file - as described here:
    https://github.com/xbmc/xbmc/issues/15772#issuecomment-479827661

    After that xbev works again as its supposed to. :)

    The xbev launch script is the only 'app', that I actually saved as a script and linked to a keyboard shortcut (ctrl+K (on Mac)), so for me its very easy to get in and out of it at will.
    --

    Also - if you want to remove the pause icon on Kodi, while pausing on a movie, after a few seconds, read this: https://forum.kodi.tv/showthread.php?tid=318757

    You can achieve that with a small edit in your skin file (still using confluence (Kodi skin), so doing that was quite straight forward. :). Switch skins to reload the skin, once edited. :)
     
    Last edited by notimp, Jan 2, 2020
  16. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Oh, and if you are interested to control your Kodi setup on the android box through Google home, read this:

    Its pretty neat, free to set up and works as expected (needs an open port (port forwarding) and static IP on your router):

    https://forum.kodi.tv/showthread.php?tid=314096

    (Use Glitch, use IFTTT for free and easy. Read the last pages in the thread for a few additional automation tips. :) )
     
  17. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    images moved to the first posting
     
    Last edited by notimp, Jan 17, 2020
  18. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Wrote a strange script today. ;)

    Remote Beta

    This is a script (to be saved as an .app with Script-Editor on a Mac), that highjacks the modifier keys on a Mac (Shift, Fn, ctrl, option, command and capslock) and uses them to send key input codes (up, down, left, right, enter, back, home) to an Android box under your TV.

    Its basically what I'd recommend you'd do with Cetusplay, but as a script, so you dont have to start Bluestacks (Android emulator for Mac).

    Now - I could only figure out to get readings on what modifier key was pressed via the shell, not any other keys, so those are the keys we are limited to in this script. ;) On the plus side, this also means, that accidental inputs on the OS level (MacOS) are rulled out, because you are only highjacking modifier keys.

    The biggest negative is, that I had one key to few to map up, down, left, right, enter, back, home - as inputs for Android, so I had to map "up and then left" on one key. This leads to oddball, but functional behavior, while navigating. ;)

    Keymappings are:

    press shift for up/left
    press fn for down
    press control for right
    press option for enter
    press command for back
    press capslock for home

    To exit that mode where your Macs keyboard inputs get highjacked, you have to close (red dot clicking using your mouse) all terminal windows that are active on your system. The script checks for that happening, and then terminates itself. :)

    Also as most of the other adb based scripts, it connects to the last IP address that you've connected to via adb connect in terminal (looks for that last adb connect entry in terminal history).

    It also needs accessibility permissions (Systemsettings/Security/Accessibility) as, yes it highjacks your keyboard inputs (as long as the script is running). :)

    So yeah. This one is a bit weird in terms of buttonlayout, but functional. :)

    Have fun:

    Code:
    -- bit 16 = CAPS LOCK
    -- bit 17 = shift (131072)
    -- bit 18 = ctrl (262144)
    -- bit 19 = option (524288)
    -- bit 20 = command (1048576)
    -- bit 23 = fn laptop key (8388608)
    use framework "Cocoa"
    tell application "Terminal"
        activate
        delay 2
        do script "clear" in window 1
        do script "eval $(history | cut -c 8- | grep 'adb connect [0-9]' | tail -1)" in window 1
        do script "echo $'\\npress shift for up/left\\npress fn for down\\npress control for right\\npress option for enter\\npress command for back\\npress capslock for home\\n'" in window 1
    end tell
    on getModifierKeyBits()
        tell application "System Events"
            if not (exists (window of process "Terminal")) then
                tell application "Terminal"
                    activate
                    do script "adb disconnect && killall Terminal"
                    do script "adb disconnect" in window 1
                    error number -128
                end tell
            end if
        end tell
        set modifierBits to current application's NSEvent's |modifierFlags|()
        set modifierBits to modifierBits * 1
        if (modifierBits > 0) then
            if (modifierBits > 8388607) then
                -- fn key is pressed, subtract it away
                set modifierBits to modifierBits - 8388608
                tell application "Terminal"
                    do script "echo 'close terminalwindows 2exit' && /usr/local/bin/adb shell input keyevent 20" in window 1
                end tell
            end if
            if (modifierBits > 1048575) then
                -- command key is pressed, subtract it away
                set modifierBits to modifierBits - 1048576
                tell application "Terminal"
                    do script "echo 'close terminalwindows 2exit' && /usr/local/bin/adb shell input keyevent 4" in window 1
                end tell
            end if
            if (modifierBits > 524287) then
                -- option key is pressed, subtract it away
                set modifierBits to modifierBits - 524288
                tell application "Terminal"
                    do script "echo 'close terminalwindows 2exit' && /usr/local/bin/adb shell input keyevent 66" in window 1
                end tell
            end if
            if (modifierBits > 262143) then
                -- ctrl key is pressed, subtract it away
                set modifierBits to modifierBits - 262144
                tell application "Terminal"
                    do script "echo 'close terminalwindows 2exit' && /usr/local/bin/adb shell input keyevent 22" in window 1
                end tell
            end if
            if (modifierBits > 131071) then
                -- shift key is pressed, subtract it away
                set modifierBits to modifierBits - 131072
                tell application "Terminal"
                    do script "echo 'close terminalwindows 2exit' && /usr/local/bin/adb shell input keyevent 19 && /usr/local/bin/adb shell input keyevent 21" in window 1
                end tell
            end if
            if (modifierBits > 65535) then
                -- capslock key is pressed, subtract it away
                set modifierBits to modifierBits - 65536
                tell application "Terminal"
                    do script "echo 'close terminalwindows 2exit' && /usr/local/bin/adb shell input keyevent 3" in window 1
                end tell
            end if
        end if
    end getModifierKeyBits
    repeat
        set answer to getModifierKeyBits()
    end repeat
     
    Last edited by notimp, Jan 2, 2020
  19. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Wrote scripts for a case where an online video source just offers you a link to a .m3u8 file (a playlist, which has individual movie segment files listed in it), but where the urls of those individual files were relative (lacking the entire link).

    This would result in Kodi not playing the movie files, because it lacked the context, from where the playlist was sourced.

    So the scripts now take the direct video (.m3u8) url, download the .m3u8 playlist to a temporary file, then remove the filename of the url in the clipboard (in my case a static index.m3u8 - if it is different in your case - you have to change that), so that only the "releative link part" remains, then add that to every video file in the playlist (in the MacOS example by replacing a static part of a filename (index) thats already in the .m3u8 file - or in the android case inserting it at the start of every line (later only the lines that dont have #EXT in it have the inserted link remaining in the android case), then pushing the file to the android box and stoping any video playing, and then starting the pushed playlist.

    MacOS Script-Editor code (save with a filename of your choice):
    Code:
    tell application "Terminal"
    do script "rm ~/Documents/FireRemote/look.m3u8"
    delay 0.2
    do script "curl `pbpaste` >> ~/Documents/FireRemote/look.m3u8" in window 1
    do script "pbpaste | sed 's,index.m3u8,,g' | pbcopy" in window 1
    do script "pbpaste | sed 's,\\/,ä,g' | pbcopy" in window 1
    do script "pbpaste | sed 's,\\.,ö,g' | pbcopy" in window 1
    do script "pbpaste | sed 's,\\:,ü,g' | pbcopy" in window 1
    do script "pbpaste | tr -d '\\n' | pbcopy" in window 1
    delay 0.5
    set variable to the clipboard
    do script "sed 's/index/" & variable & "index/g' < ~/Documents/FireRemote/look.m3u8 > ~/Documents/FireRemote/look.txt" in window 1
    delay 0.2
    do script "rm ~/Documents/FireRemote/look.m3u8" in window 1
    do script "sed -i '' 's,ä,\\/,g; s,ö,\\.,g; s,ü,\\:,g;' ~/Documents/FireRemote/look.txt" in window 1
    delay 0.5
    do script "mv ~/Documents/FireRemote/look.txt ~/Documents/FireRemote/look.m3u8" in window 1
    do script "eval $(history | cut -c 8- | grep 'adb connect [0-9]' | tail -1)" in window 1
    do script "pbpaste | sed 's,ä,\\/,g' | pbcopy" in window 1
    do script "pbpaste | sed 's,ö,\\.,g' | pbcopy" in window 1
    do script "pbpaste | sed 's,ü,\\:,g' | pbcopy" in window 1
    do script "~/Documents/FireRemote/lcopyh.sh > /dev/null 2>&1 &" in window 1
    end tell
    This also needs a shell script called lcopyh.sh in the FIreRemote folder, thats based on the fcopyh.sh script thats alredy in the thread. Use that as a template remove everything between the first two delays (sed replacement lines), and then change the filename from zwischenablage.m3u to look.m3u8 in every instance. Save that as lcopyh.sh in your ~/Documents/FireRemote folder, and make it executable with chmod +x

    I might rewrite that with the code from the Android approach in the future, as it has fewer lines of code. :)


    Android App approach.

    Use the templet from the url2kodi Android Automator workflow above - in fact set up that workflow and then copy it entirely. :)

    Remove the last "Write file" step and replace it with a http request step, that has the first field filled out with

    = clip

    as a function (which means that it will download the link that is in the clipboard), and then set the step to download to a file named index.m3u8 on the top user storage directory of your phone (/sdcard). Leave the other options in this step at default settings.
    -

    Set the two "Write file" steps above the just created one to save to a file called look.txt, also in the top user storage directory of your phone (/sdcard).
    -

    Set the ftp step to push a file named look.m3u8 in /sdcard (the top user storage directory of your phone) to /sdcard/look.m3u8 on the Android box.
    _

    After that replace the first terminal emulator 'sed' step in the script with the following code (also to be entered in a terminal emualtor shortcut in line two as in the initial workflow):
    Code:
    sed 's/index\.m3u8//g' /sdcard/look.txt > /sdcard/reallook.txt && name=$(cat /sdcard/reallook.txt) && sed "s,^,$name,g" /sdcard/index.m3u8 > /sdcard/index2.m3u8 && sed 's/.*#EXT/#EXT/g; s/index.m3u8//g;' /sdcard/index2.m3u8 > /sdcard/look.m3u8 && rm /sdcard/index.m3u8 && rm /sdcard/index2.m3u8 && rm /sdcard/reallook.txt && rm /sdcard/look.txt && exit
    And the last step of the workflow (named kodi zwischen in the image above) (also a terminal emulator shortcut)
    with the following code:
    Code:
    curl --header "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"Player.Open","params":{"item":{"file":"/sdcard/look.m3u8"}},"id":1}' "192.168.1.108:8087/jsonrpc" && exit
    Replace 192.168.1.108:8087 with your own Kodi IP and port.

    Now you can directly launch those specific streaming movies, that come in .m3u8 playlists, with the absolute filepath lacking from the file - having automatically added the entire http(s):// path where appropriate.

    These scripts have to be altered a little more extensively depending on your needs, compared to all the others in this thread, so I'd recommend not starting out with them, but getting some of the others to work first, and then trying to tackle these two. :)

    hf

    n.
     
    Last edited by notimp, Jan 11, 2020
  20. notimp

    OP notimp Well-Known Member
    Member

    Joined:
    Sep 18, 2007
    Messages:
    3,836
    Country:
    Laos
    Added an index (and moved images) to the first posting.
     
    Last edited by notimp, Jan 17, 2020
Draft saved Draft deleted
Loading...

Hide similar threads Similar threads with keywords - [Release], interface, scripts