Tuesday, November 11, 2008

Scripting in gedit

Hey. I didn't know this before, but you can run your own scripts out of gedit. Well, obviously, I was vaguely aware that this was possible, but I didn't know it was so gorram easy! So I've made some scripts that would help me script in the future.

All these scripts are installed by going to Tools > Extrenal Tools, then pressing New and then filling out the fields - most importantly, the Command(s) field.

Ok, here's what I've concocted.

First of all, I didn't want to go to the command line every time I want to make something executable, so there's this:
 
1 #!/bin/bash
2
3 chmod a+x $GEDIT_CURRENT_DOCUMENT_PATH


It's pretty self explanatory, methinks.

Then, I wanted to run a script from gedit, so I did this script:
 
1 #!/bin/bash
2
3 # Open the document's directory (just in case)
4 cd $GEDIT_CURRENT_DOCUMENT_DIR;
5
6 # Display information about script
7 echo -e "Running script: $GEDIT_CURRENT_DOCUMENT_NAME";
8 echo -e "\tin directory: $GEDIT_CURRENT_DOCUMENT_PATH";
9 echo -e "\n";
10
11 # Run script
12 $GEDIT_CURRENT_DOCUMENT_PATH;


It's mostly straightforward (just follow the comments). It displays a lot of info, because I'm the kind of guy who forgets what he ran just a second ago and needs constant reminding.

Finally, I added the ability to insert parameters with a GUI. I stole this concept from one of those default scripts that are put into gedit.
 
1 #!/bin/bash
2
3 # Get parameters for command from user.
4 params=`zenity --entry --title="Run $GEDIT_CURRENT_DOCUMENT_NAME" --text="with these parameters"`
5
6 # Open the document's directory (just in case)
7 cd $GEDIT_CURRENT_DOCUMENT_DIR;
8
9 # Display information about script
10 echo -e "Running script: $GEDIT_CURRENT_DOCUMENT_NAME";
11 echo -e "\tin directory: $GEDIT_CURRENT_DOCUMENT_PATH";
12 echo -e "\twith params: $params";
13 echo -e "\n";
14
15 # Run script
16 $GEDIT_CURRENT_DOCUMENT_PATH $params;


Here, in line 4 I call zenity to gather the parameters from the user and then just stick them to the end of the name of the script. Simple!

Hope this comes in handy.

The code is also available at GitHub in the directory gedit.

2 comments:

Andrew said...

great stuff, thanks! now i dont have to keep switching back to a terminal window :)

Kondziu said...

Happy I could help.