You are not logged in.

#1 2015-07-13 19:47:13

3p1k5auc3
Member
Registered: 2015-05-01
Posts: 30

[SOLVED]Set a default program to open contents in a folder.

I was wondering if there was any way to set a default program to open all files in a specified folder. For example if I have two folders called Ruby and Python, could I set all executable files in the respective folders to only be opened with their respective interpreters, thus removing the need for me to type ruby or python before each filename?

Last edited by 3p1k5auc3 (2015-07-17 06:59:58)


Real programmers use a magnet and a steady hand.

Offline

#2 2015-07-13 19:58:06

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,551
Website

Re: [SOLVED]Set a default program to open contents in a folder.

Err ... why not just give those files a proper shebang.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2015-07-13 20:33:52

3p1k5auc3
Member
Registered: 2015-05-01
Posts: 30

Re: [SOLVED]Set a default program to open contents in a folder.

Ah, I see. I did some research on what a shebang was, and figured out how to do that. I had no idea that was even possible, as I am new to Linux. Thanks a lot, that is very helpful. I would like to know if there is a way to set a default program for all files in a folder, if it exists, but thank you very much for that suggestion, it works just as well.


Real programmers use a magnet and a steady hand.

Offline

#4 2015-07-13 21:29:21

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,551
Website

Re: [SOLVED]Set a default program to open contents in a folder.

Without a proper shebang you cant execute them at all without specifying an interpreter.  But if you just want a "generic" interpreter that will infer the file type based solely on the directory it's in (rather than an file extension or shebang) then a little script like the following would do:

#!/bin/bash

case $(basename $(pwd)) in
   python) python $@ ;;
   ruby) ruby $@ ;;
   # ... other dirs) interpreters
esac

Or if you want it to work on where the target file is rather than where you currently are that first line would be the following:

case $(basename $(dirname $(realpath $1))) in

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2015-07-13 21:35:21

3p1k5auc3
Member
Registered: 2015-05-01
Posts: 30

Re: [SOLVED]Set a default program to open contents in a folder.

How would one activate the script? Would you have to use the ruby/python program as an argument, similar to how you would normally execute a ruby script i.e. ruby script.rb?


Real programmers use a magnet and a steady hand.

Offline

#6 2015-07-13 21:43:47

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,551
Website

Re: [SOLVED]Set a default program to open contents in a folder.

No that would defeat the purpose.  This script takes the place of the interpreters - isn't that what you wanted?  Call this script "script", then you could do something like the following:

$ script ~/code/python/my_first_python_script

$ script ~/code/ruby/my_ruby_script

In the first case "script" will see that the argument provided to it is in a folder called "python" so it executes it with the python interpreter.  In the second case, it sees the directory is named ruby, and so it uses ruby.  I thought that's what you were asking.  If not, please be more clear on what you want.  In any case, this all seems like a bad hackish approach.  Just use proper shebangs.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2015-07-13 21:58:59

3p1k5auc3
Member
Registered: 2015-05-01
Posts: 30

Re: [SOLVED]Set a default program to open contents in a folder.

Will do. I appreciate your help. As I said, I am new to Linux in general. I was just exploring my options. Thanks.


Real programmers use a magnet and a steady hand.

Offline

Board footer

Powered by FluxBB