Creating unique file names with Script-Fu
This discussion is connected to the gimp-developer-list.gnome.org mailing list which is provided by the GIMP developers and not related to gimpusers.com.
This is a read-only list on gimpusers.com so this discussion thread is read-only, too.
Creating unique file names with Script-Fu | Gino D | 23 Jun 19:58 |
Creating unique file names with Script-Fu | saulgoode@flashingtwelve.brickfilms.com | 23 Jun 22:03 |
Creating unique file names with Script-Fu | Gino D | 24 Jun 23:29 |
Creating unique file names with Script-Fu | saulgoode@flashingtwelve.brickfilms.com | 25 Jun 03:27 |
Creating unique file names with Script-Fu | Rob Antonishen | 25 Jun 15:32 |
Creating unique file names with Script-Fu | Gino D | 27 Jun 13:08 |
Creating unique file names with Script-Fu | Kevin Cozens | 26 Jul 20:33 |
Creating unique file names with Script-Fu | Jon Cruz | 27 Jun 07:14 |
Creating unique file names with Script-Fu | Gino D | 29 Jun 10:28 |
Creating unique file names with Script-Fu | paynekj | 08 Jul 12:09 |
Creating unique file names with Script-Fu | Gino D | 08 Jul 21:45 |
Creating unique file names with Script-Fu
Hi.
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 , 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-patterns-refresh)
(gimp-context-set-pattern “My Pattern”)
(gimp-edit-bucket-fill DRW2 2 0 100 255 1 0 0)
(file-delete F2)
(gimp-patterns-refresh)
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.
Creating unique file names with Script-Fu
Quoting Gino D :
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.
I do not believe it is possible for a Script-fu to delete files. You will need to either do this outside of GIMP or write a plug-in.
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.
:
:
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?
I don't have time right now to review your approach, however, ...
The following code snippet uses 'gimp-temp-name' -- which generates a filename that has an extremely good chance of being unique -- however, just to be certain, an attempt is made to open the file (in your directory, not the ~/.gimp/tmp directory). If the open succeeds, the file is closed and the process repeated.
(let ((basename "")
(filename "")
(port #t))
(while port
(set! basename (car (last (strbreakup (car (gimp-temp-name "pat")) "/"))))
(set! filename (string-append "/path/to/directory/" basename))
(set! port (open-input-file filename))
(when port
(close-output-port port)
)
)
; filename is unique & of the form "/path/to/directory/gimp-temp-######.pat"
For better cross-platform support, you should replace the slashes above with the DIR-SEPARATOR system constant.
Creating unique file names with Script-Fu
Hi.
2010/6/23 :
I do not believe it is possible for a Script-fu to delete files. You will need to either do this outside of GIMP or write a plug-in.
Deletion of files is made possible by the “file-delete” procedure, as shown at the end of the sequence of commands I proposed in my initial post.
The following code snippet uses 'gimp-temp-name' -- which generates a filename that has an extremely good chance of being unique...
The idea of employing the “gimp-temp-name” procedure for generating a most likely unique file name is very interesting. Thanks for your suggestion and the snippet you devised. Pity that such procedure affects only the GIMP temporary directory; if it were effective in every folder, it would be great, especially for the issue we are discussing.
By chance, can you tell me if my previous conjectures about the priority of the exclamation point are correct, besides on Windows, on other operating systems too?
Creating unique file names with Script-Fu
Quoting Gino D :
I do not believe it is possible for a Script-fu to delete files. You will need to either do this outside of GIMP or write a plug-in.
Deletion of files is made possible by the “file-delete” procedure, as shown at the end of the sequence of commands I proposed in my initial post.
I hadn't realized that the TinyScheme Extensions had been enabled for Script-fu. If you choose to use the 'gimp-temp-name' approach I previously proposed then 'file-exists?' would be better than opening the file.
The following code snippet uses 'gimp-temp-name' -- which generates a filename that has an extremely good chance of being unique...
The idea of employing the “gimp-temp-name” procedure for generating a most likely unique file name is very interesting. Thanks for your suggestion and the snippet you devised. Pity that such procedure affects only the GIMP temporary directory; if it were effective in every folder, it would be great, especially for the issue we are discussing.
Even within the GIMP temporary directory, 'gimp-temp-name' does not actually guarantee uniqueness against files in the directory, so the only inconvenience of using 'gimp-temp-name' is having to parse out the filename from the full filepath.
By chance, can you tell me if my previous conjectures about the priority of the exclamation point are correct, besides on Windows, on other operating systems too?
Your approach would not succeed on GNU/Linux platforms because 'dir-read-entry' does not ensure that the entries are returned in alphabetical order. Even on Windows I suspect you will find that the order entries are retrieved is dependent upon the type of filesystem (FAT, VFAT, NTFS all behave differently).
Creating unique file names with Script-Fu
Your approach would not succeed on GNU/Linux platforms because 'dir-read-entry' does not ensure that the entries are returned in alphabetical order. Even on Windows I suspect you will find that the order entries are retrieved is dependent upon the type of filesystem (FAT, VFAT, NTFS all behave differently).
Add in a merge sort (i used these three functions):
(define split
(lambda (ls)
(letrec ((split-h (lambda (ls ls1 ls2)
(cond
((or (null? ls) (null? (cdr ls)))
(cons (reverse ls2) ls1))
(else (split-h (cddr ls)
(cdr ls1) (cons (car ls1) ls2)))))))
(split-h ls ls '()))))
(define merge
(lambda (pred ls1 ls2)
(cond
((null? ls1) ls2)
((null? ls2) ls1)
((pred (car ls1) (car ls2))
(cons (car ls1) (merge pred (cdr ls1) ls2)))
(else (cons (car ls2) (merge pred ls1 (cdr ls2)))))))
;pred is the comparison, i.e.
Creating unique file names with Script-Fu
On Jun 23, 2010, at 10:58 AM, Gino D wrote:
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 a minor note, I can confirm that "!" is not the first valid symbol on all operating systems. Most operating systems allow for many more characters than Windows does.
Creating unique file names with Script-Fu
2010/6/25 Rob Antonishen :
Add in a merge sort (i used these three functions):
(define split (lambda (ls)
...
(split-h ls ls '()))))(define merge (lambda (pred ls1 ls2)
...
(else (cons (car ls2) (merge pred ls1 (cdr ls2)))))))(define merge-sort (lambda (pred ls)
...
(merge-sort pred (cdr splits))))))))Then get a sorted directory like like so: (set! varFileList (merge-sort string
Well done, Rob, and what an interesting revelation! Though not so long ago, indeed, I have collected all the Script-Fu procedures returned by the “oblist” command invocation in a text file, with the purpose of having them always within reach, nevertheless I hadn’t noticed the existence of the “file-glob” procedure before now. Hence, thanks a lot for highlighting this command, as well as for contriving those powerful procedures whereby it is possible to achieve a sorted directory.
The approach you suggest is a good alternative to the employment of the “dir-read-entry” procedure, which actually might be unreliable in certain cases, as Saulgoode points out, depending to what kind of filesystem the target directory belongs to.
In fact, after obtaining an alphabetically ordered list with the names of the folder's elements, it would be possible to exploit even the last element rather than the first one, by producing a new string that concatenates this ending string, the character “z”, and finally the string “.pat”, so as to get a file name that should be unique and non-confrontational.
Thank you again for your contribution.
Creating unique file names with Script-Fu
2010/6/27 Jon Cruz :
On Jun 23, 2010, at 10:58 AM, Gino D wrote:
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 a minor note, I can confirm that "!" is not the first valid symbol on all operating systems. Most operating systems allow for many more characters than Windows does.
That's exactly what I feared. However, now it's clear that this kind of problem can be avoided by following the method previously proposed by Rob, which, being based on sorted lists of file names, is sure to be OS independent, unlike the "dir-read-entry" command.
Anyway, thanks for the information.
- postings
- 16
Creating unique file names with Script-Fu
Would it not be possible to create a reasonably unique name using the tiny-fu, ftx extension, (time) function? That should give you a name unique to the current time, at least if you don't try and use it more than once in the current second.
You'd probably still need to do some collision avoidance just to be safe.
Creating unique file names with Script-Fu
2010/7/8 Kevin :
Would it not be possible to create a reasonably unique name using the tiny-fu, ftx extension, (time) function? That should give you a name unique to the current time, at least if you don't try and use it more than once in the current second.
You'd probably still need to do some collision avoidance just to be safe.
-- Kevin (via www.gimpusers.com)
Yes, this is another viable possibility. Thanks for your suggestion.
Creating unique file names with Script-Fu
saulgoode@flashingtwelve.brickfilms.com wrote:
I hadn't realized that the TinyScheme Extensions had been enabled for Script-fu.
Script-Fu in GIMP uses a subset of the TSX (TinyScheme eXtensions) module. The 'getenv', 'system', and various additional functions which supported use of sockets were left out.