"Open With" Context Menu - Bash Commands Topic is solved

The dedicated forum for PCMan File Manager - http://wiki.lxde.org/en/PCManFM
abacus7
Posts: 29
Joined: Sat Nov 10, 2018 8:17 pm

"Open With" Context Menu - Bash Commands

Post by abacus7 »

I frequently use the following interactive grep command:

Code: Select all

echo "Enter keyword" && read keyword && grep -i --color=always $keyword /pathtofilename
As an alternative, it would be handy to have it as a desktop file that could be called from the context menu ("Open with") in pcmanfm. Is this possible?

I've tried without success entering the Exec= field in the following ways:

- Entered the straight command line without any modifications
- Prepended the command line with bash
- Prepended the line with bash -c
- Prepended the line with lxterminal
- Prepended the line with lxterminal bash
- Added %f
- Added %F
- Made the line into a simple script and placed the script filename into the Exec= field (with and without bash, bash -e, etc.)

Any ideas?

I'd prefer to not go the script route if at all possible. I've played with simple scripts in the past but keep forgetting their ins and outs and end up going back to plain desktop files.
drooly
Posts: 791
Joined: Mon Apr 08, 2013 6:45 am

Re: "Open With" Context Menu - Bash Commands

Post by drooly »

Where were you putting the .desktop file?

I have one in ~/.local/share/file-manager/actions/openfolderasroot.desktop and it works as desired.
It contains:

Code: Select all

[Desktop Entry]
Type=Action
Name=Open as Root
Icon=utilities-terminal_su
Profiles=on_folder;

[X-Action-Profile on_folder]
Name=Open as Root
MimeTypes=inode/directory;
SelectionCount==1
Exec=sudo -A pcmanfm %f
Also see:
https://github.com/lxqt/pcmanfm-qt/wiki/custom_actions
https://wiki.lxde.org/en/PCManFM#Add_ow ... ntext_menu (old)
https://specifications.freedesktop.org/ ... c-1.1.html
abacus7
Posts: 29
Joined: Sat Nov 10, 2018 8:17 pm

Re: "Open With" Context Menu - Bash Commands

Post by abacus7 »

Thanks. I did have already an OpenFolderInRoot desktop file in the Actions folder. It was a bit different but works the same.

I also have an OpenInNano-Root desktop file that I can use on individual files and that is in the .local/share/applications folder.

What I don't have is a way to click on an individual file and have it open up a terminal with my grep or another bash command line already written in place along with the path/filename. I have a growing feeling that's not something that can be done, but wanted to ask.

My current method is to create aliases for each file that I frequently grep. But I don't have anything that would would work for individual files on the fly. Instead, I have to navigate to the file within Pcmanfm, right click on "Copy Path", call up LXTerminal, type in the grep or other bash command, then paste in the path/filename that I had copied from Pcmanfm, then execute. Not a big deal, but would save some mouse clicks. :)
drooly
Posts: 791
Joined: Mon Apr 08, 2013 6:45 am

Re: "Open With" Context Menu - Bash Commands

Post by drooly »

So the only problem is that you can get it to work on folders, but not on files?
Have you clicked on the links I gave you, because it is explained there.
You need a different mimetype.

Code: Select all

MimeTypes=all/allfiles;
should do for all actual files (but not symlinks).

Code: Select all

MimeTypes=all/all;
is for everything, files, folders...

Maybe also show us what you have.
abacus7
Posts: 29
Joined: Sat Nov 10, 2018 8:17 pm

Re: "Open With" Context Menu - Bash Commands

Post by abacus7 »

drooly wrote: Sat Feb 12, 2022 4:01 pm So the only problem is that you can get it to work on folders, but not on files?
Have you clicked on the links I gave you, because it is explained there.
You need a different mimetype.

Code: Select all

MimeTypes=all/allfiles;
should do for all actual files (but not symlinks).

Code: Select all

MimeTypes=all/all;
is for everything, files, folders...
Nope, the problem is something different. I can open files using the right-click menu. I can do it either by changing the mimetype in my OpenAsFolder action or I can use the OpenAsNano or OpenAsNanoRoot as desktop files . There are other ways to do it as well. Whichever way I choose, the file will open either in Leafpad or Nano.

In fact, I rarely ever put the mimetypes in the desktop file so they end up opening everything. (Some people say the mimetypes field is necessary; I've never found that to be the case.)

The issue is that I'd like to be able to open a file in Bash, specifically in Grep, by using one of the following:
  • Pcmanfm's right-click menu in combination with"Open With" and a desktop file named "Open in Grep" (with the specific Grep command already populated)
  • Pcmanfm's right-click menu in combination with "Open With" and a "Custom Command Line"
  • Pcmanfm's right-click menu in combination with an action named "Open in Grep" (with the specific Grep command already populated)
I'm probably overestimating the capabilities of Linux in thinking I can do that, but I figured it was worth a try.

I also think I am confused about what can be used for an "Exec". To me, a Bash command is an executable, but from what I've been reading, it's actually considered a shell. That's why I've tried using both LXTerminal and Grep (or LXTerminal and Bash and Grep) in the same Custom Command Line and or Exec but that hasn't worked either.

I have quickly gone over my head. ;)

This of course is nothing of earth-shattering value. I'm just trying to pare down the mouse clicks and/or keyboard strokes to get at the file I want and have it open in my specific Grep command.

The alternative is having a Bash alias for every single file I can think of that I may want to open in Grep someday. But that is giving in and accepting that Linux/Pcmanfm, etc. have limitations. I'd rather explore the boundaries first before throwing in the towel.
drooly
Posts: 791
Joined: Mon Apr 08, 2013 6:45 am

Re: "Open With" Context Menu - Bash Commands

Post by drooly »

Show us what you have so far. The actual code.
It sounds to me like you want to launch an interactive shell script that does what you want. One that you would need to write. That would be launched by your filemanager action.
abacus7
Posts: 29
Joined: Sat Nov 10, 2018 8:17 pm

Re: "Open With" Context Menu - Bash Commands

Post by abacus7 »

drooly wrote: Sun Feb 13, 2022 4:29 pm Show us what you have so far. The actual code.
It sounds to me like you want to launch an interactive shell script that does what you want. One that you would need to write. That would be launched by your filemanager action.
The code was in the first post. Just a little interactive grep command line. It's not a script but just what you would enter directly onto the command line, or in my case, I use it in various aliases in my .bashrc. The difference is that in the aliases, I include a specific path/filename. What I want to do now is skip entering the path/filename and rather navigate to the file in pcmanfm, right click and select an action under the context menu.

In this current experiment, I tried the command line as written, and I tried it by prepending it with various things (bash, lxterminal, both). I also tried adding %f and %F. Sometimes the terminal would actually open, but it would close so fast that I didn't know if it actually processed the command line.
drooly
Posts: 791
Joined: Mon Apr 08, 2013 6:45 am

Re: "Open With" Context Menu - Bash Commands

Post by drooly »

Did you ever read the links I provided to you in my first reply?
I feel like most your questions are answered by actually reading up on the spec.
As I also said, I think you should put that oneliner into a separate shell script, when %f %F %d etc. are passed as positional parameters.
abacus7
Posts: 29
Joined: Sat Nov 10, 2018 8:17 pm

Re: "Open With" Context Menu - Bash Commands

Post by abacus7 »

drooly wrote: Wed Feb 16, 2022 5:29 pm Did you ever read the links I provided to you in my first reply?
Yes. I read it when you first posted it, and unless I missed something, there were no examples using a bash command line as the Exec. It seems to be mainly about using desktop files for GUI programs like file managers, text editors.

Probably an easier way of getting at this is to drop the requirement for a desktop file and just figure out what to put in pcmanfm's Custom Command Line. This is really the heart of the issue and once I understand it, I can build a desktop file around it. So here's a simple exercise:


1. Open pcmanfm
2. Select a file and right click Open With
3. Select Open With at the bottom of the context menu
4. Select Custom Command Line
5. Enter some Bash code.
6. Check the box to have it Execute in Terminal Emulator

In this test I selected my .bashrc file (since it was handy) and for my code I typed

Code: Select all

grep -i alias %F
since I knew the word alias was in the .bashrc file a number of times and grep should pick it up. Nothing happened.

So, what am I supposed to add to make this work? That's the heart of the issue.
As I also said, I think you should put that oneliner into a separate shell script, when %f %F %d etc. are passed as positional parameters.
The positional parameter I used in the code in my first post was the dollar sign. I eliminated that for this latest test and instead added a % sign (though it serves a different purpose). I didn't read your last article word by word but did a search for a % sign and didn't see one. I've never seen a % sign in Bash so I'm not sure if we're mixing apples and oranges.

At any rate, I think you're saying now I should write a script. If that's the only way I can do it, I'll take a pass. I don't use scripting often enough to remember all the rules. Whereas I frequently create desktop files with simple commands written into the Exec line. That's what I was hoping I could use.

Again, this was nothing urgent or earth-shattering. I just wanted to build on the rudimentary skills that I already had which is creating desktop files and using Bash on a command line. Scripting is a little more complicated and it has its own set of rules which, when I used to try doing it, I could never remember.
drooly
Posts: 791
Joined: Mon Apr 08, 2013 6:45 am

Re: "Open With" Context Menu - Bash Commands

Post by drooly »

abacus7 wrote: Fri Feb 18, 2022 9:20 amAt any rate, I think you're saying now I should write a script. If that's the only way I can do it, I'll take a pass. I don't use scripting often enough to remember all the rules.
That's BS.
You already have a script in your first post.
Open a text editor, and paste that command in there.
Replace /pathtofile with $1.
Put quotes around all $variables, like this:

Code: Select all

echo "Enter keyword" && read keyword && grep -i --color=always "$keyword" "$1"
Save the script and make it executable - either through your filemanager, or with

Code: Select all

chmod +x script
Then edit your .desktop file thusly:

Code: Select all

Exec=/full/path/to/script %f
You see, %f in your .desktop file becomes $1 in your script, a positional parameter.

However, if you leave it like that, it works, but the terminal will close before you have a chance to see anything.
Therefore, add another read at the end - now it will wait for your input before closing:

Code: Select all

echo "Enter keyword" && read keyword && grep -i --color=always "$keyword" "$1"; read
In the future you might want to look up some shell syntax; while your script works well enough, there's room for improvement, and you'll be amazed how much you can already do with your knowledge.
Anyhow, writing .desktop files is harder than writing shell scripts...
Locked