Parsing a comma delimited string

Posted on
Sat Jun 09, 2012 4:03 pm
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Parsing a comma delimited string

How would I parse these strings returned from my AV switch?

!S,20,5,1,24,24

!S,1,5,1,1,24,24,5,8

They are two variations on the same info, with the second giving bass and treble info. As separated by the commas:

!S is the beginning of the response, and triggers the parser

1 or 20 is the type of response - not important

5 represents the zone

1 represents the source: in the first string there is one "1" and is the audio source, in the second string there is another "1" representing a video source, which is extraneous.

24 and 24 are volume right and left in both strings

5 is bass

8 is treble

I came up with the following but i don't think I'm handling the commas right. Also, when the volume is a single digit the script wouldn't work anyway. Somehow I think I need to use text delimiters and/or set the response to a list then get the items from the list?

Any thoughts would be appreciated.
Code: Select all
using terms from application "IndigoServer"
   if value of variable "vauxResponse" contains "!s" then
      set vauxResponseString to value of variable "vauxResponse" as string
      log vauxResponseString
      set vauxZoneNumber to (number 7 of vauxResponseString as number)
      log vauxZoneNumber --as string
      
      set vauxSourceNumber to (number 9 of vauxResponseString as number)
      log vauxSourceNumber
      set vauxVolumeLevel to (numbers 11 through 12 of vauxResponseString as number)
      log vauxVolumeLevel
   end if
   
   try
      if vauxZoneNumber is ("1" or "2" or "3") then set vauxZoneName to "Kitchen"
      
      if vauxZoneNumber is 4 then set value of variable "vauxZoneName" to "LivRmDnRm" as string
      if vauxZoneNumber is 5 then set value of "vauxZoneName" to "Game Room" as string
      if vauxZoneNumber is 6 then set value of "vauxZoneName" to "Patio" as string
      if vauxZoneNumber is 7 then set value of "vauxZoneName" to "Pool" as string
      if vauxZoneNumber is 8 then set value of "vauxZoneName" to "MBR" as string
   end try
   try
      if vauxSourceNumber is 0 then set value of "vauxSourceName" to "Off" as string
      if vauxSourceNumber is 1 then set value of "vauxSourceName" to "Tuner" as string
      if vauxSourceNumber is 2 then set value of "vauxSourceName" to "iTunesPan" as string
      if vauxSourceNumber is 3 then set value of "vauxSourceName" to "iPodPool" as string
      if vauxSourceNumber is 4 then set value of "vauxSourceName" to "PoolTuner" as string
      if vauxSourceNumber is 5 then set value of "vauxSourceName" to "Cable" as string
   end try
   log ((value of variable "vauxZoneName" as string) & (value of variable "vauxSourceName" as string))
end using terms from

Posted on
Sun Jun 10, 2012 10:15 am
hamw offline
Posts: 1212
Joined: Mar 31, 2008

Re: Parsing a comma delimited string

OK, got it fixed after finding this thread:
http://macscripter.net/viewtopic.php?id=12962

If anyone has any ideas as to how this could be done better, I'd be all ears..... Also, how would I get it to log the value of myList? It keeps throwing errors.

note: edited to add bass and treble info

Code: Select all
using terms from application "IndigoServer"
   
   
   if value of variable "vauxResponse" contains "!s" then
      
      set vauxResponseString to value of variable "vauxResponse" as text
      log vauxResponseString
      set {myTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {","}}
      set myList to text items of vauxResponseString
      
      set AppleScript's text item delimiters to myTID -- It's considered good practice to return the TID's to their original state
      --log list of myList
      --if myList contains less than 5 items then
      stop
   end if
   
   
   try
      if item 2 of myList is "40" then
         set vauxZoneNumber to item 3 of myList
         
         
         set vauxBassSetting to item 4 of myList
         
         set vauxTrebleSetting to item 5 of myList as string
         
         log ((vauxZoneNumber as string) & "  " & (vauxBassSetting as string) & "  " & (vauxTrebleSetting as string))
         
         if vauxZoneNumber is "0" then set value of variable "vauxZoneName" to "Off" as text
         if vauxZoneNumber is "1" then set value of variable "vauxZoneName" to "Kitchen" as text
         if vauxZoneNumber is "2" then set value of variable "vauxZoneName" to "FamRm" as text
         if vauxZoneNumber is "3" then set value of variable "vauxZoneName" to "Study" as text
         
         if vauxZoneNumber is "4" then set value of variable "vauxZoneName" to "LivRmDnRm" as text
         if vauxZoneNumber is "5" then set value of variable "vauxZoneName" to "GameRm" as text
         if vauxZoneNumber is "6" then set value of variable "vauxZoneName" to "Patio" as text
         if vauxZoneNumber is "7" then set value of variable "vauxZoneName" to "Pool" as text
         if vauxZoneNumber is "8" then set value of variable "vauxZoneName" to "MBR" as text
         
         
         set value of variable "vauxResponseBassTreble" to ((value of variable "vauxZoneName" as string) & "  Bass " & (vauxBassSetting as string) & "  Treble " & (vauxTrebleSetting as string))
         log value of variable "vauxResponseBassTreble" as string
      end if
      
   end try
   
   try
      
      if item 2 of myList is "1" then
         set vauxZoneNumber to item 3 of myList
         log vauxZoneNumber --as string
         
         set vauxSourceNumber to item 5 of myList
         log vauxSourceNumber
         set vauxVolumeLevel to item 6 of myList as string
         log vauxVolumeLevel
      end if
      
      
      if item 2 of myList is "3" then
         set vauxZoneNumber to item 3 of myList
         log vauxZoneNumber --as string
         
         set vauxSourceNumber to item 5 of myList
         log vauxSourceNumber
         set vauxVolumeLevel to item 6 of myList --as string
         log vauxVolumeLevel
      end if
      
      if item 2 of myList is "20" then
         set vauxZoneNumber to item 3 of myList
         log vauxZoneNumber --as string
         
         --set vauxSourceNumber to item 4 of myList
         log vauxSourceNumber
         set vauxVolumeLevel to item 5 of myList --as string
         log vauxVolumeLevel
      end if
      
      if vauxZoneNumber is "0" then set value of variable "vauxZoneName" to "Off " as text
      if vauxZoneNumber is "1" then set value of variable "vauxZoneName" to "Kitchen(K)" as text
      if vauxZoneNumber is "2" then set value of variable "vauxZoneName" to "Kitchen(F)" as text
      if vauxZoneNumber is "3" then set value of variable "vauxZoneName" to "Kitchen(S)" as text
      
      if vauxZoneNumber is "4" then set value of variable "vauxZoneName" to "LivRmDnRm" as text
      if vauxZoneNumber is "5" then set value of variable "vauxZoneName" to "GameRm" as text
      if vauxZoneNumber is "6" then set value of variable "vauxZoneName" to "Patio" as text
      if vauxZoneNumber is "7" then set value of variable "vauxZoneName" to "Pool" as text
      if vauxZoneNumber is "8" then set value of variable "vauxZoneName" to "MBR " as text
      
      if vauxSourceNumber is "0" then set value of variable "vauxSourceName" to "Off " as text
      if vauxSourceNumber is "1" then set value of variable "vauxSourceName" to "Tuner" as text
      if vauxSourceNumber is "2" then set value of variable "vauxSourceName" to "iTunesPan" as text
      if vauxSourceNumber is "3" then set value of variable "vauxSourceName" to "iPodPool" as text
      if vauxSourceNumber is "4" then set value of variable "vauxSourceName" to "PoolTuner" as text
      if vauxSourceNumber is "5" then set value of variable "vauxSourceName" to "Cable" as text
      if vauxSourceNumber is "8" then set value of variable "vauxSourceName" to "Tone Levels" as text
      
      set value of variable "vauxZoneVolume" to vauxVolumeLevel
      
      
      set value of variable "vauxResponseCombinedOutput" to ((value of variable "vauxZoneName" as string) & "  " & (value of variable "vauxSourceName" as string) & "  " & (value of variable "vauxZoneVolume" as string))
      log value of variable "vauxResponseCombinedOutput" as string
      
   end try

--the short zone name is used on the opening control page to see what's happening at a glance
   set vauxShortZone to value of variable "vauxZoneName" as string
   set vauxShortZoneName to (characters 1 through 4 of vauxShortZone) as string
   
   set value of variable "vauxResponseOutputShort" to ((characters 1 through 4 of vauxShortZoneName as string) & " " & (characters 1 through 4 of (value of variable "vauxSourceName" as string)) & " " & (value of variable "vauxZoneVolume" as string)) as string
   
   log value of variable "vauxResponseOutputShort" as string
   
   
end using terms from




Here's an example:

Code: Select all
10Jun, 2012 12:13:46 PM
  Trigger                         audio_8_source Python
  Simple Serial Plugin            Response from serial device (text): , of length 0 characters
  Trigger                         Vaux_Response
  Trigger                         Vaux_Response Parser
  Script                          MBR  Off  0
  Simple Serial Plugin            Response from serial device (text): !S,1,8,1,1,28,28, of length 16 characters
  Trigger                         Vaux_Response
  Trigger                         Vaux_Response Parser
  Script                          !S,1,8,1,1,28,28
  Script                          8
  Script                          1
  Script                          28
  Script                          MBR  Tuner  28

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 18 guests

cron