No internet connection
  1. Home
  2. How to

Embedding AppleScript code in if/else statements is cumbersome

By @anon6770933309
    2018-06-27 17:03:00.857Z

    When using AppleScript code that contains variables, handlers, etc. its very cumbersome to embed this as one has to repeat variables and handlers in the if and else statements. Else they will not be found.

    Example:

    // Record enable a print track "MxPrint" and mute it in case it isn't already muted
    var track = sf.ui.proTools.trackGetByName({ name: 'MxPrint' }).track;
    
    var wasRecordEnabled = true;
    
    if (track.getFirstWithTitle("Track Record Enable").value.value != 'on state')
    {
      wasRecordEnabled = false;
      track.getFirstWithTitle("Track Record Enable").elementClick();
    }
      
    if (track.getFirstWithTitle("Mute").value.value != 'on state')
      track.getFirstWithTitle("Mute").elementClick();
     
    if (wasRecordEnabled)
      sf.system.execAppleScript({
    
    // Notification via AppleScript and UserNotification CLI
    script: 'property usernotificationCLIPath : missing value\n' +
    'set usernotificationCLIPath to POSIX path of "/usr/local/bin/usernotification"\n' +
    
    'activate application "System Events"\n' +
    'tell application "System Events"\n' +
    '   using terms from application "Pro Tools"\n' +
    '       my notifyAsProTools("MxPrint", "Track was already record enabled", missing value)\n' +
    '   end using terms from\n' +
    'end tell\n' +
    
    'activate application "Pro Tools"\n' +
    
    'on notify(theTitle, theSubtitle, theInformativeText, theIdentifier)\n' +
    '   set notifyParameters to ""\n' +
    '   if theIdentifier is not missing value then set notifyParameters to notifyParameters & " -identifier " & theIdentifier\n' +
    '   if theTitle is not missing value then set notifyParameters to notifyParameters & " -title " & quoted form of theTitle\n' +
    '   if theSubtitle is not missing value then set notifyParameters to notifyParameters & " -subtitle " & quoted form of theSubtitle\n' +
    '   if theInformativeText is not missing value then set notifyParameters to notifyParameters & " -informativeText " & quoted form of theInformativeText\n' +
    '   do shell script quoted form of usernotificationCLIPath & notifyParameters\n' +
    'end notify\n' +
    
    'on notifyAsProTools(theTitle, theSubtitle, theInformativeText)\n' +
    '   notify(theTitle, theSubtitle, theInformativeText, "com.Avid.ProTools")\n' +
    'end notifyAsProTools'
    });
    
    else
      sf.system.execAppleScript({
    
    // Notification via AppleScript and UserNotification CLI
    script: 'property usernotificationCLIPath : missing value\n' +
    'set usernotificationCLIPath to POSIX path of "/usr/local/bin/usernotification"\n' +
    
    'activate application "System Events"\n' +
    'tell application "System Events"\n' +
    '   using terms from application "Pro Tools"\n' +
    '       my notifyAsProTools("MxPrint", "Track now record enabled", missing value)\n' +
    '   end using terms from\n' +
    'end tell\n' +
    
    'activate application "Pro Tools"\n' +
    
    'on notify(theTitle, theSubtitle, theInformativeText, theIdentifier)\n' +
    '   set notifyParameters to ""\n' +
    '   if theIdentifier is not missing value then set notifyParameters to notifyParameters & " -identifier " & theIdentifier\n' +
    '   if theTitle is not missing value then set notifyParameters to notifyParameters & " -title " & quoted form of theTitle\n' +
    '   if theSubtitle is not missing value then set notifyParameters to notifyParameters & " -subtitle " & quoted form of theSubtitle\n' +
    '   if theInformativeText is not missing value then set notifyParameters to notifyParameters & " -informativeText " & quoted form of theInformativeText\n' +
    '   do shell script quoted form of usernotificationCLIPath & notifyParameters\n' +
    'end notify\n' +
    
    'on notifyAsProTools(theTitle, theSubtitle, theInformativeText)\n' +
    '   notify(theTitle, theSubtitle, theInformativeText, "com.Avid.ProTools")\n' +
    'end notifyAsProTools'
    });
    

    Is there a way to use the variables and handlers just once?
    And yes, I am aware I can use the log function but that doesn't display the PT icon in the Notification. I tried half a dozen of things and the above code is the only method to make it work. But its cumbersome.

    Solved in post #2, click to view
    • 2 replies
    1. Hi @Oli_Em

      I would put this in as a function so you don't have to repeat the code.
      Look how I send a parameter message to the function, that I then embed in the script?

      function notifyAsPt(message)
      {
          sf.system.execAppleScript({
              // Notification via AppleScript and UserNotification CLI
              script: 'property usernotificationCLIPath : missing value\n' +
              'set usernotificationCLIPath to POSIX path of "/usr/local/bin/usernotification"\n' +
      
              'activate application "System Events"\n' +
              'tell application "System Events"\n' +
              '   using terms from application "Pro Tools"\n' +
              '       my notifyAsProTools("MxPrint", "' + message + '", missing value)\n' +
              '   end using terms from\n' +
              'end tell\n' +
      
              'activate application "Pro Tools"\n' +
      
              'on notify(theTitle, theSubtitle, theInformativeText, theIdentifier)\n' +
              '   set notifyParameters to ""\n' +
              '   if theIdentifier is not missing value then set notifyParameters to notifyParameters & " -identifier " & theIdentifier\n' +
              '   if theTitle is not missing value then set notifyParameters to notifyParameters & " -title " & quoted form of theTitle\n' +
              '   if theSubtitle is not missing value then set notifyParameters to notifyParameters & " -subtitle " & quoted form of theSubtitle\n' +
              '   if theInformativeText is not missing value then set notifyParameters to notifyParameters & " -informativeText " & quoted form of theInformativeText\n' +
              '   do shell script quoted form of usernotificationCLIPath & notifyParameters\n' +
              'end notify\n' +
      
              'on notifyAsProTools(theTitle, theSubtitle, theInformativeText)\n' +
              '   notify(theTitle, theSubtitle, theInformativeText, "com.Avid.ProTools")\n' +
              'end notifyAsProTools'
              });
      }
      
      // Record enable a print track "MxPrint" and mute it in case it isn't already muted
      var track = sf.ui.proTools.trackGetByName({ name: 'MxPrint' }).track;
      
      var wasRecordEnabled = true;
      
      if (track.getFirstWithTitle("Track Record Enable").value.value != 'on state')
      {
        wasRecordEnabled = false;
        track.getFirstWithTitle("Track Record Enable").elementClick();
      }
        
      if (track.getFirstWithTitle("Mute").value.value != 'on state')
        track.getFirstWithTitle("Mute").elementClick();
       
      if (wasRecordEnabled)
      {
          notifyAsPt('Track was already record enabled');
      }
      else
      {
          notifyAsPt('Track now record enabled');
      }
      
      Reply1 LikeSolution
      1. ?@anon6770933309
          2018-06-27 17:27:46.995Z

          Very cool, Christian. Thx so much!