Language Selection

English French German Italian Portuguese Spanish

Script KATE to Automagically Compile/Execute Programs

Filed under
Software
HowTos


Script KATE to Automagically Compile/Execute Programs

By Gary Frankenbery



I teach high school computer science and programming, and I'm trying to transition my computer lab from MS-Windows only to MS-Windows/Linux dual boot.

For Computer Programming, my students first learn to program with Ruby. I try to keep it simple for these fledgeling programmers, so we have been using the Crimson Text Editor under Microsoft Windows to enter/edit the Ruby source code. The Crimson Text Editor makes it easy to configure a macro to execute whatever Ruby script is being edited: that is, the Crimson macro saves the file, then calls up the Ruby interpreter, and Ruby executes the script. Students accomplish this by simply by pressing the F5 function key while in the Crimson Text Editor.

So, I started wondering if I could configure the KDE Advanced Text Editor (KATE) under Linux to do the same thing with Ruby scripts? Yes, I know that KATE has an embedded terminal where one can activate the terminal window, click in it, and then type, for example,"ruby factorial.rb" and press the enter key, to execute the current Ruby script being edited in KATE. But, I wanted to have the current ruby script in the KATE editor to be both automatically saved, and then executed in a separate Konsole window, all triggered with the simple press of a shortcut key--just like Crimson under MS-Windows.

Could I do this? I've got at least one thing going against me--I'm not a bash shell programmer--I don't really know much shell scripting at all. However, I've got some things working for me--I am a programming teacher so I can program, bash scripting is well documented, and I can be very tenacious. So, I believe that with effort, persistance, and and lot of trial and error, I can figure out how to do this with KATE and Linux. And, so, I did. Then, I wanted to do more.

Since my more advanced computer programming students program use Java (not my personal choice--it's the language mandated by the College Board in the U.S.), I figured out how to configure KATE to automatically save and compile a Java program, and then I finally worked out a script to have KATE call up the Java Run Time Environment to execute the Java program. Obviously, other programming languages (Python comes to mind) can be accommodated as well by customizing these scripts (this, as teachers so often (annoyingly) say, is left as an exercise for the reader Smile).

KATE does permit you to embed scripts, so I'll display some KATE screenshots along the way, and explain how it's done. We'll start by configuring three scripts: Ruby Execute, Java Compile, and Java Execute.Then we'll look at how to configure shortcut keys to activate the scripts under KATE.

  1. With KATE on your screen, select Settings -> Configure Kate... from the
    menu.







  2. In the Configure Dialog box shown below, click External Tools in the left panel, then then click the New... button at the bottom.









  3. The Edit External Tool Dialog comes up (below). Here, we're going to configure KATE for Ruby Program Execution. Type in a description Label for the script (I used Ruby Execute). Then type in the script itself, with all the single apostrophes, double quotes, spaces, and symbols. Because this is tricky, you can copy the text on the next line, and paste it into the script box:


    cd "%directory" && konsole -e sh -c 'ruby "%filename"; printf "%s"
    "Press any key to exit . . . "; read $dummy'







    Since the KATE External Tool scripts like an executable name, just type konsole into the Executable: text box. Next, click the drop down list arrow by the
    Save: button, and select Current Document. Click the OK button. This window will
    close. Click OK again. You should now be back at the KATE main window.


  4. Now, let's configure the Java Compile script. Starting at the KATE main window, do steps 1 and 2 again. This time, edit the External Tool Dialog to look like the screen clip below. Again, the script code needs to be exact. Here it is so you can copy and paste, if necessary:



    cd "%directory" && konsole -e sh -c 'javac "%filename"; printf "%s" "Press any key to exit . . . "; read $dummy'








    Click the OK button, and at the next window, click OK again. You should be back at the KATE main window.

  5. Time to configure the last script to execute the Java program. At the KATE main window, do steps 1 and 2 above one more time. You should edit the External Tool Dialog to look like the clip below. Again, here's the scipt command which you can copy and paste:


    cd "%directory" &&  CF="%filename" konsole -e  sh -c 'java ${CF%.*}; printf  "%s" "Press any key to exit . . . "; read $dummy'








    Finally, we are done with the scripting part. Next we'll setup shortcut keys to launch our scripts.

  6. To get started, at the main KATE window, click the Settings->Configure Shortcuts menu choices as shown.








  7. You will see the Configure Shorcuts dialog box window shown below.



    Click the Java Compile script, click the button next to Custom (at the bottom), and press the key(s) you want to launch this script. You can see that I chose the F12 key to compile the current Java program. Click OK



    For the Java Execute script, I chose Shift-F12.




    For executing a Ruby program, I chose I chose Alt+R.



    So, after you configure the three shortcuts, you're done! You can now edit your programming source code in KATE, and quickly save, compile, and execute your Ruby and Java source code with these scripts simply by
    pressing the shortcut key(s).







Script Kate Article

By default, Debian derived Linux distros execute sh scripts using dash rather than bash. Because dash won't work correctly for the above Kate scripts, just substitute bash in place of sh in the above scripts.

Gary Frankenbery
January 4, 2009

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

More in Tux Machines

Open source docks with mainstream vendors

Open source and mainstream are joining forces this week as the Docker software containerisation platform comes under the spotlight at technology-focused network and information sessions in Cape Town and Johannesburg. "The diversity of our partners at the event − Docker, Microsoft Azure, Atlassian, SUSE and HPE – is a clear indication of the excitement around the Docker platform," says Muggie van Staden, MD of Obsidian Systems. Read more

What’s the best Linux firewall distro of 2017?

You don’t have to manage a large corporate network to use a dedicated firewall. While your Linux distro will have an impressive firewall – and an equally impressive arsenal of tools to manage it – the advantages don’t extend to the other devices on your network. A typical network has more devices connected to the internet than the total number of computers and laptops in your SOHO. With the onslaught of IoT, it won’t be long before your router doles out IP addresses to your washing machine and microwave as well. The one thing you wouldn’t want in this Jetsonian future is having to rely on your router’s limited firewall capabilities to shield your house – and everyone in it – from the malicious bits and bytes floating about on the internet. A dedicated firewall stands between the internet and internal network, sanitising the traffic flowing into the latter. Setting one up is an involved process both in terms of assembling the hardware and configuring the software. However, there are quite a few distros that help you set up a dedicated firewall with ease, and we’re going to look at the ones that have the best protective open source software and roll them into a convenient and easy to use package. Read more

Zorin OS 12 Business Edition Launches with macOS, Unity, and GNOME 2 Layouts

Three months after launching the biggest release ever of the Ubuntu-based operating system, the Zorin OS team is today announcing the availability of Zorin OS 12 Business Edition. Based on Ubuntu 16.04 LTS (Xenial Xerus) and powered by the long-term supported Linux 4.4 kernel, Zorin OS 12 Business Edition ships with the innovative Zorin Desktop 2.0 desktop environment that offers multiple layouts for all tastes. These means that you can make your Zorin OS 12 desktop look like macOS, GNOME 2, or Unity with a click. Read more

GNOME and Other Software

  • Nautilus 3.24 – The changes
    Since Nautilus was created, if a user wanted to open a folder where the user didn’t have permissions, for example a system folder where only root has access, it was required to start Nautilus with sudo. However running UI apps under root is strongly discouraged, and to be honest, quite inconvenient. Running any UI app with sudo is actually not even supported in Wayland by design due to the security issues that that conveys.
  • GNOME hackaton in Brno
    Last week, we had a presentation on Google Summer of Code and Outreachy at Brno University of Technology. Around 80 students attended which was a pretty good success considering it was not part of any course. It was a surprise for the uni people as well because the room they booked was only for 60 ppl.
  • Peek Gif Recorder Gets Updated, Now Available from a PPA
    Peek, the nifty animated gif screen capture app for Linux desktops, has been updated. Peek 0.9 reduces the size of temporary files, adds a resolution downsampling option (to help the app use fewer resources when rendering your gif), and introduces fallback support for avconf should ffmpeg be unavailable.
  • Cerebro is an Open Source OS X Spotlight Equivalent for Linux
    Billed as an ‘open-source productivity booster with a brain’, Cerebro is an Electron app able to run across multiple platforms. It’s an extendable, open-source alternative to Spotlight and Alfred on macOS, and Synapse, Kupfer, Ulauncher, GNOME Do, and others on Linux.
  • JBoss Fuse 6.3 integration services for Red Hat OpenShift released
    Red Hat announced the latest update to the Red Hat JBoss Fuse-based integration service on Red Hat OpenShift. With the growth of the Internet of Things (IoT), cloud-based SaaS systems, and new data streams, organizations can face increasing pressure to more quickly deliver innovative new services. Traditional centralized, monolithic ESB-style integration approaches are often ill-suited to support the business in responding to this pressure.
  • Fedora 25: The perf linux tool.
  • Meet the chap open-sourcing US govt code – Paul, an ex-Microsoft anti-piracy engineer [Ed: Used to work for Microsoft and now spreads the GPL ("cancer" according to Microsoft) in the US government]
    The manager of the project, Berg said, really wanted to release MOOSE as open source, but didn't know how to do so. As a result it took 18 months to traverse government bureaucracy and to obtain the necessary permissions. It's now available under the GPL 2.1 license.