AppleScript to remove brackets and the text within

Posted on
Sun Jan 17, 2016 5:18 am
Andy_Wismer offline
Posts: 7
Joined: Mar 02, 2015

AppleScript to remove brackets and the text within

Hello

I'm new to AppleScript and I'm trying to remove any brackets and text within from a string saved as a variable.
I need this in a Trigger that fills the value in a second variable, but without the brackets.

This is for ForeCastIO, and im trying to use images to display the weather.
Works well so far, except for a few exceptions, like when something like this comes:

"Snow (Under 1 cm.)"

Thats the present Value, but I only need "Snow" in there.

Perl I could do it, but how's the best way to use AppleScript for this?

Thanks

Andy

Posted on
Sun Jan 17, 2016 8:11 am
kw123 offline
User avatar
Posts: 8363
Joined: May 12, 2013
Location: Dallas, TX

Re: AppleScript to remove brackets and the text within

I strongly recommend to switch to python ! String manipulations in applescript are always difficult

in python it is:
a="Snow (Under 1 cm.)"
a=a.replace("("," ").replace(")"," ")

in applescript:

set the a to "Snow (Under 1 cm.)"
set the a to replace_chars(a, "(", " ")
set the a to replace_chars(a, "(", " ")

on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars


Karl

adapted from
http://www.macosxautomation.com/applesc ... rt-06.html

Posted on
Sun Jan 17, 2016 8:36 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: AppleScript to remove brackets and the text within

That Python will only remove the brackets, leaving "Snow under 1cm "

I agree Python is the right way though.

The pseudocode would be the following but I don't know the Python equivalent:

Code: Select all
a = a.substr(0,search(a,"(")-1)



Sent from my iPhone using Tapatalk

Posted on
Sun Jan 17, 2016 8:42 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: AppleScript to remove brackets and the text within

I think it's:

Code: Select all
if ("(" in a):
    b = a.index("(") -1
    a = a[0:b]



Sent from my iPhone using Tapatalk

Posted on
Sun Jan 17, 2016 8:54 am
Bollar offline
Posts: 528
Joined: Aug 11, 2013

Re: AppleScript to remove brackets and the text within

If you truly want just the first word, here's another way to do it:

Code: Select all
import re

p = re.split("\s", a, maxsplit=1)
forecast = str(p[0])
info = str(p[1])


This uses a regular expression match to split the initial string into two parts. The split happens at the first "whitespace character" that's what "\s" does -- you could easily use just " " for a true ASCII space. p[0] returns that first word "snow" and p[1] returns everything thereafter -- "(Under 1 cm.)" in your example. The maxsplit=1 modifier is the secret to parsing the data always into just two values.

Insteon / Z-Wave / Bryant Evolution Connex /Tesla / Roomba / Elk M1 / SiteSage / Enphase Enlighten / NOAA Alerts

Posted on
Sun Jan 17, 2016 8:58 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: AppleScript to remove brackets and the text within

Bollar, can this work if the forecast is say "Partly Cloudy (under 1cm)"?

(Obviously a made up example!)


Sent from my iPhone using Tapatalk

Posted on
Sun Jan 17, 2016 9:07 am
Bollar offline
Posts: 528
Joined: Aug 11, 2013

Re: AppleScript to remove brackets and the text within

howartp wrote:
Bollar, can this work if the forecast is say "Partly Cloudy (under 1cm)"?

(Obviously a made up example!)

No, it will split at the space, so you would have p[0] = "Partly" and p[1] = "Cloudy (under 1cm)" I don't know which of the variables Andy is calling, but perhaps a different regex expression would work. Or, call a different data point object from forecast IO!

icon: A machine-readable text summary of this data point, suitable for selecting an icon for display. If defined, this property will have one of the following values: clear-day, clear-night, rain, snow, sleet, wind, fog, cloudy, partly-cloudy-day, or partly-cloudy-night. (Developers should ensure that a sensible default is defined, as additional values, such as hail, thunderstorm, or tornado, may be defined in the future.)

Insteon / Z-Wave / Bryant Evolution Connex /Tesla / Roomba / Elk M1 / SiteSage / Enphase Enlighten / NOAA Alerts

Posted on
Sun Jan 17, 2016 9:14 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: AppleScript to remove brackets and the text within

This sounds like a job for regular expressions - if you come from Perl then you probably have some idea of how those work.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sun Jan 17, 2016 11:25 am
SpencerJRoberts offline
User avatar
Posts: 256
Joined: Dec 09, 2012
Location: Mountain View, CA

Re: AppleScript to remove brackets and the text within

This website is awesome when you don't want to have to think about regular expressions: http://txt2re.com/
Here is your example case: http://txt2re.com/index-python.php3?s=Partly%20Cloudy%20(under%201cm)&1

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 21 guests