Copy the text from the clipboard to Android devices via ADB


Hello! Have you encountered the desire to copy some text on a device lying next to it? I would like it to be as easy as copy-paste to the emulator - it is boring to type hands and not always convenient.


And what about the hotkey: you press it, and the text from the PC clipboard itself starts to be typed on the screen of your phone / tablet - it sounds good, right?


In this article we will talk about using adb as a text copying tool and how it can be made convenient.


If you are an experienced adb user, and you have your own script of this kind - I advise you to go to the implementation itself and share your thoughts on this in the comments.


What and why


We will make a small script that allows you to quickly type the contents of the clipboard on a real device:



This is useful if:



http://example.com:8080/foo 192.168.1.100 


 v3rY$ecUrEP@s$w0rD 


 Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute: 


 BUexKeGHiERJ4YYbd2_Tk8XlDMVEurbKjWOWvrY 

Of course, you probably don’t have to do it regularly. But from time to time I had cases where I had to manually enter something not very convenient for this on the device.


In the end, most often it was the test data or api settings, and the script, although it did not save 5 minutes, but did the work much nicer.


But as?


This can be done using ADB (Android Debug Bridge) . Probably, all developers and most of QA are familiar with it at least due to the ability to view logs inside Android Studio or directly, through adb logcat . If you have not used adb before, you can see an example of installing on macOS here.


We are interested in the adb shell input command, which allows for input, such as tap or swipe .


It also allows you to send text - it will start typing in the input field, which is now in focus:


 adb shell input text <text> 

If you enter spaces, they must be replaced with %s , and special characters should be spelled. Otherwise, nothing happens.


It should be noted that adb works only with the Latin alphabet, numbers and special characters from the ASCII nameplates, and input is somewhat limited:


  1. Does not work with characters of type ±§
  2. Does not work with line adb shell input keyevent 66 (But you can, for example, separately call line adb shell input keyevent 66 another adb adb shell input keyevent 66 (enter) command or as described here )
  3. Does not work with Cyrillic

There is an interesting workaround for entering such characters, perhaps it will be possible to fasten the clipboard to it and then print any text. True to the device, you will need to pre-install .apk with a keyboard.


Important: In the above described form of the adb command, it is assumed that one device is connected. If there are several, you can do the following:


1) Call the team for a specific device. Option -s
You can find out the device number using the adb devices command. Then use the number when calling the command:


 $ adb devices List of devices attached 023db62er9dd7d2b device $ adb -s 023db62er9dd7d2b shell input text qwe 

2) Call the command for a single device connected via usb - option -d :


 adb -d shell input text qwe 

3) Call a command on a single active emulator - option -e :


 adb -e shell input text qwe 

More details can be read here .


If you work with several devices and these cases about you, then correct the adb command accordingly.


Implementation


Consider in detail the solution for macOS, but for other systems there is also a way:



Linux solution

At one time, the guys from KODE ( Dima Suzdalev and Dima Haiduk ) made an excellent solution for Linux and shared it with me.


It works through the X11 buffer (if you have a Wayland , read below) - you select the text for the bet, and then press the hotkey that calls the script.


Add such a hotkey is easy, you need:


1) Put xclip


2) Add a script file


 #!/bin/bash adb shell input text `xclip -o` 

3) Write the path to the script in the Shortcuts settings for the keyboard


Thanks guys and great respect.


Important: The solution above works for X11 (Xorg). For Wayland, this solution is not relevant. I could not find a way to get the contents from the buffer in Wayland, judging by my search for such a possibility yet. Correct, if not right.


If you do not know what kind of environment you have, look here. Most likely, you have X11 and everything will work.


MacOS solution

For macOS linux, the solution did not come up, so I tried to make a similar script that would simplify the adb shell input text <text> call.


At once I will tell - work with sed is not obvious to me. I tried to put together one team and add a little different recurring replacements that would help correctly extract special characters.
If you figure out how to improve this script, it will be very cool!


It looks like this:


 source ~/.bash_profile adb shell input text $(pbpaste | sed -e 's/[_<>|&$;()\"]/\\&/g' -e 's/ /\%s/g' -e 's/!+/!/g') 

( source ~/.bash_profile added if adb works in a regular console, but through Automator (more on that later) adb is not recognized, for this you first need to pull up the path to adb - for example, registered in ~/.bash_profile .)



Works like normal adb shell input text <text> , but


  1. The source of the text is pbpaste - that is, macOS clipboard.
  2. sed processes the clipboard text.
  3. The _<>&$;()\" escapes: - & -> \&
  4. Spaces are replaced with a special character: ` -> % s`
  5. With an exclamation point, everything is difficult - if someone explains to me such a replacement ! on ! helps the team not to fall down - it will be cool.

Windows solution

Unfortunately ( or not ) on Windows, I did not try to do this. The most obvious option that comes to my mind is to adapt the solution and install Cygwin . This will allow you to have a convenient linux terminal on hand, which will probably come in handy.
You will need a sed package and dependencies to it, and cygutils-extra package (provides the command to get the contents of the clipboard - getclip to replace pbpaste )


The result will be very similar to the macOS solution:


 adb shell input text $(getclip | sed -e 's/[_<>|&$;()\"]/\\&/g' -e 's/ /\%s/g' -e 's/!+/!/g') 

In Windows 10, it is also possible to get a Linux terminal out of the box . I have not tried this option, but it should be similar to the solution with Cygwin.



Script in action


Simplify work


You can copy the script to the console each time or tamper with adb shell input text <something> , but this is not very convenient. Easier to make alias or assign hotkey.


About alias for the console

Here, the difficulty is that in alias itself you will need to still extract all $ and " to make it work. I haven’t done it yet, because the hotkey is more convenient for me. The truth is that I used this - alias adp='adb shell input text' which helped to type one word like adp example . If someone makes himself alias with a script, write it - attach it here.


About hot key that will run the script

If we talk about the Linux solution - it all depends on the distribution, but it is also not difficult.


Cygwin's Windows solution is the easy way.


Initially, the article was for internal use, so the method for macOS is described in more detail, you can see it below:


Method for macOS:

There are many options for how to do this, but Automator is installed by default - you can quickly make hot keys with it.


To begin, run Automator , select the document type Service :



Then we configure the service:


  • for service receives set no input
  • inside the Actions tab, select the action Run shell script


Now you can assign a hot key to the new service:



Everything, now copying on a device should work on a hot key.


True hotkey service will only work in applications where in the application menu there is a Services tab:



There is no such tab in the Zeplin for macOS application, so it does not work there. Perhaps other applications for using scripts can bypass this limitation, so far I have had enough way through Automator.


It is also worth considering that the hot key can intercept the same Google Chrome or another application and perform its action instead of the script.


That's all


I hope the article will be useful and will help you to simplify the solution of such problems in your work.

Source: https://habr.com/ru/post/412551/


All Articles