Another Batch Script not working since upgrade

Post Reply
gcorbin
Posts: 110
Joined: 21 Aug 06 11:31
Location: Brisbane

Another Batch Script not working since upgrade

Post by gcorbin »

I have a batch script which renames images based on their headline. This script has worked fine until I upgraded to 3.3.

The batch script still works fine unless the headline contains the ' (single quote) character when it gives a syntax error at 37,113. This is due to the string '%ImageName' but I don't know how to fix it as %ImageName isn't a 'real' variable. Anyone know how to fix this?

Code: Select all

const
  StartNumber = 1;  // First Image Number
  NumberLength = 3; // Number of digits in number string
var
  FileNameNoNumber: String; // The filename based on headline using no unique number extension
  
    function IsNonImage (AFilename: String): Boolean; 
    var
      AExt: String;
    begin
      result := False;

      AExt := UpperCase (ExtractFileExt (AFileName));

      case AExt of
        '.THM', '.XMP':
          result := True;
      end;

    end;

   function IncrementFileNumber: Integer;
   var
     FileNumber: Integer;
     FileName: String;

   begin
     if IsNonImage (ImageItem.Filename) then begin
       // Use the number of the last image
       result:= ReadFromRegistry ('Scripts\IncFileNumber', 'LastNum', StartNumber);
     end else begin
        FileNumber:=StartNumber-1;

        // Find the next nonexisting filename
        repeat
           FileNumber:=FileNumber+1;
           FileName:= IncludeTrailingBackslash(ImageItem.FilenamePath) + AnsiReplaceStr('%ImageName','/','_') + ' (' + AddLeadingChars (IntToStr(FileNumber), '0', NumberLength, False) + ')' + ImageItem.FilenameExtension;
        until FileExists(FileName)=False;

        WriteToRegistry ('Scripts\IncFileNumber', 'LastNum' , FileNumber);
        result:=FileNumber;
     end;
   end;


   // Is there a headline?
   if length('%ImageName')=0 then begin
      // No. Keep filename
      result:=ImageItem.Filename;
   end else begin
         result:= AnsiReplaceStr('%ImageName','/','_') + ' (' + AddLeadingChars (IntToStr(IncrementFileNumber), '0', NumberLength, False) + ')' + ImageItem.FilenameExtension;
   end;
Hert
Posts: 7870
Joined: 13 Sep 03 6:24

Re: Another Batch Script not working since upgrade

Post by Hert »

I think this is a rename script and not a batch script (btw; PSU doesn't know the term batch script").

Am I correct that this is a rename script? Either way, the update will not have impacted your script. It just isn't written to be able to handle single quotes in a file name and you may have just gotten a file with a single quote in its name

I did some basic cleaning up, hoping this helps to get you started if more changes are needed

Code: Select all

const
  StartNumber = 1;  // First Image Number
  NumberLength = 3; // Number of digits in number string
var
  FileNameNoNumber: WideString; // The filename based on headline using no unique number extension
  HeadlineStr: WideString;

    function IsNonImage (AFilename: String): Boolean;
    var
      AExt: WideString;
    begin
      result := False;

      AExt := WideUpperCase (WideExtractFileExt (AFileName));

      case AExt of
        '.THM', '.XMP':
          result := True;
      end;

    end;

   function IncrementFileNumber: Integer;
   var
     FileNumber: Integer;
     FileName: WideString;

   begin
     if IsNonImage (ImageItem.Filename) then begin
       // Use the number of the last image
       result:= ReadFromRegistry ('Scripts\IncFileNumber', 'LastNum', StartNumber);
     end else begin
        FileNumber:=StartNumber-1;

        // Find the next nonexisting filename
        repeat
           FileNumber:=FileNumber+1;
           FileName:= WideIncludeTrailingBackslash(ImageItem.FilenamePath) + StrTran(HeadlineStr,'/','_') + ' (' + AddLeadingChars (IntToStr(FileNumber), '0', NumberLength, False) + ')' + ImageItem.FilenameExtension;
        until not WideFileExists(FileName);

        WriteToRegistry ('Scripts\IncFileNumber', 'LastNum' , FileNumber);
        result:=FileNumber;
     end;
   end;


   //HeadlineStr := '%ImageName';
   HeadlineStr := ImageItem.ParseMacros('%xmp:photoshop:headline', nil);

   // Is there a headline?
   if length(HeadlineStr)=0 then begin
      // No. Keep filename
      result:=ImageItem.Filename;
   end
   else begin
     result:= StrTran(HeadlineStr,'/','_') + ' (' + AddLeadingChars (IntToStr(IncrementFileNumber), '0', NumberLength, False) + ')' + ImageItem.FilenameExtension;
   end;
This is a user-to-user forum. If you have suggestions, requests or need support then please send a message
gcorbin
Posts: 110
Joined: 21 Aug 06 11:31
Location: Brisbane

Re: Another Batch Script not working since upgrade

Post by gcorbin »

You are correct that this is indeed a rename script. Sorry for getting this wrong.

I have been using this rename script since I migrated from IdImager to Photo Supreme and it always renamed the about 20% of my photos that include single quotes. I can only guess that %ImageName automatically changed single quotes to two single quotes but this has stopped in the recent upgrade.

However, moving forward, thank you for the changes to the script. Unfortunately, the upgraded script can rename photos without a quote in the headline but fails when renaming with a quote in the headline with the error
Syntax error. Source position_ 48,65
It appears that the '%xmp:photoshop:headline' in the updated script suffers from the same issue as the '%ImageName' in the original script.

Any other thoughts on how to overcome this?
Hert
Posts: 7870
Joined: 13 Sep 03 6:24

Re: Another Batch Script not working since upgrade

Post by Hert »

Ah yes that would be the same. Search the script for ImageItem.ParseMacros to check how to get the value in a variable
This is a user-to-user forum. If you have suggestions, requests or need support then please send a message
gcorbin
Posts: 110
Joined: 21 Aug 06 11:31
Location: Brisbane

Re: Another Batch Script not working since upgrade

Post by gcorbin »

Ok. I have worked it out between the hints Hert has provided and the Script API Document. The rename script now handles single quotes in the Headline. The key is to use ImageItem.XMPValue('photoshop:headline') rather than the original '%ImageName' or ImageItem.ParseMacros('%xmp:photoshop:headline', nil).

Thanks for your assistance Hert.

For anyone interrested, this script renames images based on the Headline appending a unique number to the end of the filename to ensure the filename is unique. eg. Headline (001). The final script is:-

Code: Select all

const
  StartNumber = 1;  // First Image Number
  NumberLength = 3; // Number of digits in number string
var
  FileNameNoNumber: WideString; // The filename based on headline using no unique number extension
  HeadlineStr: WideString;

    function IsNonImage (AFilename: String): Boolean;
    var
      AExt: WideString;
    begin
      result := False;

      AExt := WideUpperCase (WideExtractFileExt (AFileName));

      case AExt of
        '.THM', '.XMP':
          result := True;
      end;

    end;

   function IncrementFileNumber: Integer;
   var
     FileNumber: Integer;
     FileName: WideString;

   begin
     if IsNonImage (ImageItem.Filename) then begin
       // Use the number of the last image
       result:= ReadFromRegistry ('Scripts\IncFileNumber', 'LastNum', StartNumber);
     end else begin
        FileNumber:=StartNumber-1;

        // Find the next nonexisting filename
        repeat
           FileNumber:=FileNumber+1;
           FileName:= WideIncludeTrailingBackslash(ImageItem.FilenamePath) + StrTran(HeadlineStr,'/','_') + ' (' + AddLeadingChars (IntToStr(FileNumber), '0', NumberLength, False) + ')' + ImageItem.FilenameExtension;
        until not WideFileExists(FileName);

        WriteToRegistry ('Scripts\IncFileNumber', 'LastNum' , FileNumber);
        result:=FileNumber;
     end;
   end;


   HeadlineStr := ImageItem.XMPValue('photoshop:headline');

   // Is there a headline?
   if length(HeadlineStr)=0 then begin
      // No. Keep filename
      result:=ImageItem.Filename;
   end
   else begin
     result:= StrTran(HeadlineStr,'/','_') + ' (' + AddLeadingChars (IntToStr(IncrementFileNumber), '0', NumberLength, False) + ')' + ImageItem.FilenameExtension;
   end;
I must congratulate Hert on the wonderful functionality provided by the scripting in Photo Supreme. It allows the customisation and automation of almost anything. Unfortunately, it is made virtually unusable to mere mortals by the almost total lack of documentation. This is a huge loss. It was a problem in IdImager and it continues in Photo Supreme. I understand how much work it would be to fully document the APIs plus the environments like the rename environment, but without this documentation, it means the scripting functionality is out of reach of anyone except Hert plus a couple of experts.

Is there anyway to get the scripting API fully documented? Is there enough interest to fund this? Thoughts anyone?
PhilBurton
Posts: 307
Joined: 12 Sep 10 17:47
Location: CA, USA

Re: Another Batch Script not working since upgrade

Post by PhilBurton »

gcorbin wrote:I must congratulate Hert on the wonderful functionality provided by the scripting in Photo Supreme. It allows the customisation and automation of almost anything. Unfortunately, it is made virtually unusable to mere mortals by the almost total lack of documentation. This is a huge loss. It was a problem in IdImager and it continues in Photo Supreme. I understand how much work it would be to fully document the APIs plus the environments like the rename environment, but without this documentation, it means the scripting functionality is out of reach of anyone except Hert plus a couple of experts.

Is there anyway to get the scripting API fully documented? Is there enough interest to fund this? Thoughts anyone?
I agree with these comments, and these apply to other tools. Lightroom comes to mind. (Lua anyone?)

Are there "graphical" or "drag and drop" tools that might aid in construction of scripts?

What if someone organized a "wishlist for scripts" that everyone could vote on. then hert or another expert could build those scripts first. With better documentation of the existing scripts, it might be easier to modify them.

Phil
Photo Supreme user
Home built i7 3930, 32 GB RAM, Win 10 Pro 64, latest version of Photo Supreme 3, Lightroom 6 and Photoshop CS 6 (perpetual licenses)
gcorbin
Posts: 110
Joined: 21 Aug 06 11:31
Location: Brisbane

Re: Another Batch Script not working since upgrade

Post by gcorbin »

Phil, you are absolutely correct that lack of documentation is not limited to Photo Supreme. I am finding more and more advanced functionality is unusable in phones, cameras, TVs or software due to lack of documentation. In defence of Hert and the manufacturers, it is very difficult and time consuming to write good documentation and only a few like myself will actually read documentation. But, without documentation, advanced functionality like scripting is almost unused. What a waste.

I don't see the solution as Hert writing all the scripts (which is almost what seems to be happening now). I think the answer is to document the APIs. The question is how to fund this documentation because it is a significant slab of work. If I am the only one wanting this documentation, it will never be created.

There was a book (DAM Book) from memory written about how to use IdImager which was not written by Hert but with his support. I wonder if this was financially successful and the same could occur for the API documentation. I would buy a copy now.
PhilBurton
Posts: 307
Joined: 12 Sep 10 17:47
Location: CA, USA

Re: Another Batch Script not working since upgrade

Post by PhilBurton »

gcorbin wrote:Phil, you are absolutely correct that lack of documentation is not limited to Photo Supreme. I am finding more and more advanced functionality is unusable in phones, cameras, TVs or software due to lack of documentation. In defence of Hert and the manufacturers, it is very difficult and time consuming to write good documentation and only a few like myself will actually read documentation. But, without documentation, advanced functionality like scripting is almost unused. What a waste.
Early in my career as a software product manager, I learned that functionality that was unuseable and was not documented, may as well not exist.
I don't see the solution as Hert writing all the scripts (which is almost what seems to be happening now). I think the answer is to document the APIs. The question is how to fund this documentation because it is a significant slab of work. If I am the only one wanting this documentation, it will never be created.

There was a book (DAM Book) from memory written about how to use IdImager which was not written by Hert but with his support. I wonder if this was financially successful and the same could occur for the API documentation. I would buy a copy now.
The DAM Book was written by Peter Kreogh, about digital asset management systems in general, from the point of view of a photograher who needs to learn about this area. It was a great book. I don't know the title of the book about IdImager, but the author was Mike Buckley, who is a frequent contributor to this forum.

Phil
Photo Supreme user
Home built i7 3930, 32 GB RAM, Win 10 Pro 64, latest version of Photo Supreme 3, Lightroom 6 and Photoshop CS 6 (perpetual licenses)
Post Reply