Using Applesript to select an image

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: Using Applesript to select an image

Postby NNN637149307921824277 » Mon Jan 27, 2020 11:03 am

Copy and paste from those, or type the AppleScript source given here, into the editor window.
NNN637149307921824277
 
Posts: 5
Joined: Sat Jan 18, 2020 8:53 am

Re: Using Applesript to select an image

Postby steuieg » Tue May 19, 2020 7:42 pm

Hi there,

So I have been working on something similar, so I adjusted my script to take variants, either by name or a selection from a collection view, and move them to a new collection named by you.

Let me know if it works for you

I wrote it on Mac OSX 10146, Capture One 20, build 13.0.0.186

It would be nice to see how the script behaves on other OSes

Thanks
-s

EDIT:
I have added some error trapping for user interface logic and placed the script below!

Code: Select all
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

(*
BY: Stewart Ghazvini
DATE: 05/19/2020
steuieg@gmail.com
*)

(*
Can get selected of an image in a collection; set myVChoicePick to selected of myVChoice
But can not set an item as the selected; set myChoice to selected ??????
*)

################################################################################################
#   >>FUNCTIONS START<<
################################################################################################
on diag_askQuestion(aQuestion, anAns)
   
   --set myQ to "Question" & return & return & "\"Quesiton\"?"
   --set myA to "Answer"
   --diag_askQuestion(myQ, myA)
   --set myResponse to the result
   
   tell application "SystemUIServer"
      activate
      set myResponse to the text returned of (display dialog aQuestion default answer anAns buttons {"Cancel", "OK"} default button "OK")
      return myResponse
   end tell
end diag_askQuestion
--
on diag_getListChoice(allChoices, aMsg, aTitle, multipleTrue)
   
   --set myMSG to "Message... Thanks!"
   --set myTitle to "TITLE"
   --set myList to {"ITEM1", "ITEM2", "ITEM3"}
   --set multipleTrue to true
   --diag_getListChoice(myList, myMSG, myTitle, multipleTrue) of me
   --set myChoice to result
   
   tell application "SystemUIServer"
      --   activate
      if multipleTrue is true then
         set myList to choose from list allChoices with title aTitle with prompt aMsg with multiple selections allowed without empty selection allowed
      else
         set myList to choose from list allChoices with title aTitle with prompt aMsg without multiple selections allowed and empty selection allowed
      end if
      if myList is false then error number -128
      return myList
   end tell
end diag_getListChoice
--
on diag_showMsg(aMsg, giveUp)
   
   --set giveUp to 5
   --set myMSG to "All done!"
   --diag_showMsg(myMSG, giveUp)
   
   if aMsg is "" then
      set aMsg to "Hmmmm... no message was sent into the function!"
   end if
   if giveUp is "" then
      set giveUp to 5
   end if
   tell application "SystemUIServer"
      activate
      display dialog aMsg buttons {"OK"} default button "OK" giving up after giveUp
   end tell
end diag_showMsg
--
on diag_showNotify(aMsg, aTitle, aSubTitle)
   
   --set myMsg to "Hello"
   --set myTitle to "Some title"
   --set mySubTitle to "Some subtitle"
   --diag_showNotify(myMsg, myTitle, mySubTitle)
   
   display notification aMsg with title aTitle subtitle aSubTitle
end diag_showNotify
--
################################################################################################
#   >>FUNCTIONS END<<
################################################################################################


################################################################################################
#   >>MAIN START<<
################################################################################################
on run
   set myMSG to "Choose how to select your image/s!"
   set myTitle to "IMAGE CHOICE"
   set myList to {"By Name", "From Collection"}
   set multipleTrue to false
   diag_getListChoice(myList, myMSG, myTitle, multipleTrue) of me
   set findMethod to item 1 of result
   
   set myQ to "Collection Name" & return & return & "\"Enter the name of the collection please\"!"
   set myA to "Collection_Name"
   diag_askQuestion(myQ, myA)
   set newCollectionName to the result
   
   
   set newName to false
   repeat until newName is true
      tell application "Capture One 20"
         tell current document
            try
               set newCollection to make new collection with properties {kind:album, name:newCollectionName}
               set newName to true
            on error
               set myMSG to "That name is taken already! How shall I proceed?"
               set myTitle to "COLLECTION NAME TAKEN"
               set myList to {"Use It", "Make new collection", "Stop"}
               set multipleTrue to false
               diag_getListChoice(myList, myMSG, myTitle, multipleTrue) of me
               set collectionKeep to item 1 of result
               if collectionKeep is "Make new collection" then
                  set myQ to "NAME OF NEW COLLECTION" & return & return & "\"Exists\"!"
                  set myA to "New_Collection"
                  diag_askQuestion(myQ, myA) of me
                  set newCollectionName to the result
               else if collectionKeep is "Use It" then
                  set newCollection to item 1 of (every collection whose name is newCollectionName)
                  set newName to true
               else
                  error number -128
               end if
            end try
         end tell
      end tell
   end repeat
   
   if findMethod is "By Name" then
      set myQ to "IMAGE NAME" & return & return & "\"What is the name of image\" ?"
      set myA to "Image_Name" -- 2020_0514-Baronsmede-Timelapse-Test 67
      diag_askQuestion(myQ, myA)
      set aName to the result
      
      tell application "Capture One 20"
         tell current document
            set thisDocName to name
            try
               set variantsToMove to every variant whose name is aName
               if variantsToMove is {} then
                  set giveUp to 5
                  set myMSG to "Sorry, the name \"" & aName & "\" was not found in \"All Images\" Collection of \"" & thisDocName & "\""
                  diag_showMsg(myMSG, giveUp) of me
                  error number -128
               end if
            on error errMsg number errNum
               set giveUp to 30
               set myMSG to "Sorry, there was an error with the following code: " & "\"" & errMsg & "\" number: \"" & errNum & "\""
               diag_showMsg(myMSG, giveUp) of me
               error number -128
            end try
         end tell
      end tell
   else
      
      tell application "Capture One 20"
         tell current document
            set currCollectionName to name of current collection
            if newCollectionName is currCollectionName then
               set myMSG to "The requested colleciton " & "\"" & newCollectionName & "\"" & return & return & "is the current collection, which means you are trying to move variants from this collection back into itself..." & return & return & "Sorry, does not make sense!" & return & return & "Either use another collection to choose variants, or run script again and use a different name! Thanks"
               set giveUp to 120
               diag_showMsg(myMSG, giveUp) of me
               error number -128
            end if
         end tell
         
         set thisCollectionImages to the name of every variant
         if thisCollectionImages is {} then
            set myMSG to "The current colleciton " & "\"" & currCollectionName & "\"" & return & return & "is empty..." & return & return & "Sorry, does not make sense!" & return & return & "Choose a collection that has some variants inside it and rerun script! Thanks"
            set giveUp to 120
            diag_showMsg(myMSG, giveUp) of me
            error number -128
         end if
         
         set myMSG to "Please choose the images you would like to move from the list... Thanks!"
         set myTitle to "IMAGES TO MOVE TO \"" & newCollectionName & "\" COLLECTION"
         set myList to thisCollectionImages
         set multipleTrue to true
         diag_getListChoice(myList, myMSG, myTitle, multipleTrue) of me
         set varNamesList to result
         
         set variantsToMove to {}
         repeat with i from 1 to count of varNamesList
            set thisVariantName to item i of varNamesList
            set the end of variantsToMove to variant thisVariantName
         end repeat
      end tell
   end if
   
   tell application "Capture One 20"
      tell current document
         try
            set newCollection to make new collection with properties {kind:album, name:newCollectionName}
         on error
            set newCollection to item 1 of (every collection whose name is newCollectionName)
         end try
         add inside newCollection variants variantsToMove
      end tell
   end tell
   
   set myMSG to "Script has completed!" & return & return & "There were \"" & (count of varNamesList) & "\" variants moved to collection \"" & newCollectionName & "\""
   set giveUp to 30
   diag_showMsg(myMSG, giveUp)
   
end run
################################################################################################
#   >>MAIN END<<
################################################################################################
steuieg
 
Posts: 3
Joined: Tue Oct 31, 2017 8:40 pm

Previous

Return to Scripting



Who is online

Users browsing this forum: No registered users and 3 guests