Hello,
From the Terminal the command:
sysctl vm.swapusage | awk '{print $7}'
returns: 7,00M
but from a script,
do shell script "sysctl vm.swapusage | awk '{print $7}'"
returns: 7.00M
Why? How to keep the comma?
Thanks.
Hello,
From the Terminal the command:
sysctl vm.swapusage | awk '{print $7}'
returns: 7,00M
but from a script,
do shell script "sysctl vm.swapusage | awk '{print $7}'"
returns: 7.00M
Why? How to keep the comma?
Thanks.
The short answer to “why” is that do shell script inherits a different environment. The longer answer is here:
developer.apple.com/library/content/technotes/tn2065/_index.html
Thanks Shane. I knew this file but I don’t found the solution inside.
but I found that:
set swapusageused to do shell script "sysctl vm.swapusage | awk '{print $7}'"
convertPointToComma(swapusageused)
on convertPointToComma(numberAsText)
set o to offset of "." in numberAsText
if o = 0 then return numberAsText
tell numberAsText to return text 1 thru (o - 1) & "," & text (o + 1) thru -1
end convertPointToComma
Or you could tell the command to respect your locale:
do shell script "LANG=fr_FR sysctl vm.swapusage | awk '{print $7}'"
A crude way to use the decimal point character set on the user’s machine:
set swapusageused to do shell script "sysctl vm.swapusage | awk '{print $7}' | sed 'y/0.0/" & 0.0 & "/'"
An awk expert would no doubt do the sed bit with awk too. The entire edit with sed might look like this:
set swapusageused to do shell script "sysctl vm.swapusage | sed -E 's/^([^ ]+ +){6}| .*$//g ; y/0.0/" & 0.0 & "/'"
Edit: Code shortened.
Maybe Joel may use :
use AppleScript version "2.4" # requires at least Yosemite
use scripting additions
use framework "Foundation"
set thisLocale to current application's NSLocale's currentLocale()
set theLocalIdentifier to thisLocale's localeIdentifier as text --> "fr_FR"
do shell script "LANG=" & theLocalIdentifier & " sysctl vm.swapusage | awk '{print $7}'"
Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) samedi 25 mars 2017 11:32:56
Thank you all!!
Hello,
For older System, I use this, which runs with any decimal separator
set decimSep to character 2 of ((1 / 2) as text)
set swapUsage to do shell script " sysctl vm.swapusage | awk '{print $7}'"
set swapUsage to do shell script ("echo " & swapUsage & " | tr '.' '" & decimSep & "'")