Take screenshot with a unique file name #15

Open
opened 2024-09-04 20:22:16 +02:00 by Benjamin_Loison · 3 comments
Bash script:
function take_screenshot()
{
    counter=0
    get_file_name() {
        echo screenshot_$1.png
    }
    while [ -f `get_file_name $counter` ]
    do
        ((counter++))
    done
    fileName=`get_file_name $counter`
    adb exec-out screencap -p > $fileName
    if [ ! -s $fileName ]
    then
        echo "Screenshot couldn't be taken!"
        # To avoid having an empty file.
        rm $fileName
    fi
}

Source: the Stack Overflow question 9964823

<details> <summary>Bash script:</summary> ```bash function take_screenshot() { counter=0 get_file_name() { echo screenshot_$1.png } while [ -f `get_file_name $counter` ] do ((counter++)) done fileName=`get_file_name $counter` adb exec-out screencap -p > $fileName if [ ! -s $fileName ] then echo "Screenshot couldn't be taken!" # To avoid having an empty file. rm $fileName fi } ``` </details> Source: [the Stack Overflow question 9964823](https://stackoverflow.com/q/9964823)
Author
Owner
adb exec-out screencap -h
Output:
usage: screencap [-hp] [-d display-id] [FILENAME]
   -h: this message
   -p: save the file as a png.
   -d: specify the display ID to capture (default: 4630947175487691137)
       see "dumpsys SurfaceFlinger --display-id" for valid display IDs.
   --hint-for-seamless If set will use the hintForSeamless path in SF

If FILENAME ends with .png it will be saved as a png.
If FILENAME is not given, the results will be printed to stdout.

so no forcing parameter.

Related to Benjamin-Loison/android/issues/70.

```bash adb exec-out screencap -h ``` <details> <summary>Output:</summary> ``` usage: screencap [-hp] [-d display-id] [FILENAME] -h: this message -p: save the file as a png. -d: specify the display ID to capture (default: 4630947175487691137) see "dumpsys SurfaceFlinger --display-id" for valid display IDs. --hint-for-seamless If set will use the hintForSeamless path in SF If FILENAME ends with .png it will be saved as a png. If FILENAME is not given, the results will be printed to stdout. ``` </details> so no forcing parameter. Related to [Benjamin-Loison/android/issues/70](https://github.com/Benjamin-Loison/android/issues/70).
Author
Owner

Related to Benjamin-Loison/android/issues/309#issuecomment-3502576023.

Could add function to ~/.bashrc.

Related to [Benjamin-Loison/android/issues/309#issuecomment-3502576023](https://github.com/Benjamin-Loison/android/issues/309#issuecomment-3502576023). Could add function to `~/.bashrc`.
Author
Owner
~/.bashrc:
# Being able to no write top and bottom, but still also have common code with a `take_screenshot` function would be nice.
take_app_screenshot()
{
    # Assuming Virtual Machine Manager LineageOS 22.2 virtual machine on my Debian 13 GNOME laptop.
    TOP_BAR_HEIGHT=28
    APP_HEIGHT=716
    
    counter=0
    get_file_name() {
        echo screenshot_$1.png
    }
    while [ -f `get_file_name $counter` ]
    do  
        ((counter++))
    done
    fileName=`get_file_name $counter`
    adb exec-out screencap -p | convert - -crop x$APP_HEIGHT+0+$TOP_BAR_HEIGHT $fileName
    if [ ! -s $fileName ]
    then
        echo "Screenshot couldn't be taken!"
        # To avoid having an empty file.
        rm $fileName
    fi
}
<details> <summary><code>~/.bashrc</code>:</summary> ```bash # Being able to no write top and bottom, but still also have common code with a `take_screenshot` function would be nice. take_app_screenshot() { # Assuming Virtual Machine Manager LineageOS 22.2 virtual machine on my Debian 13 GNOME laptop. TOP_BAR_HEIGHT=28 APP_HEIGHT=716 counter=0 get_file_name() { echo screenshot_$1.png } while [ -f `get_file_name $counter` ] do ((counter++)) done fileName=`get_file_name $counter` adb exec-out screencap -p | convert - -crop x$APP_HEIGHT+0+$TOP_BAR_HEIGHT $fileName if [ ! -s $fileName ] then echo "Screenshot couldn't be taken!" # To avoid having an empty file. rm $fileName fi } ``` </details>
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Benjamin_Loison/adb#15