Advertisement
duquesne9

Batch import to new groups

Mar 12th, 2019
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Ctrl+Op+I BATCH IMPORT MEDIA
  2. -- Will sort by numeric prefix and place into groups, files starting with letter placed at end
  3. (*
  4. Tested with QLab v3.2.14 Oct 2018
  5. Please report any issues to robotlightsyou@gmail.com with subject "QLAB 3 template issues"
  6.  
  7. Original from Rich Walsh template of allthatyouhear.com*)
  8.  
  9. set userCopyOnImport to true -- Set this to false if you do not want the files to be copied to the workspace folder
  10. set userForceCopyFromNetwork to true -- Set this to false if you do not want files that aren't on local drives to be copied automatically
  11. set userReassuranceThreshold to 5 -- When the number of files imported is greater than this variable, a dialog will let you know that the process is complete
  12.  
  13. -- ###FIXME### Should audioFileTypes include "public.ulaw-audio"?
  14. -- ###FIXME### Is videoFileTypes a sufficiently exhaustive list? Are any of the file types not supported by QLab?
  15.  
  16. -- Declarations
  17.  
  18. global dialogTitle, sharedPath, numCues
  19. set dialogTitle to "Import files"
  20. set numCues to "2"
  21. set numCues to my enterSomeText("How many cues in your show? Enter up to 999.
  22.  
  23. Also, probably turn the sound down.", "2", false)
  24.  
  25.  
  26. set audioFileTypes to {"com.apple.coreaudio-format", "com.apple.m4a-audio", "com.microsoft.waveform-audio", "public.aifc-audio", "public.aiff-audio", ¬
  27.     "public.audio", "public.mp3", "public.mpeg-4-audio"}
  28. (* This list deliberately excludes "com.apple.protected-mpeg-4-audio" to protect against old DRM-restricted iTunes files *)
  29. set videoFileTypes to {"com.adobe.photoshop-image", "com.apple.icns", "com.apple.macpaint-image", "com.apple.pict", "com.apple.quicktime-image", ¬
  30.     "com.apple.quicktime-movie", "public.3gpp", "public.3gpp2", "public.avi", "public.camera-raw-image", "public.image", "public.jpeg", "public.jpeg-2000", ¬
  31.     "public.movie", "public.mpeg", "public.mpeg-4", "public.png", "public.tiff", "public.video", "public.xbitmap-image"}
  32. set midiFileTypes to {"public.midi-audio"}
  33.  
  34. (* cf: https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html *)
  35.  
  36. set theFileTypes to {audioFileTypes, videoFileTypes, midiFileTypes}
  37. set foldersExist to {null, null, null}
  38. set theSubfolders to {"audio", "video", "midi file"}
  39. set theCueTypes to {"Audio", "Video", "MIDI File"}
  40.  
  41. -- Main routine
  42.  
  43. tell application id "com.figure53.qlab.3" to tell front workspace
  44.    
  45.     -- Establish the path to the current workspace
  46.    
  47.     ---------------------------------------------
  48.     --STUFF RICH DID THAT I KINDA UNDERSTAND--
  49.     ---------------------------------------------
  50.    
  51.     set workspacePath to path
  52.     if workspacePath is missing value then
  53.         display dialog "The current workspace has not yet been saved anywhere." with title dialogTitle with icon 0 ¬
  54.             buttons {"OK"} default button "OK" giving up after 5
  55.         return
  56.     end if
  57.    
  58.     -- Get the path that should prefix all media file paths
  59.    
  60.     tell application "System Events"
  61.         set sharedPath to path of container of file workspacePath
  62.     end tell
  63.    
  64.     -- Choose the files to import
  65.    
  66.     set newTargets to choose file of type {"public.image", "public.audiovisual-content"} ¬
  67.         with prompt "Please select one or more audio, video or MIDI files:" with multiple selections allowed
  68.    
  69.     -- Import them
  70.     display dialog "Mixing Cuemicals" with title dialogTitle with icon 1 buttons {"OK"} default button "OK" giving up after 1
  71.    
  72.     repeat with eachFile in newTargets
  73.        
  74.         tell application "System Events"
  75.             set eachType to type identifier of eachFile
  76.             set eachName to name of eachFile
  77.             if userForceCopyFromNetwork is true then -- Only check file's locality if it will be relevant
  78.                 set fileIsLocal to local volume of disk (volume of eachFile)
  79.             else
  80.                 set fileIsLocal to true
  81.             end if
  82.         end tell
  83.        
  84.         set eachTarget to eachFile -- This variable will be updated if the file is copied
  85.        
  86.         -- Work through the three types of cues that will be processed
  87.        
  88.         repeat with i from 1 to 3
  89.            
  90.             if eachType is in contents of item i of theFileTypes then
  91.                
  92.                 if (userCopyOnImport is true) or (userForceCopyFromNetwork is true and fileIsLocal is false) then
  93.                    
  94.                     -- If copying is specified by the user definitions then…
  95.                    
  96.                     -- Check for appropriate subfolder next to workspace and make it if it doesn't exist
  97.                    
  98.                     if item i of foldersExist is null then
  99.                         set item i of foldersExist to my checkForFolder(item i of theSubfolders)
  100.                         if item i of foldersExist is false then
  101.                             my makeFolder(item i of theSubfolders)
  102.                         end if
  103.                     end if
  104.                    
  105.                     -- If the file is not already in place, copy it to the appropriate subfolder
  106.                    
  107.                     if my checkForFile(item i of theSubfolders, eachName) is false then
  108.                         my copyFileViaFinder(item i of theSubfolders, eachFile)
  109.                     end if
  110.                    
  111.                     set eachTarget to sharedPath & item i of theSubfolders & ":" & eachName
  112.                    
  113.                 end if
  114.                
  115.                 -- Make an appropriate cue
  116.                
  117.                 make type item i of theCueTypes
  118.                 set newCue to last item of (selected as list)
  119.                 set properties of newCue to {file target:eachTarget, q number:""}
  120.                 --fix ghosting names, try without delay
  121.                 tell application "System Events"
  122.                     keystroke "q"
  123.                     delay 0.1
  124.                     key code 36
  125.                 end tell
  126.                
  127.             end if
  128.         end repeat
  129.        
  130.     end repeat
  131.    
  132.     ---------------
  133.     --MAKE GROUPS--
  134.     ---------------
  135.    
  136.     tell application id "com.figure53.qlab.3" to tell front workspace
  137.        
  138.         display dialog "Preparing groups" with title dialogTitle with icon 1 buttons {"OK"} default button "OK" giving up after 1
  139.        
  140.         repeat with g from 1 to numCues
  141.             make type "group"
  142.             set newGroup to last item of (selected as list)
  143.             set properties of newGroup to {q number:g, q name:"Cue " & g, mode:fire_all}
  144.         end repeat
  145.        
  146.     end tell
  147.    
  148.     ---------------
  149.     --SELECT ALL--
  150.     ---------------
  151.    
  152.     delay 0.5
  153.     tell application "System Events"
  154.         keystroke "a" using {command down}
  155.         --delay 0.1
  156.     end tell
  157.    
  158.     --------------------------
  159.     --RENUMBER FOR MOVE--
  160.     --------------------------
  161.    
  162.    
  163.     display dialog "Doing some math, this may take a moment" with title dialogTitle with icon 1 buttons {"OK"} default button "OK" giving up after 1.5
  164.    
  165.     --this is ugly, how to set test as list or array?
  166.    
  167.     repeat with gnum from 1 to numCues
  168.         set y to "1"
  169.         repeat with eachQ in (selected as list)
  170.            
  171.             if (q name of eachQ begins with gnum & " ") then
  172.                 set q number of eachQ to ((gnum & "." & y) as string)
  173.                 set y to y + "1"
  174.             else if (q name of eachQ begins with gnum & ".") then
  175.                 set q number of eachQ to ((gnum & "." & y) as string)
  176.                 set y to y + "1"
  177.             else if (q name of eachQ begins with gnum & "-") then
  178.                 set q number of eachQ to ((gnum & "." & y) as string)
  179.                 set y to y + "1"
  180.             else if (q name of eachQ begins with gnum & ")") then
  181.                 set q number of eachQ to ((gnum & "." & y) as string)
  182.                 set y to y + "1"
  183.             else if (q name of eachQ begins with "0" & gnum & " ") then
  184.                 set q number of eachQ to ((gnum & "." & y) as string)
  185.                 set y to y + "1"
  186.             else if (q name of eachQ begins with "0" & gnum & ".") then
  187.                 set q number of eachQ to ((gnum & "." & y) as string)
  188.                 set y to y + "1"
  189.             else if (q name of eachQ begins with "0" & gnum & "-") then
  190.                 set q number of eachQ to ((gnum & "." & y) as string)
  191.                 set y to y + "1"
  192.             else if (q name of eachQ begins with "0" & gnum & ")") then
  193.                 set q number of eachQ to ((gnum & "." & y) as string)
  194.                 set y to y + "1"
  195.             else if (q name of eachQ begins with "00" & gnum & " ") then
  196.                 set q number of eachQ to ((gnum & "." & y) as string)
  197.                 set y to y + "1"
  198.             else if (q name of eachQ begins with "00" & gnum & ".") then
  199.                 set q number of eachQ to ((gnum & "." & y) as string)
  200.                 set y to y + "1"
  201.             else if (q name of eachQ begins with "00" & gnum & "-") then
  202.                 set q number of eachQ to ((gnum & "." & y) as string)
  203.                 set y to y + "1"
  204.             else if (q name of eachQ begins with "00" & gnum & ")") then
  205.                 set q number of eachQ to ((gnum & "." & y) as string)
  206.                 set y to y + "1"
  207.                
  208.             else
  209.                 set y to y + "1"
  210.                
  211.             end if
  212.            
  213.         end repeat
  214.        
  215.     end repeat
  216.    
  217.     -------------------------
  218.     --MOVE INTO GROUPS--
  219.     -------------------------
  220.    
  221.     display dialog "Shuffle up and deal!" with title dialogTitle with icon 1 buttons {"OK"} default button "OK" giving up after 1
  222.    
  223.     repeat with eachQ in (selected as list)
  224.         set eachQID to uniqueID of eachQ
  225.         repeat with p from 1 to numCues
  226.             if q number of eachQ begins with p & "." then
  227.                 move cue id eachQID of parent of eachQ to end of cue (p as text)
  228.             end if
  229.         end repeat
  230.     end repeat
  231.    
  232.    
  233.    
  234.    
  235.     try
  236.         repeat with selectedCue in (selected as list)
  237.             if q type of selectedCue is "Group" then
  238.                 if (count cues of selectedCue) is not 1 then
  239.                     set theCues to cues of selectedCue
  240.                     set previousPre to 0
  241.                     set mode of selectedCue to fire_first_enter_group
  242.                     repeat with eachCue in theCues
  243.                         set eachPre to pre wait of eachCue
  244.                         set deltaTime to eachPre - previousPre
  245.                         set previousPre to eachPre
  246.                         set pre wait of eachCue to deltaTime
  247.                         if contents of eachCue is not last item of theCues then
  248.                             set continue mode of eachCue to auto_continue
  249.                         end if
  250.                     end repeat
  251.                     set q name of selectedCue to ((q name of selectedCue) & " options")
  252.                 end if
  253.             end if
  254.         end repeat
  255.     end try
  256.    
  257.     --------------------------
  258.     --SORTING COMPLETE--
  259.     --------------------------
  260.    
  261.    
  262.     display dialog "Helping groups identify as their true self" with title dialogTitle with icon 1 buttons {"OK"} default button "OK" giving up after 1.5
  263.    
  264.    
  265.     set optionName to " (drag final choice here, delete options group)"
  266.    
  267.     repeat with theQ in (selected as list)
  268.        
  269.        
  270.         if q type of theQ is "Group" then
  271.             set groupNum to q number of theQ
  272.             if (count cues of theQ) is not 1 then
  273.                 make type "group"
  274.                 set optionGroup to last item of (selected as list)
  275.                 set optionGroupID to the uniqueID of optionGroup
  276.                 set theQID to uniqueID of theQ
  277.                 set optionName to "Drag final choice here, delete options group"
  278.                 set properties of optionGroup to {q name:"Cue " & groupNum & optionName, q number:"", mode:fire_all}
  279.                 --working line, revert to here
  280.                 move cue id optionGroupID of parent of optionGroup to end of cue (groupNum as text)
  281.                 (*
  282.                 set originalCueIsIn to parent of theQ
  283.                 if parent of optionGroup is originalCueIsIn then -- Only reorder the cues if they are in the same group/cue list
  284.                     set originalCueID to uniqueID of theQ
  285.                     --set newCueID to uniqueID of newCue
  286.                     move cue id originalCueID of originalCueIsIn to cue after cue id optionGroupID of originalCueIsIn
  287.             end if
  288.                
  289.                
  290.                
  291.                 tell parent of optionGroup
  292.                     move cue id optionGroupID to after cue (groupNum as text)
  293.                 end tell
  294.                 *)
  295.             end if
  296.            
  297.         end if
  298.        
  299.     end repeat
  300.    
  301.     --------------------------
  302.     --REMOVE Q NUMBERS--
  303.     --------------------------
  304.     delay 0.1
  305.     tell application "System Events"
  306.         keystroke "a" using {command down}
  307.         delay 0.1
  308.         keystroke "d" using {command down}
  309.     end tell
  310.    
  311.    
  312.    
  313. end tell
  314.  
  315. if (count newTargets) > userReassuranceThreshold then
  316.     display dialog "Done." with title dialogTitle with icon 1 buttons {"OK"} default button "OK" giving up after 5
  317. end if
  318.  
  319. -- Subroutines
  320.  
  321. on checkForFolder(theSuffix) -- [Shared subroutine]
  322.     tell application "System Events"
  323.         return exists folder (sharedPath & theSuffix)
  324.     end tell
  325. end checkForFolder
  326.  
  327. on makeFolder(theFolder) -- [Shared subroutine]
  328.     tell application "Finder"
  329.         make new folder at sharedPath with properties {name:theFolder}
  330.     end tell
  331. end makeFolder
  332.  
  333. on checkForFile(theSuffix, theName) -- [Shared subroutine]
  334.     tell application "System Events"
  335.         return exists file (sharedPath & theSuffix & ":" & theName)
  336.     end tell
  337. end checkForFile
  338.  
  339. on copyFileViaFinder(theSuffix, theFile)
  340.     (* NB: by using the Finder the usual file-copy progress window is invoked, which may be more reassuring than the faceless
  341.     'do shell script "cp -p " & quoted form of POSIX path of theFile & " " & quoted form of POSIX path of (sharedPath & theSuffix & ":" & theName)'
  342.     - which may look like a freeze (the -p flag copies every property of a file; "theName" would need to be passed to the subroutine to implement this) *)
  343.     tell application "Finder"
  344.         duplicate theFile to folder (sharedPath & theSuffix)
  345.     end tell
  346. end copyFileViaFinder
  347.  
  348.  
  349. on enterSomeText(thePrompt, defaultAnswer, emptyAllowed) -- [Shared subroutine]
  350.     tell application id "com.figure53.QLab.3"
  351.         set theAnswer to ""
  352.         repeat until theAnswer is not ""
  353.             set theAnswer to text returned of (display dialog thePrompt with title dialogTitle default answer defaultAnswer buttons {"Cancel", "OK"} ¬
  354.                 default button "OK" cancel button "Cancel")
  355.             if emptyAllowed is true then exit repeat
  356.         end repeat
  357.         return theAnswer
  358.     end tell
  359. end enterSomeText
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement