I was just wondering if there was a way to read the text displayed on a Android device's UI using python or any adb commands?
I have found a module for python called "UIAutomator : https://github.com/xiaocong/uiautomator", which can be used to dump the screen's contents, but it seems to be really slow and often unreliable in terms of connection to the android device.
I also found google's own UIAutomator tool, but I needed something that can be used via a python script or an adb command to read the onscreen text.
Any help would be appreciated.
Thanks in advance :)
I have done a bit of research and found out two ways to read the UI's contents.
Using AndroidViewClient's dump tool.
This is their project page : https://github.com/dtmilano/AndroidViewClient
This tutorial helped me set up AndroidViewClient : https://www.darpandodiya.com/code/setup-androidviewclient-windows/
Using this adb command :
This command saves the UI Heirarchy XML in the device's sdcard.
adb exec-out uiautomator dump
This gives the result in the console itself. adb exec-out uiautomator dump /dev/tty
Suppose I run this command in Android Studio terminal
D:\.android\sdk\platform-tools/adb shell input text 'Some Text'
How do I save it (as macro) for next time use After closing and opening Android Studio instead of retyping the full command?
I know in the current session we can use the UP/DOWN arrows, I am looking at something like macros to be saved and be permanent...
I also faced this problem and reduce my 80% time and effort to run shell command by creating .bat files with just one letter like d.bat etc (single letter for reducing keyboard interaction to lessen time).
If you have no idea about What is bat file? and How to deal with it? please check this answer.
Couple of questions, with all of them regarding batch code.
So my first question is that if I have a batch file, and it has commands in it, how would I do something like this in command prompt?
cd C:\User\username\Desktop\folder\
And in folder, there is a file called CMDTEST.bat. With this, I would want to use commands from there. In this file, it starts off by giving me a few choices. There are three, which are selected by typing i, for input, o, for output, a for ADB connection, and q for quit. My question is, how would I do something like this?
C:\Users\username\Desktop\folder\CMDTEST.bat -i
The goal of typing something like this, is it would directly go to the CMDTEST.bat file, and then execute the choice if 'i' was put in. Similarly, there would something for 'o', something for 'a', and something for 'q'. How would I go about accomplishing this?
My second question is how do I get the directory of a connected USB device? In my case, I have to get the directory of a connected tablet, which has files on it. I need to make all this run pretty quickly, so I need the directories with a simple command, like if I did the same thing as the above code block, but something like:
C:\Users\username\Desktop\folder\CMDTEST.bat -d
That would list all the usb devices connected, with their name as shown in Windows Explorer, and the path used to access the device. All of the devices would be android tablets, so theres no need for a root scheme or anything to that extent.
The third and final question I have is how would I check in an if statement, whether things were printed in a cmd Android ADB statement where I write:
C:\Users\username\Desktop\platform-tools(this is where the Android SDK is stored)
and then I would write
adb devices
which would print all available devices connected(all android devices). How would I check if things are printed from that, and if they ARE NOT, then continue to search for them in a loop, until at least one device is found, then when it is found, break out of the loop?
First question:
So my first question is that if I have a batch file, and it has commands in it, how would I do something like this in command prompt?
cd C:\User\username\Desktop\folder\
Use cd /d to change current drive as well as current directory. Combine that with the %USERPROFILE% environment variable to dynamically get to the currently logged in user's folder. You should end up with something like this:
cd /d %userprofile%\desktop
Second question:
My question is, how would I do something like this?
C:\Users\username\Desktop\folder\CMDTEST.bat -i
The goal of typing something like this, is it would directly go to the CMDTEST.bat file, and then execute the choice if 'i' was put in. Similarly, there would something for 'o', something for 'a', and something for 'q'. How would I go about accomplishing this?
Batch scripts recognize parameters as %1, %2, and so on. You could do something like this in your script:
if %1==a (
goto optionA
) else if %1 ==o (
goto optionO
) else if %1 ==q (
goto optionQ
) else (
goto end
)
:optionA
{do option A suff}
:optionO
{do option O suff}
:optionQ
{do option Q suff}
:end
echo No valid option was provided
Second Second question:
Next Question:
My second [next] question is how do I get the directory of a connected USB device?
I don't have the answer for you, but you might look at this other question on SO: Find USB Drive letter
Third and Final question:
The third and final question I have is how would I check in an if statement, whether things were printed in a cmd Android ADB statement. How would I check if things are printed from that, and if they ARE NOT, then continue to search for them in a loop, until at least one device is found, then when it is found, break out of the loop?
I'm not familiar with the function of abd devices and what or how it outputs. However, assuming it outputs to the screen you could probably pipe (|) the output to a FIND command or something to make your determination. As stated in the comments, you might have better luck trimming this out and asking it as a separate question.
I've been pulling photos from my android device to my linux OS like this:
$ adb pull <what-to-pull> <where-to-place>
In the future I would prefer to pull only the ones I don't alreay have.
What's the best way to do this?
Maybe I could put all the photos I've downloaded to the same folder and skip the ones with names that already exist in the folder I'm pulling from? How to do that?
Is that even the best way? Does an easier way to do this exist?
If so... how?
I'm on arch linux by the way, in case the distribution effects your suggested answer.
adb shell find "/sdcard/DCIM" -iname "*.jpg" | tr -d '\015' | while read line; do adb pull $line; done;
^that works well enough.
From here.
The adb-sync tool worked for me: https://github.com/google/adb-sync
Note that I had to make several changes to the source code to get it working for my use-case (invalid paths for Windows causing crash, Python version mismatch apparently, etc -- for details, see issues I commented in), but it ended up being the only way I was able to retrieve my files from a corrupted data partition.
(The adb pull of the whole directory would crash on various files, and I didn't want to manually have to delete each one then restart the whole transfer. With adb-sync [+my modifications] it would just fail that one file then continue.)
Regarding your question of having it only transfer new files, I believe adb-sync does that automatically if a file hasn't been changed. If you don't want it to re-transfer an existent file ever (ie. even if the file has been updated), I think that's what the flag mentioned here is for: https://github.com/google/adb-sync/issues/22
I am familiar with adb pull command. Is it possible to pull a file from android device/emulator and saving it in specified location in PC programmatically?
In C, on linux use system() to execute adb (perhaps by its full path) with the appropriate arguments. OSX is probably something quite similar.
On windows... CreateProcess() but that returns before the child is finished, so you may have to spend some time wandering around MSDN to figure out the equivelent of system()