Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Search on Webside, copy a line and extract information from the line to put it on a variable

Hi I'm an air traffic controller on Ivao and I have a question



I wanted to write a script that fills information that I get from the web, into a numbers sheet.



The problem is, that I am very new to applescript, so I have some ideas of how to do this...



The goal is that the script "go's" to the website http://wx.ivao.aero/metar.php, than searches for the line that contains LOWW


LOWW 271350Z 14013KT CAVOK 28/15 Q1016 NOSIG


Something like this should be copped then.


Afterwards it should extract the 5 numbers that are written in front of the KT (14013)


These 5 numbers have to be split in to 140 and 13 and saved on to 2 variables


Finally these numbers should be send to "Numbers" where they should be then put into 2 cells



Sounds easy right?😀 (or maybe it is easy but I'm to dumb😉 )



I would appreciate any Ideas...


Ben

MacBook Pro with Retina display, OS X Yosemite (10.10.5)

Posted on Aug 27, 2015 7:29 AM

Reply
Question marked as Best reply

Posted on Aug 28, 2015 2:56 PM

Hello


You may try something like this.



_main() on _main() do shell script "curl -s 'http://wx.ivao.aero/metar.php' | perl -lne 'print qq($1 $2) if /^LOWW \\S+ (...)(..)KT/'" set rr to result's words repeat with r in rr set r's contents to 0 + r end repeat set {a, b} to rr tell application "Numbers" tell document 1's sheet 1's table 1 set cell "A2"'s value to a set cell "B2"'s value to b end tell end tell end _main



Good luck,

H

17 replies
Question marked as Best reply

Aug 28, 2015 2:56 PM in response to Ben L.

Hello


You may try something like this.



_main() on _main() do shell script "curl -s 'http://wx.ivao.aero/metar.php' | perl -lne 'print qq($1 $2) if /^LOWW \\S+ (...)(..)KT/'" set rr to result's words repeat with r in rr set r's contents to 0 + r end repeat set {a, b} to rr tell application "Numbers" tell document 1's sheet 1's table 1 set cell "A2"'s value to a set cell "B2"'s value to b end tell end tell end _main



Good luck,

H

Aug 31, 2015 6:15 AM in response to Ben L.

Hello


What you need to know is regular expression pattern matching. Since this script uses Perl, you can search Google by keywords:


Perl regular expression


and will get ample examples.


Here're some fundamental documents you might explore.


perlrequick - Perl regular expressions quick start

http://perldoc.perl.org/perlrequick.html


perlretut - Perl regular expressions tutorial

http://perldoc.perl.org/perlretut.html


perlre - Perl regular expressions

http://perldoc.perl.org/perlre.html



Good luck,

H


PS. In the provided AppleScript script, the Perl script invoked via do shell script command is quoted – specifically \ is quoted as \\. Generally, two characters must be quoted in AppleScript string literal, which are " (quotation) and \ (backslash) and quoted as \" and \\ respectively. This quoting has nothing to do with Perl.


The original command line statement to run Perl script by Perl interpreter is as follows.


perl -lne 'print qq($1 $2) if /^LOWW \S+ (...)(..)KT/'



EDIT: added PS.

Aug 31, 2015 7:15 AM in response to Ben L.

Hello


To extract the 4-digit number following Q, the shell script will be like this:


#!/bin/bash # e.g., # LOWW 311320Z 15019KT CAVOK 34/10 Q1013 NOSIG curl -s 'http://wx.ivao.aero/metar.php' | perl -lne 'print qq($1) if /^LOWW \S+ \S+ \S+ \S+ Q(....)/'



And its AppleScript wrapper is like this:


_main() on _main() do shell script "curl -s 'http://wx.ivao.aero/metar.php' | perl -lne 'print qq($1) if /^LOWW \\S+ \\S+ \\S+ \\S+ Q(....)/'" set c to result --return c -- for test tell application "Numbers" tell document 1's sheet 1's table 1 set cell "C2"'s value to c end tell end tell end _main



By the way, to extract all three numbers, you may use this single shell script:


#!/bin/bash # e.g., # LOWW 311320Z 15019KT CAVOK 34/10 Q1013 NOSIG curl -s 'http://wx.ivao.aero/metar.php' | perl -lne 'print qq($1 $2 $3) if /^LOWW \S+ (...)(..)KT \S+ \S+ Q(....)/'



And its AppleScript wrapper is like this:


_main() on _main() do shell script "curl -s 'http://wx.ivao.aero/metar.php' | perl -lne 'print qq($1 $2 $3) if /^LOWW \\S+ (...)(..)KT \\S+ \\S+ Q(....)/'" set {a, b, c} to result's words --return {a, b, c} -- for test tell application "Numbers" tell document 1's sheet 1's table 1 set cell "A2"'s value to a set cell "B2"'s value to b set cell "C2"'s value to c end tell end tell end _main




* I have changed AppleScript script a little so that it now extracts values as text, for I guess these numbers are actually some id's of which leading zero's should be preserved.


Hope this may help you get the knack.

H

Aug 31, 2015 7:46 AM in response to Hiroto

Hiroto,


That Metar field for wind direction, and wind speed in knots, can also include wind gust data (e.g. 15015G25KT) where 150 is the direction, 15 is the steady wind speed, and G25 indicates that the wind is gusting to 25 knots. Though not in (LOWW) Vienna, Austria, it is possible that in some locations, the wind and gust speeds could be 3-digit values.


Currently, if that Metar field changes to gust information, your current Perl capture for that field as written, will return nothing.

Aug 31, 2015 11:42 AM in response to Ben L.

Hello Ben L.


You're quite welcome! 🙂


Now that I have looked into basics of METAR and realised that its format is variable and rather loose, here's revised script to be safer in extracting values. It will extract wind direction (ddd), wind speed (ss), gust speed (gg) and atmospheric pressure (qqqq). When there's no gust speed in record, its value is empty string.



Shell script:


#!/bin/bash # e.g., # LOWW 311320Z 15019KT CAVOK 34/10 Q1013 NOSIG # LOWW 311320Z 15019G33KT CAVOK 34/10 Q1013 NOSIG curl -s 'http://wx.ivao.aero/metar.php' | perl -lne 'print qq($1;$2;$3;$4) if /^LOWW \S+ (...)(..)(?:G(..))?KT .*? Q([0-9]{4})/'



AppleScript wrapper with additional code for Numbers:


_main() on _main() do shell script "curl -s 'http://wx.ivao.aero/metar.php' | perl -lne 'print qq($1;$2;$3;$4) if /^LOWW \\S+ (...)(..)(?:G(..))?KT .*? Q([0-9]{4})/'" set r to result set {astid0, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {";"}} set rr to r's text items set AppleScript's text item delimiters to astid0 if (count rr) < 4 then error "No entry is found." number 8000 set {ddd, ss, gg, qqqq} to rr --return {ddd, ss, gg, qqqq} -- for test tell application "Numbers" tell document 1's sheet 1's table 1 set cell "A2"'s value to ddd -- wind direction [degree] set cell "B2"'s value to ss -- wind speed [knot] set cell "C2"'s value to gg -- gust speed [knot] set cell "D2"'s value to qqqq -- atmospheric pressure [hPa] end tell end tell end _main



All the best,

Hiroto

Search on Webside, copy a line and extract information from the line to put it on a variable

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.