GIMP Chat
http://gimpchat.com/

Modify "Save as+export script to prevent overwriting
http://gimpchat.com/viewtopic.php?f=9&t=5949
Page 1 of 1

Author:  PhotoComix [ Sat Dec 08, 2012 1:33 pm ]
Post subject:  Modify "Save as+export script to prevent overwriting

I love this script make me spare a lot of time

Only problem
1) the script as default preserve and reuse the last name used to save AS +export
2) the script do not give warning but overwrite

The combination make easy overwrite files if distract, will be possible

1 ) Let pop out the warning about overwriting requiring to confirm

or even

2 ) automatically rename (ie adding to the name , if already used ,a prefix as a progressive number )

PLEASE NOTE even if are on the same file are 2 scripts, the first "save+ export" is fine as it is, it should overwrite with a updated version (this one works only on image already saved as XCF )

It is in the second script "Save AS + Export" that will be useful a warning about overwriting

Here the script
; save_and_export.scm
; by Rob Antonishen
; http://silent9.com

; Version 1.2 (20091005)

; Changes:
; v1.1 added messagebox handler
; v1.2 added second script to allow save of xcf files or save-as to allow specifying the xcf

; Description
; Saves the current XCF file as well as a png and jpg in the
; same directory with the same name  It uses the defaults for png and jpg
; and will overwrite any file already there.
;
; 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.
;
; The GNU Public License is available at
; http://www.gnu.org/copyleft/gpl.html

(define (save-as-and-export img inLayer inDir inFileRoot exportpng? exportjpg?)
  (let*
    (
      (shortname (string-append inFileRoot ".xcf"))
      (fullname (string-append inDir DIR-SEPARATOR shortname))
      (realfullname (car (gimp-image-get-filename img)))
      (dupimage (car (gimp-image-duplicate img)))
      (temp 0)
     )

    ;save the xcf file
    (gimp-progress-set-text (string-append "Saving: " shortname))
    (gimp-xcf-save 0 img (car (gimp-image-get-active-drawable img)) fullname shortname)

    ;reload the xcf - this is necessary if the original file was not an xcf or if the filname/path was changed!
    (set! temp (car (gimp-file-load RUN-NONINTERACTIVE fullname shortname)))
    (gimp-displays-reconnect img temp)
    (gimp-image-clean-all temp)

    ;remove filename from fullname to get the path
    (set! fullname (substring fullname 0 (- (string-length fullname) (string-length shortname))))
     
    ;strip old extension
    (while (not (string=? (substring shortname (- (string-length shortname) 1)) "."))
      (set! shortname (substring shortname 0 (- (string-length shortname) 1)))
    )
    (set! shortname (substring shortname 0 (- (string-length shortname) 1)))
       
    (gimp-image-merge-visible-layers dupimage CLIP-TO-IMAGE)
       
    (when (equal? exportpng? TRUE)
      ;save the png file
      (gimp-progress-set-text (string-append "Saving: " (string-append shortname ".png")))
      (file-png-save-defaults RUN-NONINTERACTIVE dupimage (car (gimp-image-get-active-drawable dupimage))
                      (string-append fullname shortname ".png")
                      (string-append shortname ".png")))

    (when (equal? exportjpg? TRUE)
      ;save the jpeg file
      (gimp-progress-set-text (string-append "Saving: " (string-append shortname ".jpg")))
      (gimp-file-save RUN-NONINTERACTIVE dupimage (car (gimp-image-get-active-drawable dupimage))
                      (string-append fullname shortname ".jpg")
                      (string-append shortname ".jpg")))
           
    ;clean up
    (gimp-image-delete dupimage)
  )
)

(script-fu-register "save-as-and-export"
                  "<Image>/File/Save/Save-As XCF and Export..."
                     "Saves the current file as a specified XCF and exports a png and jpg as selected."
                     "Rob Antonishen"
                     "Rob Antonishen"
                     "Sept 2009"
                     "RGB* GRAY* INDEXED"
                     SF-IMAGE      "image"      0
                     SF-DRAWABLE   "drawable"   0
                SF-DIRNAME    "Save-as Directory" ""
                SF-STRING     "Save-as filename (without .xcf)" "filename"
                     SF-TOGGLE     "Export a png" TRUE
                     SF-TOGGLE     "Export a jpg" TRUE
)            

(define (save-and-export img inLayer exportpng? exportjpg?)
  (let*
    (
      (shortname (car (gimp-image-get-name img)))
      (fullname (car (gimp-image-get-filename img)))
      (handler (car (gimp-message-get-handler)))
    )

    (gimp-message-set-handler MESSAGE-BOX)

    (if (= (string-length fullname) 0)
      (gimp-message "The file must be saved before a backup can be made.")
      (if (not (string-ci=? (substring shortname (- (string-length shortname) 4)) ".xcf"))
        (gimp-message "The file must be an xcf.")
        (save-as-and-export img inLayer
                        (substring fullname 0 (- (string-length fullname) (string-length shortname)))
                        (substring shortname 0 (- (string-length shortname) 4))
                        exportpng? exportjpg?)
      )
    )
    (gimp-message-set-handler handler)
  )
)

(script-fu-register "save-and-export"
                  "<Image>/File/Save/Save XCF and Export..."
                     "Re-saves the current XCF and exports a png and jpg as selected."
                     "Rob Antonishen"
                     "Rob Antonishen"
                     "Sept 2009"
                     "RGB* GRAY* INDEXED"
                     SF-IMAGE      "image"      0
                     SF-DRAWABLE   "drawable"   0
                     SF-TOGGLE     "Export a png" TRUE
                     SF-TOGGLE     "Export a jpg" TRUE
)

Author:  PhotoComix [ Mon Dec 10, 2012 6:53 am ]
Post subject:  Re: Modify "Save as+export script to prevent overwriting

:bump

Author:  saulgoode [ Mon Dec 10, 2012 3:38 pm ]
Post subject:  Re: Modify "Save as+export script to prevent overwriting

PhotoComix wrote:
1 ) Let pop out the warning about overwriting requiring to confirm

Not possible (with Script-fu).
PhotoComix wrote:
2 ) automatically rename (ie adding to the name , if already used ,a prefix as a progressive number )

Would you want both the XCF and PNG/JPG files renamed? Or the XCF overwritten and the PNG/JPG renamed? What if the XCF exists and the PNG/JPG does not, would still you want the PNG/JPG renamed? You need to provide more detail about what you want.

Author:  PhotoComix [ Tue Dec 11, 2012 4:37 pm ]
Post subject:  Re: Modify "Save as+export script to prevent overwriting

Quote:
Would you want both the XCF and PNG/JPG files renamed? Or the XCF overwritten and the PNG/JPG renamed?


I would like everything renamed, xcf, png and jpg

Quote:
What if the XCF exists and the PNG/JPG does not, would still you want the PNG/JPG renamed?


the other script (save+export) should be used in case the xcf does already exist, and to update/overwrite
Instead Save as+export should avoid to overwrite, so rename everything , this also if the jpg and png do not exist ,( because the identical name is good for search or organize the archives )
(

Author:  Odinbc [ Tue Dec 11, 2012 6:11 pm ]
Post subject:  Re: Modify "Save as+export script to prevent overwriting

If you worry about an overwrite with "Save-As XCF and Export", uncheck "Export a png" and "Export a jpg" (use as default). This way you cannot overwrite or duplicate unless you choose.

Author:  PhotoComix [ Tue Dec 11, 2012 6:28 pm ]
Post subject:  Re: Modify "Save as+export script to prevent overwriting

i don't want overwrite not even the xcf i dont want overwrite nothing , i need to rename

i don't mind much the risk of a few superfluos copy instead i fear much delete work by accident
(and alas it happened with this script )

Author:  saulgoode [ Wed Dec 12, 2012 4:28 am ]
Post subject:  Re: Modify "Save as+export script to prevent overwriting

I will probably not have time to work on this until after the holidays and I would defer to RobA anyway as it is his script (I don't particularly enjoy working on other people's scripts). If nobody has provided a solution beforehand, contact me at the beginning of next year and I will address this.

Author:  PhotoComix [ Wed Dec 12, 2012 10:17 am ]
Post subject:  Re: Modify "Save as+export script to prevent overwriting

ok thanks !

Author:  RobA [ Fri Dec 14, 2012 2:54 pm ]
Post subject:  Re: Modify "Save as+export script to prevent overwriting

(grumble - having server errors trying to reply..)

Have you considered using my backup working script instead? http://registry.gimp.org/node/14246

This script save a backup copy of your active image as [imagename-ext]-YYYY-MM-DD-HH-MM.xcfgz in the same directory as your active image everytime you call it, and leaves your existing file's name unchanged...

Or do you need the jpg/png export as well? It might be easier to just add that to the backup working script...

-Rob A>

Author:  PhotoComix [ Fri Dec 14, 2012 4:48 pm ]
Post subject:  Re: Modify "Save as+export script to prevent overwriting

i need also the png\jpg export

Author:  PhotoComix [ Fri Dec 14, 2012 6:49 pm ]
Post subject:  Re: Modify "Save as+export script to prevent overwriting

Maybe a weird idea :
my problem is that the last name used to save is preserved and reused by default, would be no possible instead assign by default a different, random name ?

In any case the name proposed should be replaced , but a random name will avoid the risk of overwriting (at worst image will be saved7exported with a not hintful name )

Author:  RobA [ Tue Dec 18, 2012 3:40 pm ]
Post subject:  Re: Modify "Save as+export script to prevent overwriting

Try this quick modification of the backup script. The only thing it does not do is delete the png and jpg files (if maxbackups is set to something other than 0):

; backup_working.scm
; by Rob Antonishen
; http://silent9.com

; Version 1.3 (20120301)

; Changes:
; 1.1 changes to use xcfgz compressed files
; 1.2 added option to limit the number of files backed up. 
; This has changed the naming convention to include the original extension
; 1.3 added option to also export a png and/or jpeg file

; Description
; Saves a backup copy as [imagename-ext]-YYYY-MM-DD-HH-MM.XCFGZ
; It will appear in the File menu.  and can easily accessed with Alt-F, B
; License:
;
; 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.
;
; The GNU Public License is available at
; http://www.gnu.org/copyleft/gpl.html

(define (backup-working img inLayer exportpng? exportjpg?)
  (let*
    (
      (timestamp (unbreakupstr
                   (append (list (number->string (+ 1900 (car (time))))) ; year
                           (map (lambda (x) (string-append (make-string (- 2 (string-length (number->string x))) #\0) (number->string x))) ; pad to two characters
                             (append (list  (+ 1  (cadr (time)))) (butlast (cddr (time)))))) "-")) ; month is 0 referenced when (time) returns it
      (filename "")
      (extension "")
      (dirname "")
      (dupimage (car (gimp-image-duplicate img)))
      (maxbackups 0)  ; Change this to define the number of backup files to keep!!!
      (filelist "")
   )
   
    ;;helper defines
    (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. <= for an ascending numeric list, or
    ;string<=? for a case sensitive alphabetical sort,
    ;string-ci<=? for a case insensitive alphabetical sort,
    (define merge-sort
      (lambda (pred ls)
        (cond
          ((null? ls) ls)
          ((null? (cdr ls)) ls)
          (else (let ((splits (split ls)))
                  (merge pred
                    (merge-sort pred (car splits))
                    (merge-sort pred (cdr splits))))))))
                   
    (define get-n-items
      (lambda (lst num)
        (if (> num 0)
          (cons (car lst) (get-n-items (cdr lst) (- num 1)))
          '()))) ;'

   (define slice
     (lambda (lst start count)
        (if (> start 1)
          (slice (cdr lst) (- start 1) count)
          (get-n-items lst count))))
   
    ; It Starts Here....
    ;---------------------
    (if (= (string-length (car (gimp-image-get-filename img))) 0)
      (gimp-message "The file must be saved before a backup can be made.")
      (begin
        (set! filename (unbreakupstr (butlast (strbreakup (car (gimp-image-get-name img)) ".")) "."))
        (set! extension (car (last (strbreakup (car (gimp-image-get-name img)) "."))))
        (set! dirname (unbreakupstr (butlast (strbreakup (car (gimp-image-get-filename img)) DIR-SEPARATOR)) DIR-SEPARATOR))

        ;update progressbar text
        (gimp-progress-set-text (string-append "Backing up as: " filename "-" extension "-" timestamp ".xcfgz"))
       
        ;save file
        (gimp-xcf-save 0 dupimage (car (gimp-image-get-active-drawable dupimage))
          (string-append dirname DIR-SEPARATOR filename "-" extension "-" timestamp ".xcfgz")
          (string-append filename "-" extension "-" timestamp ".xcfgz"))
      
      ;flatten duplicate
      (gimp-image-merge-visible-layers dupimage CLIP-TO-IMAGE)

       (when (equal? exportpng? TRUE)
        ;save the png file
        (gimp-progress-set-text (string-append "Saving: " filename "-" extension "-" timestamp ".png"))
        (file-png-save-defaults RUN-NONINTERACTIVE dupimage (car (gimp-image-get-active-drawable dupimage))
         (string-append dirname DIR-SEPARATOR filename "-" extension "-" timestamp ".png")
         (string-append filename "-" extension "-" timestamp ".png")))

      (when (equal? exportjpg? TRUE)
        ;save the jpeg file
        (gimp-progress-set-text (string-append "Saving: " filename "-" extension "-" timestamp ".jpg"))
        (file-png-save-defaults RUN-NONINTERACTIVE dupimage (car (gimp-image-get-active-drawable dupimage))
         (string-append dirname DIR-SEPARATOR filename "-" extension "-" timestamp ".jpg")
         (string-append filename "-" extension "-" timestamp ".jpg")))
      )
    )
           
    ;clean up
    (gimp-image-delete dupimage)
   
    ;delete extra backups
    (when (> maxbackups 0)
      (set! filelist (merge-sort string<=? (cadr (file-glob (string-append dirname DIR-SEPARATOR filename "-" extension "-*.xcfgz") 0))))
      (set! filelist (slice filelist 0 (max (- (length filelist) maxbackups) 0)))
      (map (lambda (x) (file-delete x)) filelist)
    )   
  )
)

(script-fu-register "backup-working"
                  "<Image>/File/Save a _Backup"
               "Saves a backup copy as [imagename-ext]-YYYY-MM-DD-HH-MM.XCFGZ"
               "Rob Antonishen"
               "Rob Antonishen"
               "March 2012"
               "*"
               SF-IMAGE      "image"      0
               SF-DRAWABLE   "drawable"   0
                SF-TOGGLE     "Also export a png" FALSE
                  SF-TOGGLE     "Also export a jpg" FALSE
)            

Author:  PhotoComix [ Thu Dec 20, 2012 1:33 pm ]
Post subject:  Re: Modify "Save as+export script to prevent overwriting

OOOPPSS ... i didn't see your post

installing now

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/