Trimming Variables

Hi Folks,

I am looking for two ways to trim Variables:

The first one:

variable_test:

+CPIN: READY

OK

In this case I am looking for READY or a Number (which if available replaces the READY)

Working with


set pin_show to characters 1 through 30 of pin_show as string

Will not work in this case because I can not garantuee that READY or the 4Digit Number will be always on this position

The next one is also very tricky

variable_test1

+CSQ: 24.99

OK

In this Case I am looking for the first 2 numbers (24 in this case) - this should also be done dynamically!

Question:

Is it possible to trim the two variables? Can someone give me some hints? I will try to fix it myself so any suggestions are welcomed!

Thanks and best regards,

Stefan

Hi Stefan (or Stefani?),

To be able to trim your variable and extract the text items you want, you’ll have to make use of AppleScript’s text item delimiter. There must be many examples in the other forum on AppleScript and OS X. I suggest that you do a search there.

Meanwhile, just to give you an idea, AppleScript’s default text item delimiter is " " (or space). You can replace it with “:” to separate your text items to “CPIN” and “READY” in your first example. Then, all you have to do is get the second text item. Unfortunately, if the position of READY or some other item that you want changes, then obviously you have to have a way to tag that item so that you can get it cleanly.

For your second example, you may have trim twice to extract the value “24”. In the second trimming, you may use “.” as your delimiter.

After using your “:” and “.” as text item delimiters, make sure to restore AppleScript’s default TID (text item delimiter, as it is often referred to in scripts). Else, your script will continue to use your personal text item delimiters and could spoil some parts of your script that also use TID.

OK, I hope this starts you up and going.

Good luck.

archseed :slight_smile:

Hi Archseed,

thanks - now I know what to look for…

Thanks again for your help!

Stefan