Advertisement
duquesne9

Import files to new groups based on filename

Jun 27th, 2018
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (*This script based heavily on the work of Rich Walsh of allthatyouhear.com*)
  2.  
  3. set userCopyOnImport to true -- Set this to false if you do not want the files to be copied to the workspace folder
  4. set userForceCopyFromNetwork to true -- Set this to false if you do not want files that aren't on local drives to be copied automatically
  5. 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
  6.  
  7. -- ###FIXME### Should audioFileTypes include "public.ulaw-audio"?
  8. -- ###FIXME### Is videoFileTypes a sufficiently exhaustive list? Are any of the file types not supported by QLab?
  9.  
  10. -- Declarations
  11.  
  12. global dialogTitle, sharedPath, numCues
  13. set dialogTitle to "Import files"
  14. set numCues to "2"
  15. set numCues to my enterSomeText("How many cues? Enter up to 999", "2", false)
  16.  
  17.  
  18. set audioFileTypes to {"com.apple.coreaudio-format", "com.apple.m4a-audio", "com.microsoft.waveform-audio", "public.aifc-audio", "public.aiff-audio", ¬
  19.     "public.audio", "public.mp3", "public.mpeg-4-audio"}
  20. (* This list deliberately excludes "com.apple.protected-mpeg-4-audio" to protect against old DRM-restricted iTunes files *)
  21. set videoFileTypes to {"com.adobe.photoshop-image", "com.apple.icns", "com.apple.macpaint-image", "com.apple.pict", "com.apple.quicktime-image", ¬
  22.     "com.apple.quicktime-movie", "public.3gpp", "public.3gpp2", "public.avi", "public.camera-raw-image", "public.image", "public.jpeg", "public.jpeg-2000", ¬
  23.     "public.movie", "public.mpeg", "public.mpeg-4", "public.png", "public.tiff", "public.video", "public.xbitmap-image"}
  24. set midiFileTypes to {"public.midi-audio"}
  25.  
  26. (* cf: https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html *)
  27.  
  28. set theFileTypes to {audioFileTypes, videoFileTypes, midiFileTypes}
  29. set foldersExist to {null, null, null}
  30. set theSubfolders to {"audio", "video", "midi file"}
  31. set theCueTypes to {"Audio", "Video", "MIDI File"}
  32.  
  33. -- Main routine
  34.  
  35. tell application id "com.figure53.qlab.3" to tell front workspace
  36.    
  37.     -- Establish the path to the current workspace
  38.    
  39.     ---------------------------------------------
  40.     --STUFF RICH DID THAT I KINDA UNDERSTAND--
  41.     ---------------------------------------------
  42.    
  43.     set workspacePath to path
  44.     if workspacePath is missing value then
  45.         display dialog "The current workspace has not yet been saved anywhere." with title dialogTitle with icon 0 ¬
  46.             buttons {"OK"} default button "OK" giving up after 5
  47.         return
  48.     end if
  49.    
  50.     -- Get the path that should prefix all media file paths
  51.    
  52.     tell application "System Events"
  53.         set sharedPath to path of container of file workspacePath
  54.     end tell
  55.    
  56.     -- Choose the files to import
  57.    
  58.     set newTargets to choose file of type {"public.image", "public.audiovisual-content"} ¬
  59.         with prompt "Please select one or more audio, video or MIDI files:" with multiple selections allowed
  60.    
  61.     -- Import them
  62.    
  63.     repeat with eachFile in newTargets
  64.        
  65.         tell application "System Events"
  66.             set eachType to type identifier of eachFile
  67.             set eachName to name of eachFile
  68.             if userForceCopyFromNetwork is true then -- Only check file's locality if it will be relevant
  69.                 set fileIsLocal to local volume of disk (volume of eachFile)
  70.             else
  71.                 set fileIsLocal to true
  72.             end if
  73.         end tell
  74.        
  75.         set eachTarget to eachFile -- This variable will be updated if the file is copied
  76.        
  77.         -- Work through the three types of cues that will be processed
  78.        
  79.         repeat with i from 1 to 3
  80.            
  81.             if eachType is in contents of item i of theFileTypes then
  82.                
  83.                 if (userCopyOnImport is true) or (userForceCopyFromNetwork is true and fileIsLocal is false) then
  84.                    
  85.                     -- If copying is specified by the user definitions then…
  86.                    
  87.                     -- Check for appropriate subfolder next to workspace and make it if it doesn't exist
  88.                    
  89.                     if item i of foldersExist is null then
  90.                         set item i of foldersExist to my checkForFolder(item i of theSubfolders)
  91.                         if item i of foldersExist is false then
  92.                             my makeFolder(item i of theSubfolders)
  93.                         end if
  94.                     end if
  95.                    
  96.                     -- If the file is not already in place, copy it to the appropriate subfolder
  97.                    
  98.                     if my checkForFile(item i of theSubfolders, eachName) is false then
  99.                         my copyFileViaFinder(item i of theSubfolders, eachFile)
  100.                     end if
  101.                    
  102.                     set eachTarget to sharedPath & item i of theSubfolders & ":" & eachName
  103.                    
  104.                 end if
  105.                
  106.                 -- Make an appropriate cue
  107.                
  108.                 make type item i of theCueTypes
  109.                 set newCue to last item of (selected as list)
  110.                 set properties of newCue to {file target:eachTarget, q number:""}
  111.                 --fix ghosting names, try without delay
  112.                 tell application "System Events"
  113.                     keystroke "q"
  114.                     delay 0.1
  115.                     key code 36
  116.                 end tell
  117.                
  118.             end if
  119.         end repeat
  120.        
  121.     end repeat
  122.    
  123.     ---------------
  124.     --MAKE GROUPS--
  125.     ---------------
  126.    
  127.     tell application id "com.figure53.qlab.3" to tell front workspace
  128.        
  129.         repeat with g from 1 to numCues
  130.             make type "group"
  131.             set newGroup to last item of (selected as list)
  132.             set properties of newGroup to {q number:g, q name:"Cue " & g, mode:fire_all}
  133.         end repeat
  134.        
  135.     end tell
  136.    
  137.     ---------------
  138.     --SELECT ALL--
  139.     ---------------
  140.    
  141.     delay 0.5
  142.     tell application "System Events"
  143.         keystroke "a" using {command down}
  144.         --delay 0.1
  145.     end tell
  146.    
  147.     --------------------------
  148.     --RENUMBER FOR MOVE--
  149.     --------------------------
  150.    
  151.     --this is ugly, how to set test as list or array?
  152.    
  153.     repeat with gnum from 1 to numCues
  154.         set y to "1"
  155.         repeat with eachQ in (selected as list)
  156.            
  157.             if (q name of eachQ begins with gnum & " ") then
  158.                 set q number of eachQ to ((gnum & "." & y) as string)
  159.                 set y to y + "1"
  160.             else if (q name of eachQ begins with gnum & "-") then
  161.                 set q number of eachQ to ((gnum & "." & y) as string)
  162.                 set y to y + "1"
  163.             else if (q name of eachQ begins with gnum & ")") then
  164.                 set q number of eachQ to ((gnum & "." & y) as string)
  165.                 set y to y + "1"
  166.             else if (q name of eachQ begins with "0" & gnum & " ") then
  167.                 set q number of eachQ to ((gnum & "." & y) as string)
  168.                 set y to y + "1"
  169.             else if (q name of eachQ begins with "0" & gnum & "-") then
  170.                 set q number of eachQ to ((gnum & "." & y) as string)
  171.                 set y to y + "1"
  172.             else if (q name of eachQ begins with "0" & gnum & ")") then
  173.                 set q number of eachQ to ((gnum & "." & y) as string)
  174.                 set y to y + "1"
  175.             else if (q name of eachQ begins with "00" & gnum & " ") then
  176.                 set q number of eachQ to ((gnum & "." & y) as string)
  177.                 set y to y + "1"
  178.             else if (q name of eachQ begins with "00" & gnum & "-") then
  179.                 set q number of eachQ to ((gnum & "." & y) as string)
  180.                 set y to y + "1"
  181.             else if (q name of eachQ begins with "00" & gnum & ")") then
  182.                 set q number of eachQ to ((gnum & "." & y) as string)
  183.                 set y to y + "1"
  184.                
  185.             else
  186.                 set y to y + "1"
  187.                
  188.             end if
  189.            
  190.         end repeat
  191.        
  192.     end repeat
  193.    
  194.     -------------------------
  195.     --MOVE INTO GROUPS--
  196.     -------------------------
  197.    
  198.     repeat with eachQ in (selected as list)
  199.         set eachQID to uniqueID of eachQ
  200.         repeat with p from 1 to numCues
  201.             if q number of eachQ begins with p & "." then
  202.                 move cue id eachQID of parent of eachQ to end of cue (p as text)
  203.             end if
  204.             --exit repeat
  205.         end repeat
  206.     end repeat
  207.    
  208.     --------------------------
  209.     --REMOVE Q NUMBERS--
  210.     --------------------------
  211.    
  212.     delay 0.1
  213.     tell application "System Events"
  214.         keystroke "a" using {command down}
  215.         delay 0.1
  216.         keystroke "d" using {command down}
  217.     end tell
  218.    
  219. end tell
  220.  
  221. if (count newTargets) > userReassuranceThreshold then
  222.     display dialog "Done." with title dialogTitle with icon 1 buttons {"OK"} default button "OK" giving up after 5
  223. end if
  224.  
  225. -- Subroutines
  226.  
  227. on checkForFolder(theSuffix) -- [Shared subroutine]
  228.     tell application "System Events"
  229.         return exists folder (sharedPath & theSuffix)
  230.     end tell
  231. end checkForFolder
  232.  
  233. on makeFolder(theFolder) -- [Shared subroutine]
  234.     tell application "Finder"
  235.         make new folder at sharedPath with properties {name:theFolder}
  236.     end tell
  237. end makeFolder
  238.  
  239. on checkForFile(theSuffix, theName) -- [Shared subroutine]
  240.     tell application "System Events"
  241.         return exists file (sharedPath & theSuffix & ":" & theName)
  242.     end tell
  243. end checkForFile
  244.  
  245. on copyFileViaFinder(theSuffix, theFile)
  246.     (* NB: by using the Finder the usual file-copy progress window is invoked, which may be more reassuring than the faceless
  247.     'do shell script "cp -p " & quoted form of POSIX path of theFile & " " & quoted form of POSIX path of (sharedPath & theSuffix & ":" & theName)'
  248.     - 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) *)
  249.     tell application "Finder"
  250.         duplicate theFile to folder (sharedPath & theSuffix)
  251.     end tell
  252. end copyFileViaFinder
  253.  
  254.  
  255. on enterSomeText(thePrompt, defaultAnswer, emptyAllowed) -- [Shared subroutine]
  256.     tell application id "com.figure53.QLab.3"
  257.         set theAnswer to ""
  258.         repeat until theAnswer is not ""
  259.             set theAnswer to text returned of (display dialog thePrompt with title dialogTitle default answer defaultAnswer buttons {"Cancel", "OK"} ¬
  260.                 default button "OK" cancel button "Cancel")
  261.             if emptyAllowed is true then exit repeat
  262.         end repeat
  263.         return theAnswer
  264.     end tell
  265. end enterSomeText
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement