run script from batcher?

Post Reply
fbungarz
Posts: 1576
Joined: 08 Dec 06 5:03
Location: Arizona, USA

run script from batcher?

Post by fbungarz » 12 Aug 16 14:41

Hi all,
any advice how I can execute frequently used scripts on a set of images directly from the batcher instead of having to go through "Tools - Scripter - File - Open"... ?
Thanks,
Frank

fbungarz
Posts: 1576
Joined: 08 Dec 06 5:03
Location: Arizona, USA

Re: run script from batcher?

Post by fbungarz » 14 Aug 16 8:19

Hi,
since I am not getting any answers, I'll try to illustrate what I want to do with two screenshots:
BatchScript_1.jpg
BatchScript_1.jpg (170.42 KiB) Viewed 3117 times
BatchScript_2.jpg
BatchScript_2.jpg (174.58 KiB) Viewed 3116 times
The script is a script written by Vlad, that assigns custom placeholders according to the file extension. If I execute it via the scripter, it runs just fine.
Scripter1.jpg
Scripter1.jpg (162.31 KiB) Viewed 3114 times
Scripter2.jpg
Scripter2.jpg (165.75 KiB) Viewed 3114 times
Running it from the batcher, however, does not work. The warning message pops up, but nothing happens.

:?

Thanks anyone for helping out.

Cheers,
Frank

PS: below the script...

Code: Select all

(*
  Script:  Assign Placeholders By Extension
  Author:  Vlad
  Version: 1.0
  Description:
        This is a custom script written for Frank Bungarz as a workaround for bugs 2864 and 2870.
        If needed, the script will replace the current placeholder with the 'Original' custom placeholder
        for all NEF files in the current selection. Also, if needed, it will replace the current placeholder
        with the 'Digital Negative' custom placeholder for all DNG files in the current selection.

        Prerequisites:
        - The 'Original' and the 'Digital Negative' custom placeholders must already be defined
        - Every (File.JPG, File.NEF, File.DNG) triplet must already form a version set, with the
          JPG file being the main version
*)


function ProcessVersionPlaceholder (AImage:  TImageItem;
                                    DesiredPlaceholder : TCatalogPlaceHolder)  : Boolean;
var
    APhs : TCatalogPlaceholders;
    APlaceHolder, DesiredPlaceHolderType : TCatalogPlaceHolderType;
    AMain, ACatItem : TCatalogItem;
begin
    result := false;
    if (not AImage._IsVersion) then
        Say2('Found non-versioned file: ', AImage.ExifFileName);
    else
    begin
        // retrieve the main version
        AMain := TCatalogItem.Create(nil);
        if not Catalog.FindImageCombined (AImage, AMain, True, vptNone) then
        begin
            Say2('Main version not found for image: ', AImage.ExifFileName);
            exit;
        end;

        ACatItem := TCatalogItem.Create(nil);
        Catalog.FindVersionImage(AImage, ACatItem);

        APhs := TCatalogPlaceHolders.Create(TCatalogPlaceHolder, '');
        if (not Catalog.EnumPlaceHoldersForVersionInMain(AMain, ACatItem, APhs)) then
        begin
            // there are no placeholders right now - add the desired placeholder

            // Say('Adding placeholder: ' + DesiredPlaceHolder.PlaceHolder);
            if (not Catalog.AssignPlaceHolderToVersionInMain (AMain, ACatItem, DesiredPlaceHolder)) then
            begin
                Say2('Could not add placeholder ' + DesiredPlaceHolder.PlaceHolder +
                     ' for image: ', AImage.ExifFileName);
                exit;
            end;
            result := true;
        end;
        else
        begin
            APlaceHolder := APhs.Items[0];
            if ((APhs.Count > 1) or
                (APlaceHolder.PlaceHolderType <> DesiredPlaceHolder.PlaceholderType)) then
            begin
                if (not Catalog.RemovePlaceHoldersOfVersion(ACatItem)) then
                begin
                    Say2('Could not remove placeholders for image: ', AImage.ExifFileName);
                    exit;
                end;

                //Say('Adding placeholder: ' + DesiredPlaceHolder.PlaceHolder);
                if (not Catalog.AssignPlaceHolderToVersionInMain (AMain, ACatItem, DesiredPlaceHolder)) then
                begin
                    Say2('Could not add placeholder ' + DesiredPlaceHolder.PlaceHolder +
                         ' for image: ', AImage.ExifFileName);
                    exit;
                end;
                result := true;
            end;
        end;

        APhs.Free;
        ACatItem.Free;
        AMain.Free;
    end;
end;


const
    JpgFileExtension   = '.jpg';
    NefFileExtension   = '.nef';
    DngFileExtension   = '.dng';

    NefPlaceHolderName = 'Original';
    DngPlaceHolderName = 'Digital Negative';
var
    ASelectedImage:   TImageItem;
    ACatItem: TCatalogItem;
    NefPlaceHolder, DngPlaceHolder : TCatalogPlaceHolder;
    i, fixedNefFiles, fixedDngFiles: Integer;
begin
    if Selected.Count = 0 then
    begin
        Say ('No selection made.');
        exit;
    end;

    NefPlaceHolder := TCatalogPlaceholder.Create(nil);
    if (not Catalog.EnumPlaceHolderByName(NefPlaceHolderName, NefPlaceHolder, false)) then
    begin
        Say('No version placeholder named ' + NefPlaceHolderName + ' is yet defined.');
        exit;
    end;

    DngPlaceHolder := TCatalogPlaceholder.Create(nil);
    if (not Catalog.EnumPlaceHolderByName(DngPlaceHolderName, DngPlaceHolder, false)) then
    begin
        Say('No version placeholder named ' + DngPlaceHolderName + ' is yet defined.');
        exit;
    end;


    if not Ask ('Are you sure you want to fix the version placeholders for all NEF and DNG images in the current selection?') then
        exit;

    fixedNefFiles := 0;
    fixedDngFiles := 0;

    Progress.Cancel := False;
    Progress.ProgressBar := True;
    Progress.Max := Selected.Count;
    Progress.Show;

    for count := 0 to Selected.Count - 1 do
    begin
        ASelectedImage := Selected.Items[count];
        ASelectedFileExt := ExtractFileExt (ASelectedImage.FileName);
        if (CompareText(ASelectedFileExt, JpgFileExtension) = 0) then
        begin
            if (not ASelectedImage._isMainVersion) then
                Say2('Found JPG file which is not main version: ', ASelectedImage.ExifFileName);
        end;
        else if (CompareText(ASelectedFileExt, NefFileExtension) = 0) then
        begin
            if (ProcessVersionPlaceholder(ASelectedImage, NefPlaceHolder)) then
                fixedNefFiles := fixedNefFiles+1;
        end;
        else if (CompareText(ASelectedFileExt, DngFileExtension) = 0) then
        begin
            if (ProcessVersionPlaceholder(ASelectedImage, DngPlaceHolder)) then
                fixedDngFiles := fixedDngFiles+1;
        end;
        // we don't care about other file extensions
    end;

    Say('Fixed the ' + NefPlaceHolderName + ' placeholder for ' + inttostr(fixedNefFiles) + ' Nef files.' + crlf +
        'Fixed the ' + DngPlaceHolderName + ' placeholder for ' + inttostr(fixedDngFiles) + ' Dng files.');

    NefPlaceHolder.Free;
    DngPlaceHolder.Free;
end;

Hert
Posts: 6338
Joined: 13 Sep 03 7:24

Re: run script from batcher?

Post by Hert » 14 Aug 16 10:40

When a script is ran in the Batcher then it runs inside a background application thread and not in the main application thread. In a background thread it is not allowed to interact directly with the user interface. Vlad's script contains several Say and Ask statements, which will be skipped in a background thread. I think you're getting one of these messages but as they are suppressed you don't know what's going on.

Try this code instead:

Code: Select all

(*
  Script:  Assign Placeholders By Extension
  Author:  Vlad
  Version: 1.0
  Description:
        This is a custom script written for Frank Bungarz as a workaround for bugs 2864 and 2870.
        If needed, the script will replace the current placeholder with the 'Original' custom placeholder
        for all NEF files in the current selection. Also, if needed, it will replace the current placeholder
        with the 'Digital Negative' custom placeholder for all DNG files in the current selection.

        Prerequisites:
        - The 'Original' and the 'Digital Negative' custom placeholders must already be defined
        - Every (File.JPG, File.NEF, File.DNG) triplet must already form a version set, with the
          JPG file being the main version
*)


function ProcessVersionPlaceholder (AImage:  TImageItem;
                                    DesiredPlaceholder : TCatalogPlaceHolder)  : Boolean;
var
    APhs : TCatalogPlaceholders;
    APlaceHolder, DesiredPlaceHolderType : TCatalogPlaceHolderType;
    AMain, ACatItem : TCatalogItem;
begin
    result := false;
    if (not AImage._IsVersion) then
        PublicBroadcastText(nil, 'SafeSay', 'Found non-versioned file: ' + CrLf2 + AImage.ExifFileName);
    else
    begin
        // retrieve the main version
        AMain := TCatalogItem.Create(nil);
        if not Catalog.FindImageCombined (AImage, AMain, True, vptNone) then
        begin
          PublicBroadcastText(nil, 'SafeSay', 'Main version not found for image: ' + CrLf2 + AImage.ExifFileName);
          exit;
        end;

        ACatItem := TCatalogItem.Create(nil);
        Catalog.FindVersionImage(AImage, ACatItem);

        APhs := TCatalogPlaceHolders.Create(TCatalogPlaceHolder, '');
        if (not Catalog.EnumPlaceHoldersForVersionInMain(AMain, ACatItem, APhs)) then
        begin
            // there are no placeholders right now - add the desired placeholder

            // Say('Adding placeholder: ' + DesiredPlaceHolder.PlaceHolder);
            if (not Catalog.AssignPlaceHolderToVersionInMain (AMain, ACatItem, DesiredPlaceHolder)) then
            begin
                PublicBroadcastText(nil, 'SafeSay', 'Could not add placeholder ' + DesiredPlaceHolder.PlaceHolder + ' for image: ' + CrLf2 + AImage.ExifFileName);
                exit;
            end;
            result := true;
        end;
        else
        begin
            APlaceHolder := APhs.Items[0];
            if ((APhs.Count > 1) or
                (APlaceHolder.PlaceHolderType <> DesiredPlaceHolder.PlaceholderType)) then
            begin
                if (not Catalog.RemovePlaceHoldersOfVersion(ACatItem)) then
                begin
                    PublicBroadcastText(nil, 'SafeSay', 'Could not remove placeholders for image: ' + CrLf2 + AImage.ExifFileName);
                    exit;
                end;

                //Say('Adding placeholder: ' + DesiredPlaceHolder.PlaceHolder);
                if (not Catalog.AssignPlaceHolderToVersionInMain (AMain, ACatItem, DesiredPlaceHolder)) then
                begin
                    PublicBroadcastText(nil, 'SafeSay', 'Could not add placeholder ' + DesiredPlaceHolder.PlaceHolder + ' for image: ' + CrLf2 + AImage.ExifFileName);
                    exit;
                end;
                result := true;
            end;
        end;

        APhs.Free;
        ACatItem.Free;
        AMain.Free;
    end;
end;


const
    JpgFileExtension   = '.jpg';
    NefFileExtension   = '.nef';
    DngFileExtension   = '.dng';

    NefPlaceHolderName = 'Original';
    DngPlaceHolderName = 'Digital Negative';
var
    ASelectedImage:   TImageItem;
    ACatItem: TCatalogItem;
    NefPlaceHolder, DngPlaceHolder : TCatalogPlaceHolder;
    i, fixedNefFiles, fixedDngFiles: Integer;
begin
    if Selected.Count = 0 then
    begin
        PublicBroadcastText(nil, 'SafeSay', 'No selection made.');
        exit;
    end;

    NefPlaceHolder := TCatalogPlaceholder.Create(nil);
    if (not Catalog.EnumPlaceHolderByName(NefPlaceHolderName, NefPlaceHolder, false)) then
    begin
        PublicBroadcastText(nil, 'SafeSay', 'No version placeholder named ' + NefPlaceHolderName + ' is yet defined.')
        exit;
    end;

    DngPlaceHolder := TCatalogPlaceholder.Create(nil);
    if (not Catalog.EnumPlaceHolderByName(DngPlaceHolderName, DngPlaceHolder, false)) then
    begin
        PublicBroadcastText(nil, 'SafeSay', 'No version placeholder named ' + DngPlaceHolderName + ' is yet defined.');
        exit;
    end;

    //if not Ask ('Are you sure you want to fix the version placeholders for all NEF and DNG images in the current selection?') then
    //    exit;

    fixedNefFiles := 0;
    fixedDngFiles := 0;

    Progress.Cancel := False;
    Progress.ProgressBar := True;
    Progress.Max := Selected.Count;
    Progress.Show;

    for count := 0 to Selected.Count - 1 do
    begin
        ASelectedImage := Selected.Items[count];
        ASelectedFileExt := ExtractFileExt (ASelectedImage.FileName);
        if (CompareText(ASelectedFileExt, JpgFileExtension) = 0) then
        begin
            if (not ASelectedImage._isMainVersion) then
                Say2('Found JPG file which is not main version: ', ASelectedImage.ExifFileName);
        end;
        else if (CompareText(ASelectedFileExt, NefFileExtension) = 0) then
        begin
            if (ProcessVersionPlaceholder(ASelectedImage, NefPlaceHolder)) then
                fixedNefFiles := fixedNefFiles+1;
        end;
        else if (CompareText(ASelectedFileExt, DngFileExtension) = 0) then
        begin
            if (ProcessVersionPlaceholder(ASelectedImage, DngPlaceHolder)) then
                fixedDngFiles := fixedDngFiles+1;
        end;
        // we don't care about other file extensions
    end;

    PublicBroadcastText(nil, 'SafeSay', 'Fixed the ' + NefPlaceHolderName + ' placeholder for ' + inttostr(fixedNefFiles) + ' Nef files.' + crlf +
        'Fixed the ' + DngPlaceHolderName + ' placeholder for ' + inttostr(fixedDngFiles) + ' Dng files.');

    NefPlaceHolder.Free;
    DngPlaceHolder.Free;
end;

Tip for script writers; it is possible to do a Say in a thread but then you'll have to send a notification to the main thread;

Code: Select all

PublicBroadcastText(nil, 'SafeSay', 'Hello world');
This is a User-to-User forum which means that users post questions here for other users.
Feature requests, change suggestions, or bugs can be logged in the ticketing system

fbungarz
Posts: 1576
Joined: 08 Dec 06 5:03
Location: Arizona, USA

Re: run script from batcher?

Post by fbungarz » 14 Aug 16 13:10

Hi Hert,
thanks for the quick reply.
Unfortunately it still does not work. If I try running the batch on two files I now get two error messages that tell me "found non versioned file" followed by the message that "placeholders were assigned to 0 file(s)".
Obviously the files are versioned, though, else I would not try using the script to assign a custom placeholder by extension...
Below the screenshots:
Non-versioned_file.jpg
Non-versioned_file.jpg (132.76 KiB) Viewed 3101 times
Non-versioned_file_2.jpg
Non-versioned_file_2.jpg (132.6 KiB) Viewed 3101 times
Thanks,
Frank

Hert
Posts: 6338
Joined: 13 Sep 03 7:24

Re: run script from batcher?

Post by Hert » 14 Aug 16 23:20

Code: Select all

(*
  Script:  Assign Placeholders By Extension
  Author:  Vlad
  Version: 1.0
  Description:
        This is a custom script written for Frank Bungarz as a workaround for bugs 2864 and 2870.
        If needed, the script will replace the current placeholder with the 'Original' custom placeholder
        for all NEF files in the current selection. Also, if needed, it will replace the current placeholder
        with the 'Digital Negative' custom placeholder for all DNG files in the current selection.

        Prerequisites:
        - The 'Original' and the 'Digital Negative' custom placeholders must already be defined
        - Every (File.JPG, File.NEF, File.DNG) triplet must already form a version set, with the
          JPG file being the main version
*)


function ProcessVersionPlaceholder (AImage:  TImageItem;
                                    DesiredPlaceholder : TCatalogPlaceHolder)  : Boolean;
var
    APhs : TCatalogPlaceholders;
    APlaceHolder, DesiredPlaceHolderType : TCatalogPlaceHolderType;
    AMain, ACatItem : TCatalogItem;
begin
    result := false;
    {if (not AImage._IsVersion) then
        PublicBroadcastText(nil, 'SafeSay', 'Found non-versioned file: ' + CrLf2 + AImage.ExifFileName);
    else}
    begin
        // retrieve the main version
        AMain := TCatalogItem.Create(nil);
        if not Catalog.FindImageCombined (AImage, AMain, True, vptNone) then
        begin
          PublicBroadcastText(nil, 'SafeSay', 'Main version not found for image: ' + CrLf2 + AImage.ExifFileName);
          exit;
        end;

        ACatItem := TCatalogItem.Create(nil);
        if not Catalog.FindVersionImage(AImage, ACatItem) then
        begin
          PublicBroadcastText(nil, 'SafeSay', 'Found non-versioned file: ' + CrLf2 + AImage.FileName);
          exit;
        end;

        APhs := TCatalogPlaceHolders.Create(TCatalogPlaceHolder, '');
        if (not Catalog.EnumPlaceHoldersForVersionInMain(AMain, ACatItem, APhs)) then
        begin
            // there are no placeholders right now - add the desired placeholder

            // Say('Adding placeholder: ' + DesiredPlaceHolder.PlaceHolder);
            if (not Catalog.AssignPlaceHolderToVersionInMain (AMain, ACatItem, DesiredPlaceHolder)) then
            begin
                PublicBroadcastText(nil, 'SafeSay', 'Could not add placeholder ' + DesiredPlaceHolder.PlaceHolder + ' for image: ' + CrLf2 + AImage.ExifFileName);
                exit;
            end;
            result := true;
        end;
        else
        begin
            APlaceHolder := APhs.Items[0];
            if ((APhs.Count > 1) or
                (APlaceHolder.PlaceHolderType <> DesiredPlaceHolder.PlaceholderType)) then
            begin
                if (not Catalog.RemovePlaceHoldersOfVersion(ACatItem)) then
                begin
                    PublicBroadcastText(nil, 'SafeSay', 'Could not remove placeholders for image: ' + CrLf2 + AImage.ExifFileName);
                    exit;
                end;

                //Say('Adding placeholder: ' + DesiredPlaceHolder.PlaceHolder);
                if (not Catalog.AssignPlaceHolderToVersionInMain (AMain, ACatItem, DesiredPlaceHolder)) then
                begin
                    PublicBroadcastText(nil, 'SafeSay', 'Could not add placeholder ' + DesiredPlaceHolder.PlaceHolder + ' for image: ' + CrLf2 + AImage.ExifFileName);
                    exit;
                end;
                result := true;
            end;
        end;

        APhs.Free;
        ACatItem.Free;
        AMain.Free;
    end;
end;


const
    JpgFileExtension   = '.jpg';
    NefFileExtension   = '.nef';
    DngFileExtension   = '.dng';

    NefPlaceHolderName = 'Original';
    DngPlaceHolderName = 'Digital Negative';
var
    ASelectedImage:   TImageItem;
    ACatItem: TCatalogItem;
    NefPlaceHolder, DngPlaceHolder : TCatalogPlaceHolder;
    i, fixedNefFiles, fixedDngFiles: Integer;
begin
    if Selected.Count = 0 then
    begin
        PublicBroadcastText(nil, 'SafeSay', 'No selection made.');
        exit;
    end;

    NefPlaceHolder := TCatalogPlaceholder.Create(nil);
    if (not Catalog.EnumPlaceHolderByName(NefPlaceHolderName, NefPlaceHolder, false)) then
    begin
        PublicBroadcastText(nil, 'SafeSay', 'No version placeholder named ' + NefPlaceHolderName + ' is yet defined.')
        exit;
    end;

    DngPlaceHolder := TCatalogPlaceholder.Create(nil);
    if (not Catalog.EnumPlaceHolderByName(DngPlaceHolderName, DngPlaceHolder, false)) then
    begin
        PublicBroadcastText(nil, 'SafeSay', 'No version placeholder named ' + DngPlaceHolderName + ' is yet defined.');
        exit;
    end;

    //if not Ask ('Are you sure you want to fix the version placeholders for all NEF and DNG images in the current selection?') then
    //    exit;

    fixedNefFiles := 0;
    fixedDngFiles := 0;

    Progress.Cancel := False;
    Progress.ProgressBar := True;
    Progress.Max := Selected.Count;
    Progress.Show;

    for count := 0 to Selected.Count - 1 do
    begin
        ASelectedImage := Selected.Items[count];
        ASelectedFileExt := ExtractFileExt (ASelectedImage.FileName);
        if (CompareText(ASelectedFileExt, JpgFileExtension) = 0) then
        begin
            if (not ASelectedImage._isMainVersion) then
                Say2('Found JPG file which is not main version: ', ASelectedImage.ExifFileName);
        end;
        else if (CompareText(ASelectedFileExt, NefFileExtension) = 0) then
        begin
            if (ProcessVersionPlaceholder(ASelectedImage, NefPlaceHolder)) then
                fixedNefFiles := fixedNefFiles+1;
        end;
        else if (CompareText(ASelectedFileExt, DngFileExtension) = 0) then
        begin
            if (ProcessVersionPlaceholder(ASelectedImage, DngPlaceHolder)) then
                fixedDngFiles := fixedDngFiles+1;
        end;
        // we don't care about other file extensions
    end;

    PublicBroadcastText(nil, 'SafeSay', 'Fixed the ' + NefPlaceHolderName + ' placeholder for ' + inttostr(fixedNefFiles) + ' Nef files.' + crlf +
        'Fixed the ' + DngPlaceHolderName + ' placeholder for ' + inttostr(fixedDngFiles) + ' Dng files.');

    NefPlaceHolder.Free;
    DngPlaceHolder.Free;
end;
This is a User-to-User forum which means that users post questions here for other users.
Feature requests, change suggestions, or bugs can be logged in the ticketing system

fbungarz
Posts: 1576
Joined: 08 Dec 06 5:03
Location: Arizona, USA

Re: run script from batcher?

Post by fbungarz » 22 Aug 16 11:18

Hi Hert,
great, this version works!
Sorry for getting back only a week later. I have been offline for a few days...
Cheers,
Frank

Post Reply