Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Linux: Mount android phone directories and open them in tabs

Last time I described the stolen script for opening multiple directories as tabs in Thunar. And now we will use that, to open the two useful directories from an mtpfs-mounted Android mobile phone.

At present, my solution depends on two shortcuts in the main application menu. Eventually I hope to write a udev rule (that works) to detect an mtp device connected, and automount and open the useful directories.

  • mount-mtp.desktop
  • umount-mtp.desktop

These files are pretty basic.

File /usr/share/applications/mount-mtp.desktop

[Desktop Entry]
Name=Connect phone and open photo dirs
Comment=After setting phone to "transfer files"
Exec=/usr/local/bin/mount-mtp.sh
Icon=phone
Type=Application
Terminal=false
Categories=System;Utility;

File /usr/share/applications/umount-mtp.desktop.

[Desktop Entry]
Name=Disconnect phone
Comment=Before unplugging phone
Exec=/usr/local/bin/umount-mtp.sh
Icon=phone
Type=Application
Terminal=false
Categories=System;Utility;

And obviously the script from last time, thunartab, which is described in the link at the top of this post.

Script /usr/local/bin/mount-mtp.sh

#!/bin/sh
# File: /usr/local/bin/mount-mtp.sh
# Author: bgstack15
# Startdate: 2022-08-21 16:22
# SPDX-License-Identifier: GPL-3.0
# Title: Mount and open phone camera directories
# Purpose: Make it easy to mount
# History:
# Usage:
#    run this from the .desktop file
# References:
#    https://wiki.archlinux.org/title/Udev#udev_rule_example
#    https://forums.linuxmint.com/viewtopic.php?t=244076
#    udevadm monitor --udev --environment
# Improve:
#    Add udev rule to run this on detection of an android or mtp device?
#    Add yad message if mtpfs is not mounted at the end
# Dependencies:
#   /usr/local/bin/thunartab
/usr/local/bin/umount-mtp.sh
if test "${USER}" != "user1" ;
then
    su user1 - mtpfs /home/user1/PHONE
else
    mtpfs /home/user1/PHONE
fi
:
if test "${USER}" = "user1" ;
then
    sleep 1 ; mount | grep -i PHONE && {
        thunar "/home/user1/PHONE/Internal shared storage/DCIM/Camera/" &
        sleep 10 ;
        thunartab "/home/user1/PHONE/SanDisk SD card/DCIM/Camera/" &
    }
fi

Script /usr/local/bin/umount-mtp.sh

#!/bin/sh
# Usage:
#    run this from the .desktop file
if test "${USER}" != "user1" ;
then
    su user1 -c 'fusermount -u /home/user1/PHONE'
else
    fusermount -u /home/user1/PHONE
fi
:

Comments