Saturday, November 22, 2014

#1 2013-02-20 01:34:28 pm

JES
Member
Registered: 2013-02-18
Posts: 11

converting a variable into something readable

Hi all,

I'm trying to get the size of a user's Documents folder and if it's over 10 gig, say it's over 10 gig and if its under that, life continues. 

I know I can use physical size of but I need to exclude certain file types so I'm using the du command with the -I option. 

I would like the display dialog to return a result that is a bit more user friendly then 8.23444E9 and I can't find the magic sequence.  I've tried making it as real, as integer, as Unicode Text, which BTW, would be fun but totally unreadable. 

Can anyone give me some guidance, or a better way of doing this? 

Thank you in advance!
Joy

Applescript:



set local_document_size to do shell script ("du -sI *.dmg -I *.iso -I *.img -I *.cdr -I *.pkg -I *.mpkg -I *.app -I *Microsoft* ~/Documents | cut -f1") as number


if local_document_size > 8.38E+7 then
   set backup_answer to display dialog "The size of your Documents folder is" & local_document_size & "GIGS. The backup will take awhile. Did you want to continue setting up the backup to the network?" buttons {"Yes", "No"} default button "Yes"
   if button returned of backup_answer is "Yes" then
       display alert "Cool"
   end if
end if

Offline

 

#2 2013-02-20 02:04:50 pm

mouramartins
Member
From: Algarve
Registered: 2010-11-21
Posts: 119

Re: converting a variable into something readable

Applescript:

set gigas to ((local_document_size/10000000) as integer)/100

Last edited by mouramartins (2013-02-20 03:40:42 pm)

Offline

 

#3 2013-02-20 03:59:41 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3794

Re: converting a variable into something readable

JES wrote:

I would like the display dialog to return a result that is a bit more user friendly then 8.23444E9 and I can't find the magic sequence.

It's hacky, but:

Applescript:

8.38E+7 as miles as string --> "83800000"

You can change miles to any unit AS supports.

You can also use ASAObjC Runner:

Applescript:

tell application "ASObjC Runner"
   format number 8.38E+7 format "#,###" --> "83,800,000"
end tell


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Online

 

#4 2013-02-21 06:13:25 am

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 3570

Re: converting a variable into something readable

What do you get if you don't coerce the shell script result to number? If the text is in the form you want, then you could use that instead, coercing it to number only for the comparison with 8.38E+7:

Applescript:

set local_document_size to do shell script ("du -sI *.dmg -I *.iso -I *.img -I *.cdr -I *.pkg -I *.mpkg -I *.app -I *Microsoft* ~/Documents | cut -f1")

if (local_document_size as number) > 8.38E+7 then
   set backup_answer to display dialog "The size of your Documents folder is" & local_document_size & "GIGS."
   -- etc.
end if

However, the "du" result isn't in GIgabytes but in 512-byte allocation blocks. You could set the environment variable BLOCKSIZE to your favourite interpretation of Gigabyte, or use "du"'s -g option to count in 1073741824-byte blocks, but the result would be "1" for each Gigabyte or part thereof. To convert a 512-byte-block count to Gigabytes, I reckon mouramartins's code should be:

Applescript:

set local_document_size to do shell script ("du -sI *.dg -I *.iso -I *.img -I *.cdr -I *.pkg -I *.mpkg -I *.app -I *Microsoft* ~/Documents | cut -f1")

-- "Gigabyte" = 10 ^ 9 bytes:
set gigas to ((local_document_size * 512 / 1.0E+7) as integer) / 100 -- Dividing by 1.0E+7 is equivalent to dividing by 1.0E+9 then multiplying by 100.
-- Or "Gigabyte" = 2 ^ 30 bytes:
set gigas to ((local_document_size * 512 / 1.073741824E+7) as integer) / 100 -- Simile.

Edit: Exponents corrected as per DJ Bazzie Wazzie's comment three posts down.

Incidentally, going back to coercing shell script results to number, 'do shell script' has an 'as' parameter which you don't want to use here. You need an AppleScript coercion. To get this, you have to parenthesise the 'do shell script' command so that the 'as' isn't interpreted as part of it:

Applescript:

set local_document_size to (do shell script ("du -sI *.dmg -I *.iso -I *.img -I *.cdr -I *.pkg -I *.mpkg -I *.app -I *Microsoft* ~/Documents | cut -f1")) as number

Last edited by Nigel Garvey (2013-02-21 02:33:56 pm)


NG

Offline

 

#5 2013-02-21 07:06:39 am

McUsrII
Member
Registered: 2012-11-20
Posts: 2407
Website

Re: converting a variable into something readable

Hello.

I wasn't totally sure of the new definitions of the GB so I googled it, and as one article stated. "when you are buying a 100GB drive, you get 107 (in the understanding that the vendor uses Apple's metrics for a GB)" big_smile

I think the new way of specifying a GB makes a whole lot of sense, since that is what the user sees. Even the late Brian W. Kernighan found it to be sensible, having first been baffled by the size of the music collection on his ipod.

I found a nice readup on the subject here

Last edited by McUsrII (2013-02-21 07:16:13 am)


Filed under: apple, size, GB

Offline

 

#6 2013-02-21 12:47:11 pm

JES
Member
Registered: 2013-02-18
Posts: 11

Re: converting a variable into something readable

Thank you ALL!!!  Nigel's suggestion worked!

Offline

 

#7 2013-02-21 02:05:53 pm

DJ Bazzie Wazzie
Member
From: the Netherlands
Registered: 2004-10-20
Posts: 2002

Re: converting a variable into something readable

I think that Nigels forgot the complete code ((n * 100 as integer) / 100) to get 2 decimals after the decimal mark. the values are 100 times too small.

Applescript:

set local_document_size to do shell script ("du -sI *.dg -I *.iso -I *.img -I *.cdr -I *.pkg -I *.mpkg -I *.app -I *Microsoft* ~/Documents | cut -f1")

-- "Gigabyte" = 10 ^ 9 bytes:
set gigas to ((local_document_size * 512 / 1.0E+7) as integer) / 100
-- Or "Gigabyte" = 2 ^ 30 bytes:
set gigas to ((local_document_size * 512 / 1.073741824E+7) as integer) / 100

Once you're invoking a shell you can do calculations easily with bc. I'm using run script, instead of an 'as number' coercion for languages that uses a comma as decimal separator like in my language.

Applescript:

#when a K = 1024
set local_document_size to run script (do shell script "bc <<< \"scale=3;$(du -sI *.dmg -I *.iso -I *.img -I *.cdr -I *.pkg -I *.mpkg -I *.app -I *Microsoft* ~/Documents | cut -f1) / (1024 ^ 3) * 512\" | xargs printf \"%.2f\"")
#when an K is 1000
set local_document_size to run script (do shell script "bc <<< \"scale=3;$(du -sI *.dmg -I *.iso -I *.img -I *.cdr -I *.pkg -I *.mpkg -I *.app -I *Microsoft* ~/Documents | cut -f1) * 512 / (10 ^ 9)\" | xargs printf \"%.2f\"")

p.s. The scale property in bc cuts of decimals and doesn't round the up if needed. I needed to use printf to do the rounding correctly

Last edited by DJ Bazzie Wazzie (2013-02-21 02:26:06 pm)


Kind regards

Offline

 

#8 2013-02-21 02:12:22 pm

McUsrII
Member
Registered: 2012-11-20
Posts: 2407
Website

Re: converting a variable into something readable

Hello.

I am far to tired too do the math right now, at the "Z" level, but it beats me that in order to do "Apple Metrics" you should disregard block sizes of 512k and use 500 instead in order to make the metrics work out correctly.

Because this is not just for GB's; they reckon 1K is 1000 and not 1024 as well, doing the same metrics.

Last edited by McUsrII (2013-02-21 02:15:38 pm)


Filed under: apple, GB's

Offline

 

#9 2013-02-21 02:27:06 pm

JES
Member
Registered: 2013-02-18
Posts: 11

Re: converting a variable into something readable

Wow.  I love this group!

I was thinking the math was going to be a bit complex. Way to complex for my brain, but I was also wondering why the numbers weren't adding up.

Thank you DJ Bazzie Wazzie and all who responded!  Perfect!!

Offline

 

#10 2013-02-21 02:33:35 pm

DJ Bazzie Wazzie
Member
From: the Netherlands
Registered: 2004-10-20
Posts: 2002

Re: converting a variable into something readable

McUsrII wrote:

In order to do "Apple Metrics" you should disregard block sizes of 512k and use 500 instead in order to make the metrics work out correctly.

When I get block sizes of 512 bytes I need to multiply the value with 512 to get the value in bytes first. Then you can divide it with 1024 ^ 3 or 10 ^ 9 to whatever your likings.


Kind regards

Offline

 

#11 2013-02-21 02:35:43 pm

Nigel Garvey
Moderator
From: Warwickshire, England
Registered: 2002-11-19
Posts: 3570

Re: converting a variable into something readable

DJ Bazzie Wazzie wrote:

I think that Nigels forgot the complete code ((n * 100 as integer) / 100) to get 2 decimals after the decimal mark. the values are 100 times too small.

Yes I did. Thanks. I've now corrected the offending script above.  roll


NG

Offline

 

#12 2013-02-21 04:32:05 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3794

Re: converting a variable into something readable

DJ Bazzie Wazzie wrote:

Then you can divide it with 1024 ^ 3 or 10 ^ 9 to whatever your likings.

FWIW, OS X 10.8 has an API for producing strings the way the Finder does it:


Applescript:

script convertBytes
   set theValue to current application's NSApp's convertedValue()
   return current application's NSByteCountFormatter's stringFromByteCount_countStyle_(theValue, 0) -- use 1 for 1024
end script

tell application id "au.com.myriad-com.ASObjC-Runner" -- ASObjC Runner.app
   set bytesString to run the script {convertBytes} converting 123456789 with response --> "123.5 MB"
   set bytesString to run the script {convertBytes} converting 1.23456789123E+10 with response --> "12.35 GB"
   set bytesString to run the script {convertBytes} converting 123 with response --> "123 bytes"
end tell


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Online

 

#13 2013-02-21 08:17:08 pm

McUsrII
Member
Registered: 2012-11-20
Posts: 2407
Website

Re: converting a variable into something readable

Hello.

This is my version, a stand along tiny utility for doing the task, save it as dusum.c in a directory, then type: make dusum, this works if you have Developer tools installed!

Code:

#include <stdio.h>

int main( int argc, char **argv)
{
if (argc>1) {
fprintf(stderr,"dusum usage: du |dusum\n") ;
return 1 ;
}
long double aLongDouble, theSum=0;
char digest[256];
while (!feof(stdin)) {
fscanf(stdin,"%Lf%s\n",&aLongDouble,digest );
theSum+=aLongDouble ;
}
long double gigas = (long double)(theSum*512)/1000000000 ;
fprintf(stdout,"%.2Lf\n",gigas);
return 0;
}

When dusum resides either in a path or with a path specified in front of it, you should be able to amened it at the end of the du statement, and get a result formatted as GB's with two decimals back.

Edit
I couldn't sleep as it nagged me that I may have had used a to small container for the number of blocks in a file, testing it originally in a folder where it was made. It is supposed to work properly now.

If not for anything else, this should serve as a demonstration of how quick it is to create a small c-program, once you have the tools installed.

C-programs like this are far faster to create if you know your way around with the scanf function (man -s3 scanf) and printf (man -s3 printf), than anything else. That it: when it comes to summing up stuff from standard input, where you would use bc or awk to perform the same. The important thing is to digest the line, so that you go on to the next one, which is the purpose of digest above: It reads the filename up to the newline, so that we go on to reading the number on the next line.

You gotta love Apple for making it so easy, when you have the tools installed! smile

This way isn't the most portable, but you may just fire up an editor, and make the executable, and later on, just drop it inside a script bundle and take it from there. And it is way faster to make the solution, than figure it out with bc, at least for me, I really deem this to be a to little job for awk, but it should be as fast to create as the C-version above:

Code:

#!/usr/bin/awk -f
{
a+=$1
}
END {
printf("%.2f\n",((a * 512 ) /1000000000))
}

You must make it executable with chmod +u dusum.awk, and either put it in a folder that is in your path, or specify the full path for it in the pipe where it comes right after the "du" statement.

Last edited by McUsrII (2013-02-21 10:27:43 pm)


Filed under: du

Offline

 

#14 2013-02-22 06:04:25 am

DJ Bazzie Wazzie
Member
From: the Netherlands
Registered: 2004-10-20
Posts: 2002

Re: converting a variable into something readable

Shane Stanley wrote:

DJ Bazzie Wazzie wrote:

Then you can divide it with 1024 ^ 3 or 10 ^ 9 to whatever your likings

FWIW, OS X 10.8 has an API for producing strings the way the Finder does it:

Thanks for the heads up, I didn't know that. I'm working mostly on 10.6.8 at the moment while I have the most fastest MBP here next to me with 10.8 which gets covered with a layer of dust neutral


Kind regards

Offline

 

#15 2013-02-22 06:19:54 pm

Shane Stanley
Member
From: Australia
Registered: 2002-12-07
Posts: 3794

Re: converting a variable into something readable

DJ Bazzie Wazzie wrote:

I have the most fastest MBP here next to me with 10.8 which gets covered with a layer of dust neutral

I'll be over there in a month or so; I can take it off your hands and free up some desk space smile


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Online

 

Board footer

Powered by FluxBB

[ Generated in 0.031 seconds, 11 queries executed ]

RSS (new topics) RSS (active topics)