Applescript to find and display 3G Modem D/U from safari Cache DB

Discussion in 'Mac Programming' started by justperry, Apr 4, 2012.

  1. macrumors 603


    Need some help here.

    A month ago I bought a TP-LINK MR3420 Wireless 3G Modem which is capable to connect to the internet by connecting a USB dongle to it.
    The Dongle is a Huawei E153 and works well.
    Before I had the TP-LINK I connected the Dongle to My laptop and used an App called SurplusMeter to Log My 3G Data usage, it's an easy program and can set both the date of the month it starts as well as how much Data is in the package, 2.5 GB in My case, after that it gets throttled.
    Now, I can see My usage in the Web based UI from the TP-Link but it's both a pain and if I switch of the router or there is a power outage it's lost and I don't know how much I used.

    But, that data (received and send) is also in Safari's data Base which is in /Users/perry/Library/Caches/
    (TP-Link status page gets updated automatically when open in Safari

    Now, if I open Cache.db in HexEdit and search for :

    var statistList = new Array(

    I can see the data as follows :

    var statistList = new Array(
    19244286, 1780152, 18004, 20168,
    0,0 );

    The four numbers are recieved-send-packets received-packets send.

    What I want is to have a script which search for var statistList = new Array( and then write the 2 numbers following it to a file which I then can use to display in Geektool and I also need it to update every 10-20 seconds.
    Preferable in Kb, then Mb if bigger than 1024 Kb and then Gb after when applicable.

    I am no good in writting a script like that, I tried many times to write small Applescripts but I always end up with nothing.
    I know for somebody familiar with Applescript it should be easy.

    Any help is greatly appreciated.
    Cheers Perry
  2. thread starter macrumors 603


    Never mind, I already wrote TP-Link about it and it's probably going to be solved by them.
  3. thread starter macrumors 603


    I found a grep command which gives me the output.

    grep -a -i -A 1 'statistList = new Array' * /Users/*/Library/Caches/

    Output is:

    /Users/*/Library/Caches/ statistList = new Array(
    /Users/*/Library/Caches/, 2199940, 16070, 18010,

    So what I need to do next is get (only) those numbers in bold to write to a file and then
    make a formula to write them into geektool.
    There is also the problem that when I switch of the router the data gets reset, I do not want that, I want the last output to be remembered and added to the new output.
    Any suggestions.

  4. kryten2, Apr 21, 2012
    Last edited: Apr 22, 2012

    macrumors 6502a

    Mar 17, 2012
    I've added a closing parenthesis to your string as your first post showed it. Change the code for your purpose :

    --set the_String to do shell script "-- your grep line here"
    -- purely as example
    --set the_String to "/Users/*/Library/Caches/ statistList = new Array(\n/Users/*/Library/Caches/, 2199940, 16070, 18010,)"
    set character_count to count characters of the_String
    set x to the offset of "-" in the_String
    set the_Numbers_from_String to text (x + 1) thru (character_count - 2) of the_String
    	set oldDelims to AppleScript's text item delimiters -- save their current state
    	set AppleScript's text item delimiters to {","} -- declare new delimiters
    	-- do script steps here
    	set these_items to the text items of the_Numbers_from_String
    	-- the result of the these_items is a list of strings:
    	-- {"14135935", " 2199940", " 16070", " 18010"}
    	set AppleScript's text item delimiters to oldDelims -- restore them
    	set these_items_integer_list to {}
    	repeat with i in these_items
    		set i to i as integer
    		set end of these_items_integer_list to (contents of i)
    		-- these_items_integer_list is a list of integers:
    		-- {14135935, 2199940, 16070, 18010}
    	end repeat
    on error
    	set AppleScript's text item delimiters to oldDelims -- restore them in case something went wrong
    end try
    For writing to a file I suggest you take a look at the StandardAdditions Library--> File Read/Write stuff.

Share This Page