Go to footer

What's a better way to test that there's an open image ?

Discussions on script-fu scripts for GIMP, help with scripts, best practice examples and more.

Moderator: Wilbers


What's a better way to test that there's an open image ?

Postby stryker » Fri Jul 08, 2011 4:53 pm

Hi

I've found that a call to gimp-image-get-filename will crash the script-fu if there is no open image. To avoid this, I want to test that there is a value in the parameter img which is being passed.
Code: Select all
(if (not(= img TRUE))  ; assert a current image loaded in GIMP
     (begin
          ; do there's-no-image-loaded stuff
     )                     
     (begin
          ; else, img is valid, so gimp-image-get-filename won't crash the script   
          (set! fileOne (car (gimp-image-get-filename img)))   
          ; etc

I'm finding this approach isn't entirely reliable, with it sometimes giving a false-positive despite there being an image open, and in the active window of Gimp when the script is run.

Is there a better test that you can recommend achieve this? Or, a better way in general to safeguard that gimp-image-get filename doesn't crash you out?

Thanks
Geoff
User avatar
stryker
Akrotiri Fresco
Akrotiri Fresco
 
Posts: 44
Joined: Mon Jun 20, 2011 2:32 pm


Re: What's a better way to test that there's an open image ?

Postby paynekj » Fri Jul 08, 2011 5:13 pm

Would gimp-image-is-valid do what you want?
Kevin
paynekj
Egyptian Fresco
Egyptian Fresco
 
Posts: 14
Joined: Wed Jan 20, 2010 12:18 pm
Location: UK


Re: What's a better way to test that there's an open image ?

Postby stryker » Sat Jul 09, 2011 6:32 am

paynekj wrote:Would gimp-image-is-valid do what you want?
Thankyou! I'll certainly give that a go. From the procedure browser info it looks very promising. Cheers ! Geoff
User avatar
stryker
Akrotiri Fresco
Akrotiri Fresco
 
Posts: 44
Joined: Mon Jun 20, 2011 2:32 pm


Re: What's a better way to test that there's an open image ?

Postby saulgoode » Sun Jul 10, 2011 11:04 pm

stryker wrote:I've found that a call to gimp-image-get-filename will crash the script-fu if there is no open image. To avoid this, I want to test that there is a value in the parameter img which is being passed.

In the general case, it should not be necessary to test for a valid image because either your script creates the image or the image is passed to your script as an SF-IMAGE argument (in the 'script-fu-register' procedure).

Of particular note with regard to 'script-fu-register' is the seventh parameter, which is the menu constraint specification. This parameter is string specifying when your command will be accessible in the menus and represents a test for the colorspace of currently active drawable. If the colorspace of the active drawable does not match the value of the seventh argument, the menu command will be disabled. Possible values are currently "RGB", "RGBA", "GRAYSCALE", "GRAYSCALEA", "INDEXED", and "INDEXEDA". Multiple colorspaces can be supported by appending the above strings separated by commas, by using an asterisk as a wildcard character, or a combination of the two (e.g., "RGB*,GRAY*" would make the command available unless the images is in INDEXED mode).

If your script is able to work on any type of drawable, your constraint specification string should "*" (which is equivalent to "RGB,RGBA,GRAYSCALE,GRAYSCALEA, INDEXED,INDEXEDA").

Importantly, if your constraint parameter is the empty string ("") then your script does not expect an image at all (and there should not be a SF-IMAGE declaration in the PDB parameter list). This option should be employed when your script creates its own image(s) from scratch (see the scripts under "File->Create" for examples), or your script is doing something other than manipulating images (for example, generating a gradient or palette).

Note that if your script is registered with a constraint that is anything but the empty string, there is no way the user can invoke your script without there being a valid image available. Thus it is typically not necessary to test for the validity of the image.

Nonetheless, if you do wish to test for the validity of your image, paynekj's suggestion offers a safe way of doing so (but I'd advise that you double-check the necessity of doing such).
User avatar
saulgoode
Etruscan Mural
Etruscan Mural
 
Posts: 77
Joined: Mon Jan 18, 2010 11:03 pm


Re: What's a better way to test that there's an open image ?

Postby stryker » Mon Jul 11, 2011 9:17 am

Thankyou saulgoode

I certainly hadn't read that previously, and now param 7 makes a great deal more sense (and the mystery of how to make script-fu's that appear in menus only when images are open is also now solved.
saulgoode wrote:Importantly, if your constraint parameter is the empty string ("") then your script does not expect an image at all...
And yes that's exactly how mine was specified. I've changed it to "*" now and I can remove the test, as the crash won't be possible either.

Thanks ! Geoff
User avatar
stryker
Akrotiri Fresco
Akrotiri Fresco
 
Posts: 44
Joined: Mon Jun 20, 2011 2:32 pm


Return to Board index

Return to GIMP Script-Fu

Who is online

Users browsing this forum: No registered users and 2 guests