Nemo script to get selected files as one command line string

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
JohnML
Level 1
Level 1
Posts: 35
Joined: Sat Mar 20, 2010 1:13 pm
Location: frankfurt, germany

Nemo script to get selected files as one command line string

Post by JohnML »

Usable with LinuxMint cinnamon nemo!!

On windows ( Powertoys?!) i used it very often and missed it for Linux:

Copy selected files to string usable as command line argument.

Example:
"/home/john1/.local/share/nemo/scripts/Queue in Audacious" "/home/john1/.local/share/nemo/scripts/Scan with Avast"

As you see, long filenames will be ok

its just a one line script
Edit: 11.10.17 uses single quotes 'PathAndName'

Code: Select all

#!/bin/bash

# Powertoys "copy as name" nachahmen, geht auch mit langen Dateinamen

# single Quotes
copyq copy -- "$(echo -en "$NEMO_SCRIPT_SELECTED_FILE_PATHS" |sed -e 's/^\|$/'\''/g' | tr '\n' ' ' )"

Save it to your home-folder/.local/share/nemo/scripts/Copy_filenames
Make it executable
Goto Nemo edit/plugins
Right listbox
Check it
Select some files, right click srcipts/Copy_filenames

Paste it for example to terminal: gedit + paste

Really great copyq need to be installed!!!

Code: Select all

deb http://ppa.launchpad.net/hluk/copyq/ubuntu xenial main
deb-src http://ppa.launchpad.net/hluk/copyq/ubuntu xenial main


I could not write a sript thta handles all xcopy ...

Hope its useful ...

... have to learn which files can be attached: no scripts no *.list !!!

Ok, next solution: open terminal besides nemo and drag'n drop selected files to terminal ... bat that's much to easy, i wanted the srcipt :-)
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 3 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
Termy
Level 12
Level 12
Posts: 4254
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Nemo script to get selected files as one command line string

Post by Termy »

I'm having a hard time understanding what the goal of this one-liner is. Is it to convert this:

Code: Select all

$ ls
Beep.wav  bin  BP.wav  Desktop	Documents  dong.wav  Downloads	FB.wav	GitHub	kdenlive  Music  Pictures  ShellPlugins  Videos
...to this:

Code: Select all

$ ls -Q
"Beep.wav"  "bin"  "BP.wav"  "Desktop"	"Documents"  "dong.wav"  "Downloads"  "FB.wav"	"GitHub"  "kdenlive"  "Music"  "Pictures"  "ShellPlugins"  "Videos"
As for copying files, shell does this very, very well out of the box, thanks to things like brace expansion, parameter expansion, and then there's tab auto-completion. Find too is really good for this, thanks to features like -exec.

You're calling sed three times here; I recommend revising that. You can combine multiple sed expressions into one sed command. Here are two typical approaches:

Code: Select all

sed 's/[^ ][^ ]*/"&"/g; s/\%20/\ /g; s/file:\/\///g'
sed -e 's/[^ ][^ ]*/"&"/g' -e 's/\%20/\ /g' -e 's/file:\/\///g'
I'm also confused by the duplication of [^ ]; one of them would optionally match a space at the beginning of the line; two make no sense to me, but as it's optional, probably won't break the regex. If you have issues with these examples, which is a potentiality, you may need to revise the logic. I'd be interested in seeing an example of the output you're dealing with.

Oh, I see what the goal is now! That's awesome. Nice one. I didn't think nemo could share that sort of info like that. Makes me wonder what else it shares which could be scripted.

You could use this to save calling sed at all, and thus rely only on shell builtins for more portability (useful just about anywhere) and efficiency:

Code: Select all

for FILE in $NEMO_SCRIPT_SELECTED_URIS
{
        while read
        do  
                echo "${REPLY//%20/ }"
        done <<< ${FILE/file:\/\/}
}
Last edited by Termy on Mon Oct 02, 2017 10:04 am, edited 2 times in total.
I'm also Terminalforlife on GitHub.
Misko_2083

Re: Nemo script to get selected files as one command line string

Post by Misko_2083 »

This is interesting.
I have this in my ~/.bashrc

Code: Select all

select_files() {
OIFS=$IFS
IFS=$'\n' 
local files=" $(for line in $(zenity --file-selection --multiple --title="Select a File"  2> /dev/null | tr '|' '\n'); do printf "'%s' " "$line"; done)"
IFS=$OIFS
READLINE_LINE="${READLINE_LINE:0:READLINE_POINT}$files${READLINE_LINE:READLINE_POINT}"
READLINE_POINT=$((READLINE_POINT + ${#files}))
}

bind -x '"\C-g":select_files'
When I press ctrl+g, zenity dialog pops up and let's me select files.
User avatar
Termy
Level 12
Level 12
Posts: 4254
Joined: Mon Sep 04, 2017 8:49 pm
Location: UK
Contact:

Re: Nemo script to get selected files as one command line string

Post by Termy »

Nice! Maybe I should start using GUIs more. xD
I'm also Terminalforlife on GitHub.
Locked

Return to “Scripts & Bash”