Batch Rename Settings

Discussion regarding all scripting related questions
Please DO NOT post to this thread anything that is not directly related to scripting of Capture One.

Re: Batch Rename Settings

Postby QBus » Sat Sep 29, 2018 10:27 pm

Hi SeanMurp

Thanks for taking the time to support the resolution of my renaming issue. In this case I can exclude a focus change with certainty.
Also, it doesn't make any difference whether I use
Code: Select all
set theVariants to variants whose selected is true
instead of
Code: Select all
set theVariants to selected variants


The problem must be related to the repeated and rapid invocation of
Code: Select all
batch rename variants
while renaming just a single variant each time.
My hypothesis is that Capture One updates (i.e. reorders) the image selection after each invocation (which is a very sensible thing to do!) and that — for whatever reason — this affects the list of variants used by
Code: Select all
repeat with variant_ in selectedVariants
even though I (try) to create a copy of the original result of
Code: Select all
selected variants
to avoid exactly this negative effect.
QBus
 
Posts: 4
Joined: Wed Oct 12, 2016 8:57 am

Re: Batch Rename Settings

Postby SeanMurp » Mon Oct 01, 2018 6:45 pm

Hi QBus,
Here is an idea that could get you out of repeating through individual files to name. Instead you could repeat through the selected files to set the image's original file number to a different IPTC field (similar to how you are setting the original file name to Status Source). Status Provider is the field directly above Status Source. Then do an actual batch rename on the selected variants but this time use the tokens to call Provider for the original file number. This also removes the adjustment of rename settings with each file (especially removing counter adjustment).

Below is a bit of re-working of your code that seems to work for me. Main changes:
1. numberSuffixFormat changed to Provider token
2. Only creating one list of selectedVariants
3. Repeat through selectedVariants to get the originalFileNameNumberSuffix and place that number in status provider metadata field (NOTE: I couldn't get your numberSuffix handler to compile. Kept giving me an 'Expected “,” but found “text”.' error).
4. Batch rename settings set once
5. Batch rename all selectedVariants at once.

Hope this works for you.

Code: Select all
tell application "Capture One 11"
   
   set prefix to "AK"
   set dateFormat to "[Image Year (yyyy)]-[Image Month (MM)]-[Image Day of Month (dd)]" -- use Capture One token names
   set numberSuffixFormat to "[Provider]" -- use Capture One token names
   set tokenFormat to prefix & "_" & dateFormat & "_" & numberSuffixFormat
   
   if (count of selected variants) = 0 then
      display dialog "No selection." buttons {"Abort"}
      return
   end if
   
   set numberOfFilesRenamed to 0
   set NEWLINE to ASCII character 10
   
   set selectedVariants to variants whose selected is true
   
   
   --
   -- Repeat to get file name number suffix and apply to metadata field
   --
   repeat with variant_ in selectedVariants
      set i to parent image of variant_
      set originalFileName to (name of i)
      
      try
         set originalFileNameNumberSuffix to my numberSuffix(originalFileName)
         
         -- store originalFileName in "IPTC Status Source" field if field is still empty:   
         -- add number suffix to "IPTC Provider" field as text
         tell variant_
            if (status source = "") then
               set status source to originalFileName
            end if
            set status provider to originalFileNameNumberSuffix
         end tell
         
         set numberOfFilesRenamed to numberOfFilesRenamed + 1
         
      on error
         --display dialog "Error on image name: " & (name of i) buttons {"OK", "Abort"} default button "OK" cancel button "Abort"
      end try
      
   end repeat
   
   tell current document
      tell batch rename settings
         set method to text and tokens
         set token format to tokenFormat
         --set counter to originalFileNameNumberSuffix
         set pair RAWs and JPGs to true
         set include file extension to false
      end tell
      
      batch rename variants selectedVariants
   end tell
   
   display dialog "" & (count of selectedVariants) & " of " & numberOfFilesRenamed & " images renamed" buttons {"OK"}
end tell

on numberSuffix(fileName)
   set matchedText to (find text "[0-9]+\\.[^\\.]+$" in fileName with regexp and string result)
   set matchedText to (find text "^[0-9]+" in matchedText with regexp and string result)
   return matchedText as number
end numberSuffix

SeanMurp
 
Posts: 75
Joined: Wed Nov 02, 2016 4:30 am

Re: Batch Rename Settings

Postby Eric Nepean » Tue Oct 02, 2018 8:54 pm

I think I see a possible pitfall here.

When Capture one returns a list of variants, or a single variant, the variant is identified by its sequence number in the browser.

The sequence number of a given variant depends on the sort order of the browser, which may be Name, Date, Rating, Color Tag and a bunch of other fields.

For example here are two results for the same 3 selected images:
Code: Select all
tell application "Capture One 11" to get selected variants
## images sorted by image name returns:
## {variant 5 of collection 7 of collection 1 of collection 4 of collection 8 of document "Main5M" of application "Capture One 11", variant 6 of collection 7 of collection 1 of collection 4 of collection 8 of document "Main5M" of application "Capture One 11", variant 7 of collection 7 of collection 1 of collection 4 of collection 8 of document "Main5M" of application "Capture One 11"}

Code: Select all
tell application "Capture One 11" to get selected variants
## images sorted by image file size returns:
## {variant 177 of collection 7 of collection 1 of collection 4 of collection 8 of document "Main5M" of application "Capture One 11", variant 221 of collection 7 of collection 1 of collection 4 of collection 8 of document "Main5M" of application "Capture One 11", variant 232 of collection 7 of collection 1 of collection 4 of collection 8 of document "Main5M" of application "Capture One 11"}


So how does this apply to image renaming?

If the browser is set to order variants by parent image name, each renaming operation can change the index of one or many variants. The variant references now point to a different variant.

The first operation will be predictable, but the variant parent image renamed by the second operation could be as expected if the first variant remains before the second variant in the sort order, or rather unexpected if the renaming operation places the first variant after the second variant.
Cheers, Eric
[late 2015 iMac, 4GHz i7, 24GB RAM, external SSDs. GX8, E-M1, GX7, GM5, GM1 ....]
Eric Nepean
 
Posts: 370
Joined: Sat Oct 25, 2014 8:02 am

Re: Batch Rename Settings

Postby SeanMurp » Tue Oct 02, 2018 9:33 pm

Yes Eric, that is a good thing to point out when scripting the renaming of files. If you are renaming a set of files and then need to act further on those selected files after the rename you will need to set your variable to selected variants again.
Code: Select all
set selectedVariants to variants whose selected is true


This shouldn't be an issue with QBus's problem since they are interacting with values pulled from each individual file, so order of images should be independent of this process and not have an impact on the rename. But, yes, if the script was to then take further action on these images they would need to set the selectedVariants variable again.
SeanMurp
 
Posts: 75
Joined: Wed Nov 02, 2016 4:30 am

Re: Batch Rename Settings

Postby QBus » Wed Oct 03, 2018 9:45 pm

Hi Sean, hi Eric

Sean, your solution is is what I call looking at a problem from a different angle! Thank you very much for this creative solution. It works really nicely.
The reason you got that syntax error for find text is that I am using a rather nice library for regex matching that I had forgot to mention in the header of my script (it's mentionend now): http://www.satimage.fr/software/en/down ... saxen.html

Eric, I found exactly the same explanation for how variants are represented in Applescript: not much more than a position index to the current collection of Capture One. The index is only valid as long as the sort order of the collection remains unchanged, after that it may point to a wrong variant.
It took me some time to come up with a combination of two solutions to alleviate the problem with the fragile position index of Applescript variants:

1. From the current selection of variants, I create a list with just the names of those variants (this list will not change even if the names in Capture One do). Then I iterate over the names list, obtain the proper variant for each name and rename that variant.
2. I call refresh after each batch renaming invocation.

Now, my script looks like this and works like a charm:
--
-- Renames a list of selected image variants honouring the original camera file-name number suffix
--
-- e.g. "DSC00044.jpg" to "OR_2018-02-15_00044.jpg"
--
-- something that Capture One seems unable to do.
--
-- In addition, if the variant IPTC data property "Status Source" property is undefined, it is set to the original
-- file name created by the camera, like so:
--
-- "DSC00044.jpg"
--
-- This can later (and after possible further renamings) be used to revert back to the original file name or to
-- reuse the number suffix.
--
-- Note: This script uses the Satimage osax library for regex pattern matching and won't work without it
-- (see http://www.satimage.fr/software/en/down ... saxen.html)
--

tell application "Capture One 11"

set prefix to "AK"
set dateFormat to "[Image Year (yyyy)]-[Image Month (MM)]-[Image Day of Month (dd)]" -- use Capture One token names
set numberSuffixFormat to "[5 Digit Counter]" -- use Capture One token names
set tokenFormat to prefix & "_" & dateFormat & "_" & numberSuffixFormat

set selectedVariants to variants whose selected is true -- assignment required to have correct type
if (count of selectedVariants) = 0 then
display dialog "No selection." buttons {"Abort"}
return
end if

--
-- Important: the Applescript representation of Capture One variants seems to be a rather poor concept
-- in that it is not much more than a numeric position index in a list rather than a proper, stable data
-- object for a given image (or variant, to be precise). This shortcoming becomes apparent when the
-- ordering of the list changes, e.g. if the list represents an image collection of Capture One and the
-- order of images in the collection is changeing due to ongoing renaming, then a given Applescript
-- variant can suddenly "point" to an object that wasn't even selected in the first place. As a consequence,
-- the wrong object may be processed.
-- Thus, in order to obtain a stable list of selected images, we create a list with just the *names* of the
-- variants in the current selection. Later, we iterate over those names later rather than over the "variants"
-- that are currently selected in Capture One.
--
set variantNames to {}
repeat with v in selectedVariants
set the end of variantNames to (name of v)
end repeat

--
-- Now "batch"-rename the variants one by one by (= not batch) setting the 'counter' token to the
-- correct value for the respective variant
--
set numberOfFilesRenamed to 0
repeat with variantName_ in variantNames

set singleVariantList to (variants whose name is variantName_)
if ((count of singleVariantList) ≠ 1) then
display dialog "Error, found " & (count of singleVariantList) & " variant(s) named '" & variantName_ & "'." buttons {"Abort"}
end if

set variant_ to (item 1 of singleVariantList)
set originalFileName to (name of (parent image of variant_))
--display dialog "Original image name: " & originalFileName & ", variant name: " & (name of variant_) buttons {"OK"}

try
set originalFileNameNumberSuffix to my numberSuffix(originalFileName)
--display dialog "Original image name: " & (name of variant_) & ", suffix: " & originalFileNameNumberSuffix buttons {"OK", "Abort"} default button "OK" cancel button "Abort"

-- store originalFileName in "IPTC Status Source" field if field is still empty:
tell variant_
if (status source = "") then
set status source to originalFileName
end if
end tell

tell current document
tell batch rename settings
set method to text and tokens
set token format to tokenFormat
set counter to originalFileNameNumberSuffix
set pair RAWs and JPGs to true
set include file extension to false
end tell

batch rename variants singleVariantList
end tell
set numberOfFilesRenamed to numberOfFilesRenamed + 1
refresh -- this is crucial, otherwise the next listing of "variants" will return an incoherent result as outlined above

on error
display dialog "Error on image name: " & (name of variant_) buttons {"OK", "Abort"} default button "OK" cancel button "Abort"
end try

end repeat
display dialog "" & (count of selectedVariants) & " of " & numberOfFilesRenamed & " images renamed" buttons {"OK"}
end tell

--
-- Extract the number suffix of the given file name, e.g. 44 from "DSC00044.jpg"
--
-- fileName must include type suffix, e.g. ".jpg" or ".raw", etc.
--
-- returns integer >= 0 or throws an error
--
on numberSuffix(fileName)
set matchedText to (find text "[0-9]+\\.[^\\.]+$" in fileName with regexp and string result)
set matchedText to (find text "^[0-9]+" in matchedText with regexp and string result)
return matchedText as number
end numberSuffix
QBus
 
Posts: 4
Joined: Wed Oct 12, 2016 8:57 am

Re: Batch Rename Settings

Postby Eric Nepean » Wed Oct 03, 2018 10:45 pm

Hi Qbus
Good progress
Nice Library you reference, thanks!!

Two comments -
1)If you put your AppleScript code between [code] and [/code] (making sure to not select "Disable BBCode" below the editor window) then it becomes much more readable. These character sequences can be generated with the “code” button at the top of the editor.

2) The variant's ID is unique, unlike the name.You can write “get id of variants” and “get the variant whose id is theID” similar to the name (but more robust)
Cheers, Eric
[late 2015 iMac, 4GHz i7, 24GB RAM, external SSDs. GX8, E-M1, GX7, GM5, GM1 ....]
Eric Nepean
 
Posts: 370
Joined: Sat Oct 25, 2014 8:02 am

Re: Batch Rename Settings

Postby QBus » Tue Oct 16, 2018 10:09 pm

Hi Eric

You truly made my evening! Sorry for not replying earlier but I kind of have to "steal" my time to work on this.
The name-based version of my script turned out to be usable when there were multiple variants of a picture and thus your hint number 2) solved this problem within a few minutes. Thank you very much for this!

Regarding 1): absolutely true. I did use the code tags earlier and I must just have hit the wrong button.

So, here is again the (hopefully) final version of my script for others to use:
Code: Select all
--
-- Renames a list of selected image variants honouring the original camera file-name number suffix
--
-- e.g. "DSC00044.jpg" to "AK_2018-02-15_00044.jpg"
--
-- something that Capture One seems unable to do.
--
-- In addition, if the variant IPTC data property "Status Source" property is undefined, it is set to the original
-- file name created by the camera, like so:
--
-- "DSC00044.jpg"
--
-- This can later (and after possible further renamings) be used to revert back to the original file name or to
-- reuse the number suffix.
--
-- Note: This script uses the Satimage osax library for regex pattern matching and won't work without it
-- (see http://www.satimage.fr/software/en/downloads/downloads_companion_osaxen.html)
--

tell application "Capture One 11"
   
   set prefix to "AK"
   set dateFormat to "[Image Year (yyyy)]-[Image Month (MM)]-[Image Day of Month (dd)]" -- use Capture One token names
   set numberSuffixFormat to "[5 Digit Counter]" -- use Capture One token names
   set tokenFormat to prefix & "_" & dateFormat & "_" & numberSuffixFormat
   
   set selectedVariants to variants whose selected is true -- assignment required to have correct type
   if (count of selectedVariants) = 0 then
      display dialog "No selection." buttons {"Abort"}
      return
   end if
   
   --
   -- Important: the Applescript representation of Capture One variants seems to be a rather poor concept
   -- in that it is not much more than a numeric position index in a list rather than a proper, stable data
   -- object for a given image (or variant, to be precise). This shortcoming becomes apparent when the
   -- ordering of the list changes, e.g. if the list represents an image collection of Capture One and the
   -- order of images in the collection is changeing due to ongoing renaming, then a given Applescript
   -- variant can suddenly "point" to an object that wasn't even selected in the first place. As a consequence,
   -- the wrong object may be processed.
   -- Thus, in order to obtain a stable list of selected images, we create a list with just the *is* of the
   -- variants in the current selection (all variants of an image share the same name, so don't
   -- use that. Later, we iterate over those ids later rather than over the "variants" that are currently
   -- selected in Capture One (thanks to Eric Nepaean for this hint).
   --
   set variantIDs to {}
   repeat with v in selectedVariants
      set the end of variantIDs to (id of v)
   end repeat
   
   --
   -- Now "batch"-rename the variants one by one by (= not batch) setting the 'counter' token to the
   -- correct value for the respective variant
   --
   set numberOfFilesRenamed to 0
   repeat with variantID_ in variantIDs
      
      set singleVariantList to (variants whose id is variantID_)
      if ((count of singleVariantList) ≠ 1) then
         display dialog "Error, found " & (count of singleVariantList) & " variant(s) named '" & variantID_ & "'." buttons {"Abort"}
      end if
      
      set variant_ to (item 1 of singleVariantList)
      set originalFileName to (name of (parent image of variant_))
      --display dialog "Original image name: " & originalFileName & ", variant name: " & (name of variant_) buttons {"OK"}
      
      try
         set originalFileNameNumberSuffix to my numberSuffix(originalFileName)
         --display dialog "Original image name: " & (name of variant_) & ", suffix: " & originalFileNameNumberSuffix buttons {"OK", "Abort"} default button "OK" cancel button "Abort"
         
         -- store originalFileName in "IPTC Status Source" field if field is still empty:   
         tell variant_
            if (status source = "") then
               set status source to originalFileName
            end if
         end tell
         
         tell current document
            tell batch rename settings
               set method to text and tokens
               set token format to tokenFormat
               set counter to originalFileNameNumberSuffix
               set pair RAWs and JPGs to true
               set include file extension to false
            end tell
            
            batch rename variants singleVariantList
         end tell
         set numberOfFilesRenamed to numberOfFilesRenamed + 1
         refresh -- this is crucial, otherwise the next listing of "variants" will return an incoherent result as outlined above
         
      on error
         display dialog "Error on image name: " & (name of variant_) buttons {"OK", "Abort"} default button "OK" cancel button "Abort"
      end try
      
   end repeat
   display dialog "" & (count of selectedVariants) & " of " & numberOfFilesRenamed & " images renamed" buttons {"OK"}
end tell

--
-- Extract the number suffix of the given file name, e.g. 44 from "DSC00044.jpg"
--
--   fileName must include type suffix, e.g. ".jpg" or ".raw", etc.
--
--    returns integer >= 0 or throws an error
--
on numberSuffix(fileName)
   set matchedText to (find text "[0-9]+\\.[^\\.]+$" in fileName with regexp and string result)
   set matchedText to (find text "^[0-9]+" in matchedText with regexp and string result)
   return matchedText as number
end numberSuffix
QBus
 
Posts: 4
Joined: Wed Oct 12, 2016 8:57 am

Previous

Return to Scripting



Who is online

Users browsing this forum: No registered users and 2 guests