Saturday, December 3, 2022

#26 2022-08-06 12:07:32 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2589

Re: Convert the Text to Record (effectively)

@peavine,

I tested your latest script. It runs for me in 9 ms, that is, it is 3 times slower than a JXA script. I think it's because of the use of a repeat loop, and the AsObjC solution can only get faster than the JXA solution with the correct Regex expression applying, without repeat loops.


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 15.6.1
Ram: 4 GB

Offline

 

#27 2022-08-06 04:39:12 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 1093

Re: Convert the Text to Record (effectively)

It's unfortunate that the results from the airport command are so sloppy and haphazard. Multiple lines randomly begin with spaces, and a single line ends with multiple; the need for correction lends itself to less than optimal efficiency—no matter the method. Since my initial attempt eliminated internal spaces, I played around with a regex and married it to CK's initial approach. It may or may not be faster, but it's brief.  smile

Applescript:


({«class usrf»:(do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | egrep -o '\\w[^:]+|:.+' | awk '{gsub(\" +$|: \",\"\"); print} ' ")'s paragraphs})'s contents as anything

Last edited by Marc Anthony (2022-08-06 04:47:32 pm)

Offline

 

#28 2022-08-06 11:06:11 pm

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2589

Re: Convert the Text to Record (effectively)

Marc Anthony wrote:

It's unfortunate that the results from the airport command are so sloppy and haphazard.

Applescript:


({«class usrf»:(do shell script "/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | egrep -o '\\w[^:]+|:.+' | awk '{gsub(\" +$|: \",\"\"); print} ' ")'s paragraphs})'s contents as anything


It's not just the airport utility and its sloppy and haphazard result, but the fact that many other utilities produce a similar key-value structure. Therefore, I wanted to make sure which approach is the most effective in such cases. Some utilities has filtering options for its output, but still doesn't return it as record. In this topic, I made sure that using a regular expression is the most effective solution.

I tested your last script and I can confirm that it works instantly (2 ms), i.e. 1 ms faster than the JXA solution.

Thanks, this will come in handy.

Last edited by KniazidisR (2022-08-06 11:12:13 pm)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 15.6.1
Ram: 4 GB

Offline

 

#29 2022-08-07 01:08:58 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5588

Re: Convert the Text to Record (effectively)

Here's an ASObjC solution using regex. It renders number values in the final record as numbers rather than as text.

Applescript:

use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions

set WifiStats to "        agrCtlRSSI: -29
       agrExtRSSI: 0
       agrCtlNoise: -87
       agrExtNoise: 0
       state: running
       op mode: station
       lastTxRate: 59
       maxRate: 72
       lastAssocStatus: 0
       802.11 auth: open
       link auth: wpa2-psk
       BSSID: 8:78:8:0:aa:5c
       SSID: AndroidAP
       MCS: 6
channel: 6"


set regex to current application's NSRegularExpressionSearch
tell (current application's class "NSMutableString"'s stringWithString:(WifiStats))
   -- Trim leading and trailing white space and empty lines.
   its replaceOccurrencesOfString:("(?m)^\\s++|\\h++$|\\s++$") withString:("") options:(regex) range:({0, its |length|()})
   -- Enbar labels and enquote values.
   its replaceOccurrencesOfString:("(?m)^([^:]++):\\h++(.++)") withString:("|$1|: \"$2\"") options:(regex) range:({0, its |length|()})
   -- Unenquote number values.
   its replaceOccurrencesOfString:("\"([0-9.-]++)\"") withString:("$1") options:(regex) range:({0, its |length|()})
   -- Replace internal line endings with commas.
   its replaceOccurrencesOfString:("\\R++") withString:(", ") options:(regex) range:({0, its |length|()})
   -- Concatenate between AS text braces (thereby also coercing to AS text) and run as script code.
   return my (run script ("{" & it & "}"))
end tell

Or perhaps more coolly presented:  smile

Applescript:

use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions

set WifiStats to "        agrCtlRSSI: -29
       agrExtRSSI: 0
       agrCtlNoise: -87
       agrExtNoise: 0
       state: running
       op mode: station
       lastTxRate: 59
       maxRate: 72
       lastAssocStatus: 0
       802.11 auth: open
       link auth: wpa2-psk
       BSSID: 8:78:8:0:aa:5c
       SSID: AndroidAP
       MCS: 6
channel: 6"


set regex to current application's NSRegularExpressionSearch
set patternsAndReplacements to
   {"(?# Trim leading and trailing white space and empty lines.)(?m)^\\s++|\\h++$|\\s++$", ""}, ¬
   {"(?# Enbar labels and enquote values.)(?m)^([^:]++):\\h++(.++)", "|$1|: \"$2\""}, ¬
   {"(?# Unenquote number values.)\"([0-9.-]++)\"", "$1"}, ¬
   {"(?# Replace internal line endings with commas.)\\R++", ", "} ¬
       }
tell (current application's class "NSMutableString"'s stringWithString:(WifiStats))
   repeat with this in patternsAndReplacements
       (its replaceOccurrencesOfString:(this's beginning) withString:(this's end) options:(regex) range:({0, its |length|()}))
   end repeat
   return my (run script ("{" & it & "}"))
end tell

Last edited by Nigel Garvey (2022-08-07 03:06:20 am)


NG

Offline

 

#30 2022-08-07 02:05:40 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2589

Re: Convert the Text to Record (effectively)

Nigel Garvey wrote:

Here's an ASObjC solution using regex. It renders number values in the final record as numbers rather than as text.


I can confirm: it is very fast (3ms) + returns numbers in numbers format smile

Last edited by KniazidisR (2022-08-07 02:05:54 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 15.6.1
Ram: 4 GB

Offline

 

#31 2022-08-07 07:19:57 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1517

Re: Convert the Text to Record (effectively)

KniazidisR wrote:

@peavine,

I tested your latest script. It runs for me in 9 ms, that is, it is 3 times slower than a JXA script. I think it's because of the use of a repeat loop, and the AsObjC solution can only get faster than the JXA solution with the correct Regex expression applying, without repeat loops.



KniazidisR wrote:

I can confirm: it is very fast (3ms) + returns numbers in numbers format smile



I ran timing tests to verify the above. My computer is a 4-year-old Mac mini running Monterey and is neither fast nor slow by current standards. I used Script Geek and the results are in milliseconds:

SCRIPT - 1 ITERATION - 10 ITERATIONS

CK (JXA) - 4 - 45

peavine (ASOBJ) - 4 - 41

Nigel (first ASOBJC) - 2 - 10

IMO, these timing results are sufficiently close that other factors should probably determine which is used.


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#32 2022-08-08 12:36:26 am

CK
Member
From:: UK
Registered: 2018-11-04
Posts: 176

Re: Convert the Text to Record (effectively)

Nigel Garvey wrote:

They've not been mentioned in any edition of the AppleScript Language Guide for at least 25 years. So either the author(s) have been very forgetful or the AppleScript team hasn't intended square brackets to be an official part of the language. The latter explanation's by far the more likely.



You yourself posted the excerpt below entitled From notes accompanying AS version 1.1, which proves (in the words of someone who clearly was an engineer on the team that developed AppleScript) that they are, and therefore still remain, a part of AppleScript.

Nigel Garvey wrote:

under a different name



I only post using my own name.

Nigel Garvey wrote:

It's the permanency of your stubbornness rather than that of square brackets that's being demonstrated.



Nigel, this is not acceptable.  I already had to ask another user to refrain from targeting personal comments at me (or anyone else), but that I now need to ask that this same code of conduct be adhered to by a moderator is disgraceful.

Please do not throw personal insults at me (or other users).  I understand that, for whatever reason, my disagreeing with you appears to upset you.  But this is not ok.


Nigel Garvey wrote:

This isn't some law I've made up myself. It's a philosophy passed on by the many professional coders with whom I've crossed swords over the past quarter century. Likewise with not using reserved terms (with a few exceptions) as labels for script and user record properties. But as I noted above, your convoluted script seems to depend on the labels not being user ones.



I'm familiar with the philosophy.  I began my programming career somewhat more than a quarter of a century ago.  I have explained it in previous posts, which I seem to recall you also took issue with. 

And, no, my convoluted script doesn't depend on them.


Nigel Garvey wrote:

Not being casual or a smartarse



Again, not acceptable.  I get it, you don't like me.  I don't care.  Find a better way to compose yourself or leave me alone.

I am aware I have certain communication issues that cause me to be interpreted as blunt, rude, or "a smartarse".  I am autistic, and this is something I continually work to improve, but it's also not something I easily get right.  I'm fine with criticisms being made about how I write in way that's designed to be helpful and provide a way for me to learn, but you don't know me as a person, and you don't have the right to label me however you see fit just because you're in a bad mood.

Am I being clear about this ?


Nigel Garvey wrote:

I in turn would encourage you to read MacScripter's forum rules, which were also not made up by me. Rule 2 states: "You will be required to enter your legal first and last name when you register." The first and last name shown in your profile don't conform to this requirement and I'd encourage you to correct them before Mark notices.  smile



So it seems that when a moderator doesn't agree with one of the members, the thing to do is to try and attack them in an oblique fashion ?  My name was Christofer, but it was changed long ago (by deed poll) to CK.  There are some social media sites where this name is too short to be accepted, and I've been forced to retain my old name.  I also use my old name in some professional contexts.  But, frankly, none of this is any of your business, and I'm confident Mark does not behave in the manner that I'm seeing here.

Offline

 

#33 2022-08-08 05:18:06 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5588

Re: Convert the Text to Record (effectively)

CK wrote:

And, no, my convoluted script doesn't depend on them.


No. You're right. They're completely unnecessary. I worked that out when I finally got round to de-obfuscating the script properly. (See post #24.)

Again, not acceptable.  I get it, you don't like me.  I don't care.  Find a better way to compose yourself or leave me alone.


As the moderator, it's not for me to like or dislike anyone on this forum, any more than it is for you to tell me how to moderate it. That I do dislike.

My main concerns when moderating include:

• People asking for help here should be given accurate information that they can both use in the immediate context of their enquiry and understand well enough to be able use or adapt for use elsewhere if need be. Similarly, people reading posts in order to increase their AppleScript knowledge shouldn't be handed stuff that might lead them astray later. This isn't always possible, of course, but it should be the ambition. Obfuscated and uncommented scripts unnecessarily employing hacks and obsolete, long-undocumented features don't come anywhere near achieving this.

• Posters with obvious talent and/or enthusiasm for AppleScript and MacScripter should be encouraged and given the best possible guidance — hopefully without stifling their own creativity. They should also be able to enjoy visiting and contributing to MacScripter without fear of their contributions being mocked (although constructive criticism is of course always welcome) or of being abused themselves. (See Posting Guidelines.) The spectacle of occasional visitors insisting that black is white, condescendingly referring to people's comfort zones, and then laying down the law themselves about what is or is not acceptable is something we could all do without.

• MacScripter's very few Rules and Posting Guidlines apply to everyone. I didn't write them. I've just been landed with the job of moderating. If I see rules apparently being flouted by someone who's also being difficult in the fora, it very much is my business.

Matters that simply boil down to common sense, such as not cluttering the fora with unnecessary replies to queries posted many years before, are, according to my brief, decided at my own discretion.

If you have issues with any of this, refer them to Mark. I'll refer this topic to him myself so that he can remove my moderator status should he deem it necessary. Meanwhile this topic is now locked.


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)