ssborbis / ContextSearch-web-ext

Search engine manager for modern browsers
352 stars 37 forks source link

ability to pass arguments to other applications #459

Closed karinaardos closed 1 year ago

karinaardos commented 2 years ago

The ability to pass arguments to other applications is very lacking. I'm currently using the External Application Button extension

ssborbis commented 2 years ago

That would be a cool addition.

I have a python3-based native app ready to go. Basic testing looks good.

I've tested the following so far

"open in firefox" "open in chrome" "open in gimp"

externalProgram

karinaardos commented 2 years ago

thank you, you are a very responsive programmer. and I want to tell you that you have created the best of the best supplement. Your add-on works flawlessly and performs its functions perfectly. I meant that the add-on can transfer the selected text to other windows 10 applications.

many programs have a command line

ssborbis commented 2 years ago

I meant that the add-on can transfer the selected text to other windows 10 applications. many programs have a command line

What programs are you looking to open from this addon? I can do some testing locally.

ssborbis commented 2 years ago

I am loving this addition BTW. I do a lot of graphic design and opening images straight into GIMP is already saving me time. Thanks for the suggestion!

I'll reopen the old native app page I have and replace the old code with the new code that supports launching external apps over the next week or 2.

karinaardos commented 2 years ago

Я имел в виду, что дополнение может передавать выделенный текст другим приложениям Windows 10. многие программы имеют командную строку

Какие программы вы хотите открыть из этого дополнения? Я могу сделать некоторые тесты на месте.

Instant file search program on all drives (Everything.exe)

I would like to transfer the selected text to the (AkelPad.exe) notepad - "but it seems to me that there will be difficulties with this program"

ssborbis commented 2 years ago

I would like to transfer the selected text to the (AkelPad.exe) notepad - "but it seems to me that there will be difficulties with this program"

I'm sure there are ways.

If you wanted a new file with the text, you could launch a script that saves the selected text to a temporary file, then launches AkelPad.exe

ssborbis commented 2 years ago

I have a beta in the release section ( Firefox ) and the python native app can be found here if you want to test.

karinaardos commented 2 years ago

Unfortunately I didn't get anything.

I did all the steps according to the instructions but I only see an error message at the top of firefox (Native messaging app is not connected. See support site for more details.)

Here's what I did. Python 3.9 i use windows 10 firefox portable version

  1. Created a folder in a directory named (ContextSearch-webext) c:\Users\MYNAME\AppData\Roaming\ContextSearch-webext\

  2. Copied two files into this folder c:\Users\MYNAME\AppData\Roaming\ContextSearch-webext\ContextSearch.py c:\Users\MYNAME\AppData\Roaming\ContextSearch-webext\contextsearch_webext.json

  3. Opened in notepad (contextsearch_webext.json) and changed the way { "name": "contextsearch_webext", "description": "Launch applications from ContextSearch-webext", "path": "c:\Users\MYNAME\AppData\Roaming\ContextSearch-webext\ContextSearch.py", "type": "stdio", "allowed_extensions": [ "{5dd73bb9-e728-4d1e-990b-c77d8e03670f}" ] }

  4. Added a line to the registry (and it appeared in the registry) REG ADD "HKCU\Software\Mozilla\NativeMessagingHosts\contextsearch_webext" /ve /t REG_SZ /d "c:\Users\MYNAME\AppData\Roaming\ContextSearch-webext\contextsearch_webext.json" /f

  5. In (Context Search 1.35.0beta) Created an external application launch button Name. Everything

    The path is shown like this. d:\Total Commander\Utilites\Everything\Everything.exe

I can't figure out what I'm doing wrong

ssborbis commented 2 years ago

There's a problem with Windows inconsistently launching python *.py files as executables. The workaround is to make a *.bat file instead. I had to do this on my Windows 10 install to get this to work.

In contextsearch_webext.json, change ...

"path": "c:\Users\MYNAME\AppData\Roaming\ContextSearch-webext\ContextSearch.py",

to

"path": "c:\Users\MYNAME\AppData\Roaming\ContextSearch-webext\ContextSearch.bat",

In folder c:\Users\MYNAME\AppData\Roaming\ContextSearch-webext\, create a new file named ContextSearch.bat with the following content

@echo off
C:\Users\MYNAME\AppData\Local\Programs\Python\Python309\python.exe ContextSearch.py

Change line 2 to point to your python.exe

ssborbis commented 2 years ago

Also, I just noticed you're using a portable version of Firefox. I don't know how that will effect things. I will test the portable version on my machine.

ssborbis commented 2 years ago

The portable version appears to be reading the registry keys correctly.

At any point, did you receive a prompt to grant the nativeMessaging permission during your testing?

Check that the nativeMessaging permission is granted under Addons -> ContextSearch web-ext -> Permissions tab permission

ssborbis commented 2 years ago

I added an install.py script to the repo. This should work to install to all platforms. Run it from an administrative prompt. Defaults to user-level now. Shouldn't need admin privileges.

karinaardos commented 2 years ago

Still doesn't work for me. Watch the video how I did it.

https://disk.yandex.ru/i/EzyGk9ABNJG29Q

ssborbis commented 2 years ago

Thanks for the video. That's very helpful.

Everything looks like it installed ok. The only thing I couldn't check was the registry keys. Could you run install.py again from the console and paste the output here?

Lets see if the browser is in fact communicating with the python app.

Open the CS options page, then the dev console ( Shfit+Ctrl+K ) and enter this

browser.runtime.sendNativeMessage("contextsearch_webext", {verify:true}).then(r => console.log(r));

If you get an error, the native messaging API is not finding the app

If you get true, maybe try a simpler command.

From the same console, try running browser.runtime.sendNativeMessage("contextsearch_webext", {path:"notepad"});

ssborbis commented 2 years ago

I think python is requiring double-backslashes for windows locations, or a single forward-slash

So, if I were using the full path for, say, notepad

"C:\\Windows\\system32\\notepad.exe" or "C:/Windows/system32/notepad.exe" would work

but not "C:\Windows\system32\notepad.exe"

ssborbis commented 2 years ago

Your command may work if replaced with:

"d:/Total Commander/Utilities/Everything/Everything.exe" -s {searchTerms}

Notice the quotes around the .exe path. Those are probably necessary, as there is a space in the path.

(edit) I had to remove the quotes from around {searchTerms} as they were causing the command to fail. I'll have to look into that further, but this current command is working for me to at least open Everything.exe

karinaardos commented 2 years ago

You were right about the "\" count

I pasted the path from your example: "C:\Windows\system32\notepad.exe" and it works

I noticed that there is a space in my path and I moved the program to the root of the disk to get rid of spaces e:\Everything\Everything.exe -s {searchTerms} and it started up fine with the right arguments

Doesn't work if: d:\Total Commander\Utilites\Everything\Everything.exe -s "{searchTerms}" "d:\Total Commander\Utilites\Everything\Everything.exe" -s "{searchTerms}"

Working "d:\Total Commander\Utilites\Everything\Everything.exe" -s {searchTerms}

You did it wonderfully. thanks a lot

ssborbis commented 2 years ago

The quotes issues seems to be taken care of in newer versions of ContextSearch.py.

You can download the package again and run install.py or replace ContextSearch.py in %user%\AppData\Roaming\ContextSearch-webext with the new version

(edit) By this, I mean

"d:/Total Commander/Utilities/Everything/Everything.exe" -s "{searchTerms}"

should run without errors

karinaardos commented 2 years ago

Yes, it's even better now.

karinaardos commented 2 years ago

I can't run files with extension (.bat)

Is it so intended?

ssborbis commented 2 years ago

I can't run files with extension (.bat)

I updated ContextSearch.py with a slightly different call to open programs. It seems to be running .bat files for me.

If you need to see the terminal output, use start in front of the command like so:

start c:/users/mclovin/desktop/test.bat

If you want to see the output, but close the window when finished, try

start cmd /c c:/users/mclovin/desktop/test.bat
ssborbis commented 2 years ago

FYI, if you're using the beta2 with the test button, you need to save before hitting "Test", otherwise it will not have the updated path.

ssborbis commented 2 years ago

You may also run into the issue of your .bat file not working because the current working directory is not the same as the directory the .bat is in. If need be, I can probably add a cwd field, but you should be able to get around it with something like this:

start cmd /k "cd c:/users/mclovin/Desktop && test.bat"
karinaardos commented 2 years ago

You never cease to make me happy. Bat files now run perfectly. thank you so much

ssborbis commented 2 years ago

Bat files now run perfectly. thank you so much

Good deal!

I went ahead and added a working directory field to app launcher options.

Beta-3 combined with the latest native app code will allow for this.

cwd

https://github.com/ssborbis/ContextSearch-web-ext/releases/download/1.35.0-beta/contextsearch_web_ext-1.35.0b3-an+fx.xpi

karinaardos commented 2 years ago

I write and laugh. because I wanted to offer you this, but you seem to have read my thoughts. As a result, you created a very cool add-on

ssborbis commented 2 years ago

1.35.0 has been released to AMO and Chromestore.

logusergithub commented 2 years ago

Remarkable new feature. Thanks. I tested it with PotPlayer, MPV, Yt-dl, Firefox (other profile), Edge, JDownloader and Foobar2000. I don't know why the extension tell me that 'Messenger app is not connected' despite everything works fine, but I probably miss some understanding. Unless you already can do, is it possible to add an option inside the Launcher 's settings to directly use the address bar URL when nothing is selected? This mainly concerns a faster use with the Toolbar.

ssborbis commented 2 years ago

I don't know why the extension tell me that 'Messenger app is not connected' despite everything works fine, but I probably miss some understanding

That is odd. If everything is working correctly, it should show the version of the native app where the message you're seeing is. I wonder if you have a slightly older version of the native app installed.

native

Unless you already can do, is it possible to add an option inside the Launcher 's settings to directly use the address bar URL when nothing is selected? This mainly concerns a faster use with the Toolbar.

I can add additional variables besides {searchTerms} to be replaced.

I know {domain} and {subdomain} are replaced ( for search engines at least, not sure about app launchers ). I could add {url}. That would effectively give you what you want.

logusergithub commented 2 years ago

Oh, I see in your picture v2.09, I installed v2.00 from the related Tags. As for the feature request that would be nice, I've usually always used a "Toolbar (list)" like tool that uses URLs on the address bar and a Context Menu for the rest. Thanks.

ssborbis commented 2 years ago

Code is in the dev branch

Next release, you can simply add this to your Modify Terms regex

/^$/{url}/g

It will replace empty terms with {url} which is then replaced with the url of the current active tab

logusergithub commented 2 years ago

This is perfect. At this rate in the near future I imagine that if I will use the Toolbar to look for 'coffee' a cup from the screen will be released. Just for the record, at the moment your extension can support over 500 search engines without slowdowns.

ssborbis commented 2 years ago

v1.36 is live with the {url} param added. Let me know how it works out

logusergithub commented 2 years ago

v1.36 is live with the {url} param added. Let me know how it works out

I tested on 1.36 (new profile) and 1.37b1 (main profile): "C:\Program Files\Mozilla Firefox\firefox.exe" -p "default" -no-remote "{url}" It works well. Thanks. NB:

ssborbis commented 2 years ago

as I wrote in the other post, seems that 'Modify terms regex' in 'App launchers' are not saved

It is saved, but is not displayed when you next open the edit modal. It's fixed now locally. Thanks.

for other users... if you 'Copy' an 'App launcher' is not like a shortcut so you have to modify the copy too

This is true. Only OpenSearch engines get the shortcut option. Everything else is an independent copy. ( scripts, app launcher, site search, firefox engines, etc ).

logusergithub commented 2 years ago

It is saved, but is not displayed when you next open the edit modal.

I would not have thought of that on my own. I see now the regex saved if I open the backup.json.

This is true. Only OpenSearch engines get the shortcut option

I misunderstood at the beginning because when I made a copy the name is the same like when you do a shortcut. You can imagine when I changed "{url}" inside 'Firefox' in the folder and used the second 'Firefox' not in the folder during the test. :-)

karinaardos commented 2 years ago

There is a small problem. If there are two {searchTerms} in the argument, then only one is passed to the program as an argument

for example. If the path is like this d:\PotPlayerMini.exe n:{searchTerms}{searchTerms}_1080P.ts" /new

Transmitted like this d:\PotPlayerMini.exe n:\selectText{searchTerms}_1080P.ts" /new

Should be sent like this d:\PotPlayerMini.exe n:\selectText\selectText_1080P.ts" /new

Can this be implemented?

ssborbis commented 2 years ago

Can this be implemented?

I think it's just moving from .replace() to .replaceAll(). I never anticipated the terms being used more than once. Easy fix.

ssborbis commented 2 years ago

Here, this should do it contextsearch_web_ext-1.38a1-an+fx.xpi.zip .

karinaardos commented 2 years ago

Works great, thanks for the fast work

logusergithub commented 2 years ago

"C:\Program Files\MPV\mpv.exe" "{searchTerms}" "C:\Program Files\PotPlayer\PotPlayerMini64.exe" "{searchTerms}"

I noticed using MPV or PotPlayer from CS, unlike the other dedicated extension I always used, that both the programs close themself during streaming after I close Firefox. Morevover if with Potplayer I set "/add" the link is not added to Playlist. It is related to how the native app works or the parameters I use? It can be replicated with YouTube.

ssborbis commented 2 years ago
"C:\Program Files\DAUM\PotPlayer\PotPlayerMini64.exe" /add {searchTerms}

Works for me. With quotes, it would add a bunch of window .wav files for some reason.

unlike the other dedicated extension I always used, that both the programs close themself during streaming after I close Firefox

I'll look into it. ~You might be able to get around it using cmd /K in your command line to open a new terminal~

logusergithub commented 2 years ago

Works for me. With quotes, it would add a bunch of window .wav files for some reason.

Thanks. I removed the quotes and the playlist was added, however seems it does't affect the closing behaviour of the program during streaming. As usual if you need my config file I'm available. I almost feel guilty about finding odd issues.

ssborbis commented 2 years ago

I put a new version of ContextSearch.py in the repo. You can grab a copy and place in /Users/username/AppData/Roaming/ContextSearch-webext.

The way it works right now: If you do NOT have any code in the Post-App Script field, the program should stay open after you close the browser If you DO have code, it's going to close with the browser.

ssborbis commented 2 years ago

Actually, I'm still seeing the apps close. I'll need some time to see what's going on.

logusergithub commented 2 years ago

Thanks for the note. I was about to start over 2.10 again to confirm if I had something wrong and I was ready to make a video. I see that you are engaged in a work in progress on 2.11. I also noticed that PotPlayer /add works now with quotes on {searchTerms}.

I was also thinking about the {url} parameter: if it is actually useful when you open the external application from the toolbar, it places limitations if you use it from the context menu obviously outside 'page' search (links, audio, video) so you have to use a second App loader with {searchTerms}. To fully simulate dedicated extensions, which themselves are simpler than the useful complexity of CS, the {url} parameter should act as {searchTerm} in the context menu if possible. But it's a little nuance not so important.

ssborbis commented 2 years ago

I think I have a fix for the closing when the browser closes. Please try this ContextSearch.py.zip

It's a windows-only issue. I never saw that problem on linux.

logusergithub commented 2 years ago

Tested on multiple sites, it works. As always, thanks.

From what I see Windows 10 will be the last and when the time comes I'll switch to Linux.

ssborbis commented 2 years ago

I am continuing to make changes to the ContextSearch.py file due to differences in how python behaves on linux and windows. If you want to update to the latest version, you can open the console from the CS options page and type this command:

checkAndUpdateNativeApp()

It will check the repo and prompt you to update if one is available. On the next version of CS, I'll add a button.

logusergithub commented 2 years ago

I noticed this as I was on CS finally finishing adding the latest search engines. 2.14 works fine as the last tested file. I was just thinking about adding the NativeApp to my AutoHotkey script which I already use to update some filters on GitHub, but you anticipated me. Sure the future button is a useful new feature.