Script to Search for Images

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

Script to Search for Images

Postby Eric Nepean » Fri Mar 24, 2017 8:47 am

Here is an improved script that searches for user collections containing the selected images

Code: Select all
-- Applescript to search a COP 10 Catalog for Collections containing the Image of a selected variant
-- Version 5.0 !! NO SUPPORT !!  Eric Valk, Ottawa, Canada

-- ***To Initialise
-- Start Script Editor, open a new (blank) file, copy and paste all of this code into the file, compile (hammer symbol) and save.
-- Best if you make "Scripts" folder somewhere handy in your Douments or Desktop

-- *** Operation
-- Open  the compiled and saved file
-- Open the Script Editor log window, and select the messages tab
-- Select only a small number of variants, from any collection.
-- Run the script
-- Results appear first in the AppleScript Log when the search for each variant is completed, and in Display Dialog Windows when all searching is completed
-- This script does not write or delete any information in the COP Catalog or Session or the image file


property searchTimePerVariant : 0

global debug
set debug to false

global maxSearchLevel
set maxSearchLevel to 100

global searchedImageID
set searchedImageID to ""

global theAppName

global COPDocName
set COPDocName to ""

set InitMaxVariants to 3
set maxSearchTime to 30

set minCOPversion to 10.0 as real


tell application "System Events"
   set COPProcList to every process whose name contains "Capture One" and background only is false
end tell

if (count of COPProcList) = 0 then error "COP is not running"
if (count of COPProcList) ≠ 1 then error "Unexpected: >1 COP instances"
set theAppRef to item 1 of COPProcList

tell application "System Events" to set theAppName to ((get name of theAppRef) as text)

using terms from application "Capture One 10"
   tell application theAppName to set copVersion to (get app version)
end using terms from

tell application "System Events" to set copDetailedVersion to get version of my application theAppName

if debug then
   --properties of application "Capture One 10"
   tell application "System Events"
      log (get every process whose background only is false)
   end tell
   log "theAppName: " & theAppName
   log "COP Version: " & copVersion
   log "COP Detailed Version: " & copDetailedVersion
   log "Max Search Level: " & maxSearchLevel
   log "Measured Search Time Per Variant: " & searchTimePerVariant
   log "maxSearchTime: " & maxSearchTime
end if

if the theAppName ≠ "Capture One 10" then
   display notification "Wrong COP Application"
   error "Found COP Application " & theAppName & " The only supported COP application is Capture One 10"
end if

set numCOPversion to (item 1 of (extract_version_strings(".", (word -1 of copVersion)))) as integer
if numCOPversion < minCOPversion then
   display notification "COP Version is too low"
   error "This COP Version is " & numCOPversion & " - the minimum supported COP version is " & minCOPversion
end if

tell application "System Events" to set frontmost of process theAppName to true

set everyDocName to name of every document of my application theAppName
if (count of everyDocName) = 0 then error theAppName & " has no open documents"

-- searching for the window with AXMain = true can choose the Activities, Live View, Events or Viewer windows if they are open
-- so we search for windows with the same name as open documents

tell application "System Events"
   tell process theAppName
      set WinTitles_L to get title of every window
   end tell
end tell

if debug then log "Windows: "
if debug then log WinTitles_L

set WinTitlesCount to count of WinTitles_L
set COPDocName to ""

if WinTitlesCount > 0 then
   repeat with WinTitlesCtr from 1 to WinTitlesCount --find the first window which is a catalog
      set thisWinTitle to item WinTitlesCtr of WinTitles_L
      if everyDocName contains thisWinTitle then -- we find the document's main window
         if debug then log "found: " & thisWinTitle
         set COPDocName to thisWinTitle
         exit repeat
      end if
   end repeat
end if
if debug then log "COPDocName: " & COPDocName

if COPDocName = "" then
   display notification "Document not found " & everyDocName & " :" & WinTitlesCount & ": " & WinTitles_L
   error "No open documents found"
end if

using terms from application "Capture One 10" -- now check if this is a catalog
   tell application theAppName to set thisDocKind to (get kind of document thisWinTitle) as text
end using terms from
if debug then log "kind: " & thisDocKind

if thisDocKind ≠ "catalog" then
   display notification "This Applescript only works with Catalogs" & return & COPDocName & " is a " & thisDocKind
   error "This Applescript only works with Catalogs:   " & COPDocName & " is a " & thisDocKind
end if

using terms from application "Capture One 10"
   tell application theAppName
      
      set selectedVariants to (get selected variants)
      set countSelectedVariants to count of selectedVariants
      if debug then log "In this window " & (count of selectedVariants) & " Selected Variants"
      
      if countSelectedVariants = 0 then
         display alert "No images selected -  select one or more images"
         error "No images selected"
      else
         set estimatedDuration to countSelectedVariants * searchTimePerVariant
         
         if countSelectedVariants > InitMaxVariants and searchTimePerVariant = 0 then
            display dialog "Are you sure you want to search for " & countSelectedVariants & " variants? " with icon caution
            
         else if estimatedDuration > maxSearchTime then
            display dialog "Are you sure you want to search for " & countSelectedVariants & " variants? Estimated time: " & estimatedDuration & " seconds" with icon caution
         end if
         
         set startTime to get time of (get current date)
         tell document COPDocName
            
            set everyCollection to get every collection
            if (count of everyCollection) = 1 then -- If there are no user collections, there is only the "All Images" collection.
               display notification "No User Collections to Search"
               error "No User Collections to Search"
            end if
            
            set allResults to {}
            
            repeat with v_Counter from 1 to countSelectedVariants
               
               
               set thisVariant to item v_Counter of selectedVariants
               set searchedImage to (get parent image of thisVariant)
               set searchedImageName to (get name of searchedImage)
               set searchedImageID to (get id of searchedImage)
               
               if debug then log "Searching for " & searchedImageName & "     ID: " & searchedImageID
               display notification "Now searching for " & searchedImageID
               
               set isFound to false
               set foundPaths to ""
               set pathSeparator to ""
               set searchLevel to 0
               set nextSearchLevel to searchLevel + 1
               
               if nextSearchLevel ≤ maxSearchLevel then
                  repeat with c_Counter from 1 to count of everyCollection
                     
                     set searchCollName to (get name of collection c_Counter) as text
                     set searchCollKind to (get kind of collection c_Counter) as text
                     
                     if searchCollName ≠ "All Images" then
                        
                        set resultPaths to my search_collection((collection c_Counter), nextSearchLevel)
                        set resultCount to count of resultPaths
                        
                        if debug then log "Result of search in " & searchCollName & " (" & searchCollKind & ") is: " & resultPaths
                        
                        if resultCount > 0 then
                           set isFound to true
                           repeat with p_counter from 1 to count of resultPaths
                              
                              set foundPaths to foundPaths & pathSeparator & item p_counter of resultPaths
                              set pathSeparator to return -- after the first path is added the separator become the newline character /r
                              
                           end repeat
                        end if
                     end if
                  end repeat
               end if
               
               if isFound then
                  set thisResult to {v_Counter, searchedImageName, true, foundPaths}
                  log searchedImageName & " was found in:"
                  log foundPaths
                  display notification searchedImageName & " was found"
               else
                  set thisResult to {v_Counter, searchedImageName, false, foundPaths}
                  display notification searchedImageName & " was not found"
               end if
               set allResults to allResults & thisResult
            end repeat
            
            
            set stopTime to get time of (get current date)
            if stopTime < startTime then set startTime to startTime - 86400 -- the search started yesterday and ended today
            set elapsedtime to stopTime - startTime
            log "This search took " & elapsedtime & " seconds"
            set searchTimePerVariant to elapsedtime / countSelectedVariants
            
            
            say "Finished!"
            repeat with v_Counter0 from 0 to countSelectedVariants - 1
               set searchedImageName to (item ((v_Counter0 * 4) + 2) of allResults) as text
               set successImageName to (item ((v_Counter0 * 4) + 3) of allResults) as boolean
               set pathsImageName to (item ((v_Counter0 * 4) + 4) of allResults) as text
               if successImageName then
                  set foundImagename to "found in"
               else
                  set foundImagename to "not found"
               end if
               display alert searchedImageName & " was " & foundImagename & ":" message pathsImageName giving up after 180
            end repeat
         end tell
      end if
   end tell
end using terms from


tell application "System Events" to set frontmost of process "Script Editor" to true


--Handlers -------------------

on search_collection(thisCollection, searchLevel)
   -- recursive handler to search a collection and it's subcollections
   -- if successful, returns a list of paths each as a text string
   
   global debug
   global maxSearchLevel
   global searchedImageID
   global COPDocName
   global theAppName
   
   set nextSearchLevel to searchLevel + 1
   
   --tell application "Capture One 10"
   using terms from application "Capture One 10"
      tell application theAppName
         tell document COPDocName
            tell thisCollection
               set {thisCollName, thisCollKind} to {name as text, kind as text}
               set CollsinthisColl to every collection
               set countCollsthisColl to count of CollsinthisColl
               
               set everyImageIDList to get id of every image
               set found_paths to {} -- initialise the list
               if everyImageIDList contains searchedImageID then
                  set found_paths to found_paths & (thisCollName & "[" & thisCollKind & "]")
                  if debug then log "Found here"
               end if
               
               if debug then
                  --set everyParentList to get parent image of every variant
                  --set countParents to count of everyParentList            
                  --set nameCollsthisColl to (get name of every collection)
                  --set thisCollImages to get every image
                  --set everyImageNameList to get name of every image
                  log "In " & thisCollName & " at level " & searchLevel & " (" & thisCollKind & ")   found " & (count of everyImageIDList) & " images   " & countCollsthisColl & " collections"
               end if
               
               
               set nextSearchLevel to searchLevel + 1
               if nextSearchLevel ≤ maxSearchLevel then
                  
                  if countCollsthisColl > 0 then
                     repeat with c_Counter from 1 to countCollsthisColl
                        
                        set resultPaths to my search_collection((item c_Counter of CollsinthisColl), nextSearchLevel)
                        set countResultPaths to count of resultPaths
                        
                        if debug then
                           set searchCollName to (get name of collection c_Counter) as text
                           set searchCollKind to (get kind of collection c_Counter) as text
                           log "Result of search in " & searchCollName & " (" & searchCollKind & ") is: " & resultPaths
                        end if
                        
                        if countResultPaths > 0 then
                           repeat with p_counter from 1 to countResultPaths
                              set found_paths to found_paths & (thisCollName & "<" & item p_counter of resultPaths)
                           end repeat
                        end if
                        
                     end repeat
                  end if
               end if
               
            end tell
         end tell
      end tell
   end using terms from
   return found_paths
   
end search_collection


on extract_version_strings(d, t)
   set astid to AppleScript's text item delimiters
   try
      set AppleScript's text item delimiters to d
      set version_strings to text items of t
   on error
      set AppleScript's text item delimiters to astid
   end try
   set AppleScript's text item delimiters to astid
   
   return version_strings
end extract_version_strings
Cheers
Eric
(OSX 10.12, iMac and MacBook Air, Panasonic GX7,GM5,G5, Olympus E-M1)
Eric Nepean
 
Posts: 385
Joined: Sat Jun 28, 2014 8:54 pm
Location: Ontario, Canada

Return to Scripting



Who is online

Users browsing this forum: No registered users and 2 guests