• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: maths
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: maths


  • Subject: Re: maths
  • From: Christopher Stone <email@hidden>
  • Date: Wed, 18 Apr 2012 18:01:27 -0500

On Apr 18, 2012, at 15:29, Javi Pérez wrote:
Well, it worked in this way:
______________________________________________________________________

Hey Javi,

You don't have control of what 'Words' means in Applescript, and sometimes it will give unexpected results.  For instance - during several test runs of your first posted script "word 29" sometimes returned "SharedLibs" on my system (Lion).

Text Item Delimiters allow you a bit more precision:

set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, {": ", "% ", ", "}}
tell paragraph 4 of (do shell script "top -n1 -l1") to set totalUsage to 100 - (its text items's item 6)
set AppleScript's text item delimiters to oldTIDS
totalUsage

--> 27.78

You could also do most of the work in the shell, since you're going there anyway.

set totalUsage to 100 - (do shell script "top -n1 -l1 | sed -En '/^CPU.+/p' | sed -E 's/.+sys[^0-9]+([0-9.]+).+/\\1/'")

--> 31.58

My preferred tool for parsing in Applescript is the Satimage.osax.  It makes regular _expression_ find, find/replace, and a slew of other useful functions directly available to Applescript.

set topReport to do shell script "top -n1 -l1"
set totalUsage to 100 - (find text "^.+?([0-9.]+)%\\s+idle" in topReport using "\\1" with regexp and string result)

--> 26.32

Note that on Lion all the strings coerce to number without incident (and this should be true of most if not all of its antecedents).

On Apr 18, 2012, at 16:30, Javi Pérez wrote:
Spanish uses "," as decimal. And yes probably I could need change my system language to english, it gives me headaches sometimes

Ah.  Change the period decimal point to a comma as needed in the above.  Note that '.' is a token for any single character in regex, so you need only change an escaped period '\.' or in Applescript the backslash must itself be escaped '\\.'.

Since Shane Stanley has released ASObjC Runner.app, I think it worth mentioning for people unwilling to install an OSAX.  It has a nice Applescript Dictionary and is useful for those who want to start bridging Applescript and ObjC without requiring an applet.

RegEx find with capture:

set topReport to do shell script "top -n1 -l1"
tell application "ASObjC Runner"
set totalUsage to 100 - (look for "^.+?([0-9.]+)%\\s+(idle)" in topReport capture 1 options "im")
end tell

--> 33.34

--
Best Regards,
Chris

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >maths (From: Javi Pérez <email@hidden>)
 >Re: maths (From: Alex Zavatone <email@hidden>)
 >RE: maths (From: Javi Pérez <email@hidden>)
 >Re: maths (From: Alex Zavatone <email@hidden>)
 >RE: maths (From: Javi Pérez <email@hidden>)

  • Prev by Date: Re: maths
  • Next by Date: Re: Run Handler-Name from Text?
  • Previous by thread: Re: maths
  • Next by thread: Re: maths
  • Index(es):
    • Date
    • Thread