'Open with' batch commands

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
PatchMonster
Posts: 91
Joined: Thu Aug 07, 2008 4:01 pm UTC
Location: Maryland, USA
Contact:

'Open with' batch commands

Postby PatchMonster » Mon Dec 22, 2008 6:36 pm UTC

I figured it would save me a considerable amount of time if I made a few batch scripts to open with Java classes, files, and Jars which would do the command line codes for me. However, I can't seem to figure out a way to 'pipeline' the opened file into my batch script. I want to be able to assign the batch scripts to "open with" so that when I double click on them it will javac/java -jar/java to the source/jars/classes, respectively.

Pseudo code; what I'm looking for, working with Jar files:

Code: Select all

java -jar [Mystically get the file I opened with]
pause


How would I get the file I assigned it to open with? I'm really newbie at batch scripting and apparently Googling as well :roll: . I know I could set them to open with the java .exe's but it doesn't work out with all of them.

Edit: Vista by the way.
The liberal media has a hidden agenda!

User avatar
greysquare
Posts: 49
Joined: Wed Feb 20, 2008 7:06 pm UTC
Location: Manchester, UK

Re: 'Open with' batch commands

Postby greysquare » Mon Dec 22, 2008 11:31 pm UTC

When you open a file with a program from a file manager (or wherever) the name of the file is passed as the first argument to the program, so inside your script, the name of the file is in the variable $1 .

To make it pause, there is a read function, so

Code: Select all

read -p "Press enter to continue"
will make it wait.

This works if you run it from a command line, but if you open it from a file browser, then nothing happens, because it runs in the background. I found a line of bash script on a forum that solves this, so the complete script is:

Code: Select all

#! /bin/bash
tty -s; if [ $? -ne 0 ]; then gnome-terminal -e "$0 $1"; exit; fi
java -jar $1
read -p "Press enter to continue"


Feel free to substitute gnome-terminal with something more exotic.

edit: i read batch as bash, and assumed UNIX, so yeah...that doesn't work :roll: .

on windows, i think it's:

Code: Select all

java -jar %1
pause

User avatar
PatchMonster
Posts: 91
Joined: Thu Aug 07, 2008 4:01 pm UTC
Location: Maryland, USA
Contact:

Re: 'Open with' batch commands

Postby PatchMonster » Tue Dec 23, 2008 5:20 am UTC

Thank you, GreySquare, helped me out a lot :). Worked as expected the first time.

I knew someone would mistake the title with bash :s. But I duel boot with linux so it will help me out there as well :D.
The liberal media has a hidden agenda!


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 6 guests