Skip to content

Instantly share code, notes, and snippets.

@the-ge
Last active March 30, 2026 08:21
Show Gist options
  • Select an option

  • Save the-ge/083694fb2795c18b41b9007a1ad86753 to your computer and use it in GitHub Desktop.

Select an option

Save the-ge/083694fb2795c18b41b9007a1ad86753 to your computer and use it in GitHub Desktop.
#!/usr/bin/bash
set -eEu
cue-fatal()
{
local title="$1" message="$2"
notify-send \
--hint='string:desktop-entry:org.kde.dolphin' \
--app-name='file-rename-hack' \
--icon='dialog-error' \
--expire-time=4000 \
"$title" \
"$message"
exit 1
}
main()
{
local class wid path new_name new_path
local -a info=()
# get current window class and PID
readarray -t info < <( xdotool getactivewindow getwindowclassname getactivewindow )
class="${info[0]}"
wid="${info[1]}"
[ "$class" == 'dolphin' ] || cue-fatal 'Unsupported app' "Called from:\n<b>$class</b>\nThis script is to be used in a Dolphin window."
# simulate pressing the 'Menu' key, then 'r' after 50 msecs
xdotool key --window "$wid" 'Menu' && sleep 0.05 && xdotool key --window "$wid" 'r'
}
main "$@"
#!/usr/bin/bash
set -eEu
. .notify-desktop
. .qdbus
# same inline renaming as Dolphin
rename-by-action()
{
local id="$1"
dolphin-action-trigger "$id" 'renamefile'
}
# copy the file-to-be-renamed location to clipboard,
# then use it to open a kdialog for renaming
rename-by-dialog()
{
local qid="$1"
# copy location to clipboard (copy_location)
dolphin-action-trigger "$qid" 'copy_location'
# retrieve path from the clipboard
path="$( xclip -selection clipboard -out )" # get path from the clipboard
new_name="$( kdialog --title "Rename File" --inputbox "Type the new name:" "$( basename "$path" )" )"
new_path="$( dirname "$path" )/$new_name"
if mv "$path" "$new_path"; then
echo "Renamed '$path' to '$new_path'." | systemd-cat --identifier="${0##*/}" --priority='info'
else
echo "Failed renaming '$path' to '$new_path'!" | systemd-cat --identifier="${0##*/}" --priority='warning'
fi
}
# simulate pressing the 'Menu' key, then 'r' after 50 msecs
rename-by-context()
{
local wid="$1"
xdotool key --window "$wid" 'Menu' && sleep 0.05 && xdotool key --window "$wid" 'r'
}
main()
{
local class qid wid path new_name new_path
local -a info=()
# get current window class and PID
readarray -t info < <( xdotool getactivewindow getwindowclassname getwindowpid getactivewindow )
class="${info[0]}"
qid="org.kde.dolphin-${info[1]}"
wid="${info[2]}"
[ "$class" == 'dolphin' ] || cue-fatal 'Unsupported app' "Called from:\n<b>$class</b>\nThis script is to be used in a Dolphin window." "${0##*/}"
#rename-by-dialog "$qid"
#rename-by-action "$qid"
rename-by-context "$wid"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment