[Gimp-developer] filetype plug-in to get type of entity (file/directory)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

this is a trivial plug-in that implements one single call:

file-get-type <path>

returning the type of the entity pointed to by 'path' (currently only: 0 = 
directory, 1 = file). (Symbolic constants like TYPE-DIRECTORY or such should 
be added if this is included.)

I'm also attaching a script that recursively traverses a given directory and 
returns a list of path names to (exclusively) regular files that are located 
inside this directory or any of its sub-directories for further processing. 
Try with

(all-files "/home/yourname/testdir")

on the Script-FU console, where "testdir" is a directory containing a few 
directories and files, NOT a few thousands (can cause problems on the 
console).

I'm giving this another shot (after bug #145370, taking your feedback into 
account - TinyScheme currently lacks a similar feature), because it seems to 
me that batch-processing is the single most often asked issue on the 
comp.graphics.apps.gimp NG, and it would be great if we could just give 
people a script similar to the sample I'm attaching, tell them to copy it to 
~/.gimpXY/scripts/ and let's rock, without the need to download and compile a 
separate plug-in. After the transition to TinyScheme, they would simply type:

gimp -c -i -b "(map my-specialized-conversion (all-files "allmypictures"))"

no matter which platform they are on.

Markus.
;; sample recursive traversal and return all files (needs filetype plug-in)
;; written by M. Triska <triska@xxxxxx>, July 16th 2004



(define (all-files directory)
  (let* ((outlist '())
	 (entitylist (cadr (file-glob (string-append directory "/*")))))
    (while (not (null? (car entitylist)))
	   (if (= (car (file-get-type (car entitylist))) 1)
	       (set! outlist (cons (car entitylist) outlist)) ;; file
	       (set! outlist (append outlist (all-files (car entitylist)))))
	   (set! entitylist (cdr entitylist)))
    outlist))

	
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */


#include <string.h>

#include <glib.h>
#include "libgimp/gimp.h"

/* Utility function useful for batch processing
 * Author: Markus Triska <triska@xxxxxx> 
 * 
 * 0.8 initial version, July 16h 2004
 */

#define FILETYPE_DIRECTORY 0
#define FILETYPE_REGULAR   1

static void   query      (void);
static void   run        (const char    *name,
                          gint      nparams,
                          const GimpParam  *param,
                          gint     *nreturn_vals,
                          GimpParam **return_vals);

GimpPlugInInfo PLUG_IN_INFO =
{
  NULL,    /* init_proc */
  NULL,    /* quit_proc */
  query,   /* query_proc */
  run,     /* run_proc */
};

MAIN ()

static void
query ()
{
  static GimpParamDef filetype_args[] =
    {
      { GIMP_PDB_STRING, "path", "Path of entity to check" }
    };

  static GimpParamDef filetype_return_vals[] =
    {
      { GIMP_PDB_INT32, "type", "Type of entity: DIRECTORY (0), REGULAR (1)" }
    };

  gimp_install_procedure ("file_get_type",
                          "Get type of an entity pointed to by 'path'",
                          "Useful in scripts (e.g., batch processing)",
                          "Markus Triska <triska@xxxxxx>",
                          "Markus Triska",
                          "2004",
                          NULL,
			  NULL,
                          GIMP_PLUGIN,
			  G_N_ELEMENTS (filetype_args),
			  G_N_ELEMENTS (filetype_return_vals),
			  filetype_args,
			  filetype_return_vals);
}

static void
run (const char    *name,
     gint      nparams,
     const GimpParam  *param,
     gint     *nreturn_vals,
     GimpParam **return_vals)
{
  static GimpParam values[2];

  *nreturn_vals = 1;
  *return_vals = values;

  values[0].type = GIMP_PDB_STATUS;
  values[0].data.d_status = GIMP_PDB_CALLING_ERROR;

  if (strcmp (name, "file_get_type") == 0)
    {
      *nreturn_vals = 2;
      values[0].data.d_status = GIMP_PDB_SUCCESS;
      values[1].type = GIMP_PDB_INT32;
      if (g_file_test (param[0].data.d_string, G_FILE_TEST_IS_REGULAR)) {
	values[1].data.d_int32 = FILETYPE_REGULAR;
      } else if (g_file_test (param[1].data.d_string, G_FILE_TEST_IS_DIR)) {
	values[1].data.d_int32 = FILETYPE_DIRECTORY;
      }
    }
}

[Index of Archives]     [Video For Linux]     [Photo]     [Yosemite News]     [gtk]     [GIMP for Windows]     [KDE]     [GEGL]     [Gimp's Home]     [Gimp on GUI]     [Gimp on Windows]     [Steve's Art]

  Powered by Linux