I'm working on a script in which I would like to insert a sequence of
commands aimed to save a drawable as PAT file and eventually, when no
longer needed, eliminate the file in question. This trick allows to
create a temporary pattern existing only during the script execution,
so as to fill a layer or a channel with the content of the drawable
this pattern derives from.

The problem is that I can’t choose whatever arbitrary name for the PAT
file, because the destination folder might contain a file with the
same name, which would therefore run the risk of being overwritten and
lastly removed by the script. So, this observation points out the
necessity of implementing a method for creating unique file names by
means of the Scipt-Fu language.

Personally, instead of writing conditional statements which cyclically
verify the existence of a certain file name (with the “file-exists?”
procedure), I have thought about the possibility of using the
“dir-read-entry” procedure. Supposing to store the result of this
command into a variable named F1, if F1 equals <#EOF>, the target
directory is empty, thus any name can be chosen (for instance,
“0.pat”). If the folder is not empty instead, F1 will return the name
of the alphabetically first element (file or folder) inside such
directory. In this case, a solution may be to create a new string
called F2 by connecting the character “!”, the string F1 and, finally,
the string “.pat”. Having noticed that the exclamation point is the
first available symbol for naming files/folders (at least, on Windows
platform), well the string F2 shall precede the string F1 in
alphabetical order, so as to furnish, this way, a valid and unique
file name which doesn't already exist within the folder, as confirmed
by the fact that further invocations of “dir-read-entry” will return
strings of higher order.

In short, here is the set of procedures I imagine:

(define PD (string-append gimp-data-directory "\\patterns\\"))
(define STM (dir-open-stream PD))
(define F1 (dir-read-entry STM))
(if (eof-object? F1)
   (define F2 (string-append PD "0.pat"))
   (define F2 (string-append PD "!" F1 ".pat"))
(file-pat-save 1 IMG DRW1 F2 “” “My Pattern”)
(dir-close-stream STM)
(gimp-context-set-pattern “My Pattern”)
(gimp-edit-bucket-fill DRW2 2 0 100 255 1 0 0)
(file-delete F2)

After doing several tests, my impression is that such a stratagem
should work well. Nevertheless, I'm not totally sure of its
infallibility, because, among other things, I don't know if the
character “!” is really the first valid symbol on all operating
systems supported by GIMP, as well as I can't figure out if there are
any character combinations that invalidate my remarks on the
alphabetical priority performed by appending the exclamation point
before whatever string.
Can anyone clarify my doubts and definitely confirm or deny the
effectiveness of the method I have just stated? Moreover, any
suggestions on how to generate unique file names in a different and
simpler way?

Thanks in advance.
Gimp-developer mailing list

Reply via email to