Is it possible to call an application selection window (Right click->Open With->Other) from the linux console? - linux

On Gnome/KDE you can select in which application you want to open file (Right click on file -> Open With -> Other). Is it possible open file that way, but from console?
For example: you print " file.ext" and instead of opening in concrete application, there are that application selection window forced and then users chooses - starts selected program.
I tried to figure out that myself, but not found anything like that.
"edit file.ext" doesn't fits my needs, because it starts preferred application and you cannot choose which. And also on my desktop it says:
"Error: no "edit" mailcap rules found for type "image/jpeg"
So, am I able to forse that "open with" window from console? If yes, can you say how?
Both on windows and mac you can do such things.
//edit at 2009-02-10 14:17
Thank you very much for answers. Command will be used in program code, so unfortunately probably I would not be able to make some extra bash scripts.

For GNOME:
gnome-open <file>
For KDE:
kfmclient exec <file>
These commands should open up the <file> in the preferred application in GNOME or KDE respectively, although I don't have an installation of either to test on.

Take a look at man run-mailcap, you can change or add selected applications for each mimetype modifying the /etc/mailcap, ~/.mailcap files and some others.

Traditionally, on Unix systens (and therefore Linux, too), you start applications from the console (and not from a UI). The command line (or console) expects you to enter the name of the application and then the filename (plus some options).
This allows to use applications (or commands) in shell scripts.
On Windows, there is no real console (the DOS box is just a reminiscence of the dark ages of MS DOS). So the MS developers came up with the idea to have the OS treat anything as a command. If it's not a real command or application, the OS will determine the file type (by extension on Windows and by some header information on Mac). For each file type, there will be an associated application in a look up table.
This is why on Windows, it appears that you can enter the name of a file on the console and you will get the application to edit that file.
If you want a quick way to fix this in the Unix console, create a script called "open" or "o" and use the file command with the option --mime to identify the file type. You can then use a case statement to launch your favorite editor.
As for the error about "mailcap rules": There is a file called "mailcap" on Unix where you can define abstract "commands" (open, edit, view, print) for file types. See the mailcap man page.

Related

How does File Explorer open files?

It's maybe stupid question, but what happens behind the scenes when i double click the word app, or .exe app?
For 'Non-Developer' it just opens the file in right environment after doubleclicking. But I would like to know, how is it done, how can the file manager know what to open? (.docx in word, .txt in texteditor,etc...)
+ I would like to know how can I do that in Node.js, is it the best way to use child_process and if statements for every suffixes?
There is a file association to tell Windows how to treat a certain file extension. You can type assoc in a Windows Command Prompt to see them. As an example:
C:\test>assoc .txt
.txt=txtfile
So Windows knows now, that the file with the extension .txt is a txtfile.
ftype defines, how that filetype is to be handled:
C:\test>ftype txtfile
txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1
So whenever you doubleclick on a file, Windows checks if it is associated with any filetype. Then it looks up, how to handle that filetype and executes that command (in the example above, it opens Notepad with the filename as a parameter).
Both assoc and ftype are able to change the settings (for example to open .txt files with another editor). But if you try that, do yourself a favor and note the original settings, so you are able to revert your changes when needed.
The changes can also be done directly in the registry (not recommended, when you are not experienced in handling the registry)
For Node.js, you can use child_process to shell out to the start command which does similar things as the File Explorer.
E.g. start some/path/to/file.docx will open that file in the default program associated with the format.

Is there a way to set a python script as program to open a type of file?

The problem:
I'm currently working on a programming language, which uses a simple python interpreter. The interpreter loops over every line with a bunch of if-statements.
The file extension I'd like to use is .ccp.
So far my progress. I want the computer to recognise .ccp files as a CalcScript file, and open it with the script.
I don't want to have a default filename which I can open using text = open("filename.idk","r").read(), I want to open a file like a 'normal' file. You double-click on the file in explorer, and it opens it in the python script. Regardless of the filename.
What I've tried:
Tinkering with the default applications in settings
Tinkering in regedit
Converting my .py file to .exe
Scouering the internet
My code:
https://github.com/AnonymousPixel/CalcScript
Sorry for bad English if there was any.
Summarizing my comments on the question, you can follow the steps below to achieve what you are asking:
Use sys.argv to access the command line arguments. For example the following script will just print all the arguments given to it:
import sys
print("Given arguments: ", str(sys.argv))
Let's name it myprogram.py.
You can then call it (I think) with: python myprogram.py arg1 arg2 arg3 and it will run and print the command line arguments.
Notice that the first argument (sys.argv[0]) is the script's path. So arg1 would be sys.argv[1], arg2 would be sys.argv[2] and so on.
I am saying to use sys.argv because as far as I remember double clicking a file with an extension which has a default opening program, will open that program with the file path as an argument.
Next step is to package your python script to an executable. This has been already asked and answered for example here (which is a duplicate, where you can follow the question which came before it to see even more examples). I used PyInstaller to test it (and on Windows OS). My command was like:
pyinstaller myprogram.py
... and it generated some folders and files. Specifically the folder dist\myprogram contained the executable along with its dependencies. You can then run your program by double clicking on it in the dist\myprogram folder. It should pop a CLI window, printing the arguments (ie only the program's path, since we called it without any other) and immediately exit. Or you can open a CLI window and run it with a command like:
myprogram argument1 argument2 argumentN
(supposing your current working directory is dist\myprogram) and it will indeed print the arguments.
Finally you have to set it up as the program which by default opens files with .ccp extension. On Windows 10, you can do this via:
Open up File Explorer.
Find a file with .ccp extension (or create one).
Right click on it.
Click on Properties on the dialog that pops up.
Go to General tab (if you are not already there) on the dialog that pops up.
On the Open with: section there is a button which reads Change. Click it.
Select More apps at the bottom of the dialog.
Make sure you have the Always use this app to open .ccp files checkbox selected.
Scroll to the bottom of the dialog and click on the blue text which prompts for manually selecting the default app, which in turn pops up a file chooser. I am not running on English language so it is a bit difficult to translate it exactly (I followed some online pages to actually see the default translation for the previous steps).
Select your executable as the default.
Confirm your choices by selecting Ok, Apply or anything else required.
Then you will also be able I think to change this extention later via:
Settings --> Apps --> Default Apps --> Choose default apps by file type.
Some references:
PyInstaller website and introductory manual.
Official page for step 3.
Unofficial page for step 3, a lot more detailed.

Automate "Right-click + Print" on .xlsx files

I need to automate the act of printing .xlsx file.
I have already seen some answers to this task saying that it is possible by creating a VBA script, as well as some examples. That is not about what my question revolves around.
Thought, I know that it is also possible to right-click on a .xlsx file and click "Print", which does the exact task that I want. It opens Excel, prints the file to the default printer, then closes Excel. (Windows 7, by the way)
So I'm thinking that the work has already been done here.
What process is launched when clicking this "Print" option? Can it be launched via command line, or "clicked" by a python script or something? And if not, why? How can something so easy to click be impossible to automate? I assume a process of some sort must be launched in some way.
Found it!
This task can be easily launched using python.
import os
os.startfile('C:/path/to/the/file.xlsx','print')
This code will launch the same print task. From there, it is pretty trivial for a python developer to automate the task in his scripts.
However, if you do not know much about Python and do not want to learn it now, an easy (or lazy?) way to add it in any automation script would be to save the two lines of code above in a whatever.py file, and launch it via command line (with Python installed, of course).
The context menu print command for Office documents utilizes Dynamic Data Exchange (DDE) and does not directly run a command that can be replicated from the command line.
You can view the content of the commands in the registry. Browse to HKEY_CLASSES_ROOT\.xlsx and look at the (Default) data column. On my machine, "Excel.Sheet.12" is the type of a .xslx file. Then browse to HKEY_CLASSES_ROOT\Excel.Sheet.12\shell\ to see the commands registered for that file type. On my machine, the Print (Default) is "C:\Program Files\Microsoft Office\Office16\EXCEL.EXE" /dde and the "command" is zn=BV5!!!!4!!!!MKKSkEXCELFiles>]-z5hw$l[8QeZZR4_X=$ /dde, none of which is directly useful or accessible for running from a command line.
It will require another program to allow you to access the interface, but there are programs that allow you to make use of DDE from the command line. I recommend Freddy Vulto's Class Exec. More information and a few other similar utilities can be found here.

How to get list of programs which can open a particular file extension in Linux?

Basically I am trying to get list of programs in Linux which are installed and can open particular file extension .jpg for example. If not all, At-least default program should get listed.
Linux (the kernel) has no knowledge on file types to application mapping. If you want to use Gnome programs you can look at https://people.gnome.org/~shaunm/admin-guide/mimetypes-7.html. For KDE there is another mechanism. Each toolkit can define it as it likes. And the programmer can use the defaults or not. So it is simply application specific!
What do you want to achieve?
If you (double) click with a explorer/browser application on an icon or file name, exactly the explorer/browser looks for the file type. Typically this is realized via mime type dictionary. But how a program looks for the file type and maybe execute another program is only related to the programmer who writes that program. The GUI tool-chains like Gnome and KDE have a lot of support for that topic and so you have basic conformity for each family of applications.
If you want to know how a application do the job, start it with strace. But it is quite hard to dig into the huge amount of data.
Also you can take a look for xdg-open. Many programs use this helper to start applications. As an example: If you start Dolphin with strace you will find a line like lstat64("/etc/xdg", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 after clicking on a file.
you can run from command line with:
xdg-open <file-name>
You maybe also want to have a look for applications which registers for file types: /usr/share/applications/*.desktop
Here you can find in each desktop file some mime-types which are registered for the applications. E.g. for audiacity is:
MimeType=application/x-audacity-project;audio/flac;audio/x-flac;audio/basic;audio/x-aiff;audio/x-wav;application/ogg;audio/x-vorbis+ogg;
For your example with jpg:
$ xdg-mime query filetype <any-jpg-file>
image/jpeg
$ grep 'image/jpeg' -R /usr/share/applications/*
...
/usr/share/applications/mimeinfo.cache:image/jpeg2000=kde4-kolourpaint.desktop;gimp.desktop;
So you can see that gimp is one of the default applications for jpg
The place to start looking is at the mailcap (/etc/mailcap) and MIME-types, e.g., in /etc/mime.types in Debian (the filename and path will vary according to who provides it).
The mailcap file gives some rules for opening a file, while MIME-types lists the known filetypes with a tag that allows multiple applications to know about the file types.
Except for embedded or reduced-functionality systems (such as those based on busybox), you would find these files on almost every UNIX-like system.

How do I reveal a file as "selected" in *nix from the command line?

Is there a semi-universal mechanism by which to reveal files as selected in various *nix window managers via the command line? For example, in Windows I can say the following:
explorer.exe /select,C:\TestDir\TestFile.txt
…and Explorer will reveal the file and select it for you. In OS X I can say the following:
osascript -e 'Tell application "Finder" to reveal "MacHD:Users:myaccount:Desktop:filename.txt"'
…and it will do the same. My question is, is there any way to do the exact same thing (somewhat universally) in any of the various popular *nix flavors across window managers? Obviously "Open Containing Folder" is simple enough, but I want to go the extra step of actually opening it with the specific file selected. Any assistance is appreciated.
Best.
I don't about other file managers (other answers can add that) but for nautilus it's been recently fixed
This allows to call nautilus
uri:///path/to/file from the command
line to open uri:///path/to with file
pre-selected.
For OSX AppleScript works for all versions, but if you know you'll be dealing with 10.6 or later you'd be better served by using the -R option for "open". It's around 30 times faster.
open -R "/Volumes/Users/Desktop/file-to-open.txt"
For Linux Nautilus allows for direct calling of the file, a generic solution for GNOME (you won't find one for "Linux") is the "gnome-open" command, which currently could open the directory but won't highlight the file:
"gnome-open /tmp/file.txt"

Resources