3

I use Latex to write my documents. Latex creates MANY auxiliary files to compile a document. I often times want to clean my working directory.

While I worked on Windows, I used to keep a .bat file in the working directory that looked like this:

del *.aux
del *.pdf
del *.log
del *.bak
del *.gz
del *.bbl
del *.blg

which I could just click on to get rid of all auxiliary files in the current directory.

Now, I want to do the same on my Mac. I have created a .sh file like this:

#!/bin/bash

echo "Cleaning files..."

rm *.aux
rm *.bak
rm *.bbl
rm *.blg
rm *.gz
rm *.log
rm *.pdf

echo "Done!"

which I know I can run (i.e. invoke from command line), but I cannot click on - which is more convenient because not always I will be using Terminal.

I should stress the fact that the script should delete the files in the directory where it was clicked from!

How can I convert this script into a "clickable" one?

I appreciate any input!

thiagoveloso
  • 2,537
  • 3
  • 28
  • 57
  • 1
    You'll need to dynamically find the path to the script file: http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in – Alexander O'Mara Jul 06 '15 at 06:02
  • 1
    Clicking doesn't *belong* to any directory. Do you mean it should remove some files in the directory your Terminal is in? – Mark Setchell Jul 06 '15 at 06:42
  • @MarkSetchell I can right-click it and associate it to Terminal.app, which actually opens a Terminal with the call: `/path_of_script.sh; exit;` – thiagoveloso Jul 06 '15 at 06:46
  • 1
    An AppleScript added to the toolbar that gets the current folder and runs the script would work. – empedocle Jul 06 '15 at 07:26
  • @AlexanderO'Mara your link as really helpful, I very much appreciate it. – thiagoveloso Jul 06 '15 at 07:28
  • @empedocle would an AppleScript have any advantage over a shell script? – thiagoveloso Jul 06 '15 at 07:28
  • @thiagoveloso If you want to run it from the Finder toolbar, an AppleScript can get the current folder of that Finder window. From that, you can either delete through AppleScript or pass the folder to your shell script. – empedocle Jul 06 '15 at 07:29

2 Answers2

2

Based on this reference (Getting the source directory of a Bash script from within), I ended up solving my problem with the following code:

#!/bin/bash

DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

echo "Cleaning files..."

cd $DIR
rm *.aux
rm *.bak
rm *.bbl
rm *.blg
rm *.gz
rm *.log
rm *.pdf

echo "Done!"

#read -p "Press [Enter] to continue..."

It works very well to clean all the nasty files left behind by Texmaker!

Community
  • 1
  • 1
thiagoveloso
  • 2,537
  • 3
  • 28
  • 57
1

See this answer for making a shell script double-clickable, but note that there is no concept of a "current directory" when you launch a script from the Finder.

Community
  • 1
  • 1
Paul R
  • 208,748
  • 37
  • 389
  • 560
  • A `command` file is by nature directed to the home directory, and hence it does not work for me. – thiagoveloso Jul 06 '15 at 06:53
  • 1
    Yes, that's why I said that there is no "current directory" when launching from the Finder - you'll need to designate a directory somehow. – Paul R Jul 06 '15 at 06:59