Configuring Shortcuts | Configuring 4Pane | Devices and Editors

## User-defined Tools

One of the good things about using a gui file manager is being able to select several files, then do something to them e.g. rename or delete them. But rename etc has to be built into the file manager. Wouldn't it be nice if it were possible to extend this idea to process selected files using commands of your own. Well, with 4Pane you can do exactly this. A user-defined tool can call any available commands or scripts, to which you can pass selected items as parameters.
Though you can run programs that don't need parameters, such as 'df -h', you'll usually want to use them. This is how:

- %s is replaced by a single selected file or directory.
- %f is replaced by a single selected file only; directories are ignored.
- %d is replaced by a single selected directory only; files are ignored.
- %a is replaced by all the items selected in the active pane.
- %b is replaced by a single selected item from both of the file-views.
- %p will ask the user each time the command is run for a parameter to pass to the application.
- %% is replaced by %, in case you need to use one in a script.

Sometimes you'll want finer control over which pane the items come from. For the 's', 'f', 'd' and 'a' parameters this can be done using the following modifiers (these examples use %s, but the other parameters could be substituted):

- %1s An item from the left (or top if the split is horizontal) dir-view.
- %2s An item from the left (or top) file-view.
- %3s An item from the right (or bottom) dir-view.
- %4s An item from the right (or bottom) file-view.
- %As An item from the currently Active pane.
- %Is An item from the Inactive pane (the equivalent pane on the opposite side).

Since 4Pane 6.0 you can also use:

- %5s An item from the Active left (or top) dir-view.
- %6s An item from the Inactive (or top) dir-view.
- %7s An item from the Active right (or bottom) file-view.
- %8s An item from the Inactive right (or bottom) file-view.

Note that some combinations aren't sensible (e.g. %3f: you can't select a file in a dir-view); and multiple modifiers aren't permitted (e.g. %A3d).
These parameters can be combined or repeated as appropriate e.g. ~/myscript %p %a %p will prompt the user for two string parameters, then pass these and all the currently selected items to myscript.
diff -u %2f %4f will diff two selected files, one from each file-view ("diff -u %b" or "diff -u %Af %If" would do the same if a file-view is active).
rsync -avu %3d/ %1d will update the contents of the selected directory in the left dir-view to match those of the right dir-view selection (the extra '/' is needed by rsync).
You can run a tool with superuser privileges without opening a terminal: tick the box when you Add the tool. However this ability is limited: the command should be one that quickly returns, e.g. fdisk %p, not one that will linger like kwrite %f; and as there's no terminal there is nowhere to display output, so there's no sense in doing something like fdisk -l. Instead try using an outside front-end such as kdesu or gksu e.g. kdesu kwrite %f; or in a terminal by prefixing 'su -c ' or 'sudo '.
The available user-defined commands can be run from the Tools > Run a Program menu or from the context menu. Alternatively you can give a tool a keyboard shortcut: see Configuring Shortcuts.
Add a new tool
4Pane comes with a "starter-pack" of these tools, but the idea is to add your own. You can do this in the Tools section of Options > Configure 4Pane. In the first box of the Add a tool sub-page, type in the full command e.g. myscript %f %p.
If the command needs to run in a terminal e.g. df -h, tick the "Run in Terminal" tickbox below. Some terminal commands (again df -h is a good example) display their output in the terminal, then immediately close before you can read the results. To prevent this, tick the "Keep terminal open" box.
The next two tick-boxes are 'Refresh pane after' and 'Refresh opposite pane too'. They are for commands that don't run in a terminal, and tell 4Pane to update either the current pane, or both of them, once the command has finished running.
You'd want this for a command that affects the files in one of the visible panes. For example, this mini-script uses imageMagick to resize all the selected files to quarter-size:
for image in %a; do path=`dirname ${image}` && filename=`basename ${image}` && name=`echo ${filename} | cut -d\'.\' -f1` && ext=`echo ${filename} | cut -d\'.\' -f2` && convert ${image} -resize 25%% ${path}/${name}-25%%.${ext}; done
You'd like to be able to see that the size of the files has changed when the command has finished running, so tell 4Pane to update the current pane by ticking the 'Refresh pane after' box.
The last tickbox is for when you want to run a program as root, and don't need to be shown any detailed output; for example, editing a root-owned file in a text editor. If you do need to view output you need to run in a terminal instead.
In the second box type the label: the words that you want to be seen in the Tools menu; if you leave this blank, the command itself will be used.
Lastly you need to choose to which menu or submenu the command should be added. The default is Run a Program, but you can also select any available submenus. You can create a new submenu, or delete the currently selected one, using the buttons on the right.
Edit an existing tool
The next sub-page lets you edit existing tools. Select from the dropdown box the label of the tool to be edited; the command and "in terminal" etc status will be shown below. When you have the correct tool selected. click the "Edit this Command" button. You will then be able to alter any of the entries. When you've finished click the button again (it will have changed its label to "Click when finished").
Delete an existing tool
The last sub-page handles deletion. Select the tool to delete from the dropdown box, then click the "Delete this command" button. An "Are you sure" dialog will appear, to allow you to change your mind.
Once the data has changed, the Apply and Cancel buttons become enabled. When you've finished all your alterations, click "Apply" (or Cancel if you've changed your mind). Until you do this, the results are not saved.
If you've finished configuring, click "Finished" to close the Configure dialog.