Jump to content

Need help with geektool scripting


  • Please log in to reply
8 replies to this topic

#1
harmc0re

harmc0re

    InsanelyMac Protégé

  • Members
  • PipPip
  • 81 posts
  • Gender:Male
Hello,

Yesterday I installed GeekTool, which posts the output of .bash scripts to the desktop,
so you can put date, cpu usage, weather and other useful info on the desktop.

So, using google, my Java and my very basic C++ knowledge, I wrote two scripts so far, here's my current output:

Posted Image

Using this script for the bigger letters:
Attached File  GeekTool_Time_Script.bash.txt   1.33KB   18 downloads
and this script for the smaller letters:
Attached File  GeekTool_System_Stats_Script.bash.txt   607bytes   10 downloads

I want to have additional info right on the desktop, but I need some help from you guys.
I will be searching further in the meantime, but will be more than happy if some people could help me solve some individual questions.

What I want to accomplish:

1) my name capitalized (I know it's easier to just put "Harm" there, but I want the script to be universal) Done, thanks ruger42!
2) remove the first zero and the space in 09:05 pm. Done, thanks ruger42!
3) I want the full date in English (so, not my native language), see lower Done, thanks ruger42!
4) the weather, without capital letters, see lower Done, thanks ruger42!
5) a counter for my emails, see lower Still looking for a Hotmail counter
6) other system info Still looking for a Battery meter

So this is what I would like to see on my display

Good evening Harm,
it's now 9:05pm.
Today's Saturday the 6th of August 2011.

It's 14 degrees outside and the weather is mostly cloudy.
You have 34 new emails, 34 in Hotmail and 0 in Gmail.


RAM: 20%
CPU: 8%
Battery: 100%


Ethernet: 192.168.0.2

External IP: 81.165.218.26


After that has been done, I will re-upload the script so everyone can use it. Posted Image

Update:

Here's a zip with my scripts. Note that they will need to be modified, and you'll need to
update bash in order to make them work properly.
Attached File  Scripts.zip   5.31KB   16 downloads

#2
ruger42

ruger42

    InsanelyMac Geek

  • Donators
  • 127 posts
  • Gender:Male
  • Location:South Florida, USA
Here's some help with parts 1 - 3, but you'll need to update BASH to 4.2 for it to work properly (I followed the instructions here)

#!/bin/bash
	 
	 hour=`date +"%H"`
	 day=`date +"%e" | sed -E 's/([^1]1)$/\1st/'| sed -E 's/([^1]2)$/\1nd/' |sed -E 's/([^1]3)$/\1rd/' | sed -E 's/([0-9])$/\1th/' | sed -e 's/^[ \t]*//'`
	 dayw=`date +"%A"`
	 monthyear=`date +"%B %Y"`
	 
	 time=`date +"%l:%M" | sed -e 's/^[ \t]*//'`
	 am_pm=`date +"%p"`
	 
	 username=`LOGNAME`
	 
	 if [ $hour -ge 0 -a $hour -le 6 ]
	 then
	 echo "Good night ${username[@]^},"
	 echo "		  it's now $time${am_pm,,}."
	 fi
	 
	 if [ $hour -gt 6 -a $hour -le 12 ]
	 then
	 echo "Good morning ${username[@]^},"
	 echo "		  it's now $time${am_pm,,}."
	 fi
	 
	 if [ $hour -gt 12 -a $hour -le 16 ]
	 then
	 echo "Good afternoon ${username[@]^},"
	 echo "		  it's now $time${am_pm,,}."
	 fi
	 
	 if [ $hour -gt 16 -a $hour -le 23 ]
	 then
	 echo "Good evening ${username[@]^},"
	 echo "		  it's now $time${am_pm,,}."
	 fi
	 
	 echo "Today's $dayw the $day of $monthyear."
I'll try to come up with something for the weather a little later...

#3
ruger42

ruger42

    InsanelyMac Geek

  • Donators
  • 127 posts
  • Gender:Male
  • Location:South Florida, USA
Try this for the weather:
weather=`curl --silent "http://xml.weather.yahoo.com/forecastrss?p=FRXX4403&u=f" | grep -E '(Current Conditions:|F<BR)' | sed -e 's/Current Conditions://' -e 's/<br \/>//' -e 's/<b>//' -e 's/<\/b>//' -e 's/ F<BR \/>//' -e 's/<description>//' -e 's/<\/description>//'`
   
   weather2=`curl --silent "http://xml.weather.yahoo.com/forecastrss?p=FRXX4403&u=c" | grep -E '(Current Conditions:|C<BR)' | sed -e 's/Current Conditions://' -e 's/<br \/>//' -e 's/<b>//' -e 's/<\/b>//' -e 's/<BR \/>//' -e 's/<description>//' -e 's/<\/description>//'`
	  
   conditions=`echo ${weather2,,}  | cut -f 1 -d ","`
	  
   tempC=`echo ${weather2} | cut -f 2 -d "," | sed -e 's/^[ \t]*//' | cut -c1-2`
	  
   if [ "$weather2" != "" ]
   then
   echo "It's $tempC degrees outside and the weather is $conditions."
   fi
All combined it should look like this:
Attached File  GeekToolHarmclShell.jpg   127.6KB   8 downloads


This will get you the count of unread emails at Gmail (not my creation, found it on the web):
curl --silent https://username:password@mail.google.com/mail/feed/atom/ | awk '/<fullcount>/{print}' | grep -o "[0-9]\+"
I can't find any way to retrieve data from Hotmail...

#4
harmc0re

harmc0re

    InsanelyMac Protégé

  • Members
  • PipPip
  • 81 posts
  • Gender:Male
ruger42! Thanks for the fast and great help! :)
I sure didn't expect half of the things were going to be solved that fast! :o

I have some problems with the Gmail command you gave me,
I can't get it to work, it gives nothing back.
If I go to https://username:password@mail.google.com/mail/feed/atom/ in my browser, I see the RSS with 2 new emails, so I know I got the username and password right... :P

#5
ruger42

ruger42

    InsanelyMac Geek

  • Donators
  • 127 posts
  • Gender:Male
  • Location:South Florida, USA

ruger42! Thanks for the fast and great help! :)
I sure didn't expect half of the things were going to be solved that fast! :o

I have some problems with the Gmail command you gave me,
I can't get it to work, it gives nothing back.
If I go to https://username:password@mail.google.com/mail/feed/atom/ in my browser, I see the RSS with 2 new emails, so I know I got the username and password right... :P

Don't know what to tell you, the Gmail code works perfectly for me as a single command or as part of the shell script:
unreadCount=`curl --silent https://username:password@mail.google.com/mail/feed/atom/ | awk '/<fullcount>/{print}' | grep -o "[0-9]\+"`
   echo "You have $unreadCount unread emails at Gmail"
Now gives me this:
Attached File  GeektoolHarmcl2.jpg   98.27KB   7 downloads

EDIT:
Make sure you're not using your full email address as the username, I seem to recall reading that if you use the @ symbol in the username field the script will not work...

#6
ruger42

ruger42

    InsanelyMac Geek

  • Donators
  • 127 posts
  • Gender:Male
  • Location:South Florida, USA
Here's some more stuff I found online for RAM, CPU, and Battery:
#!/bin/bash

##Change the value of totalRAM to match the amount of memory in your computer   

   myRAM=`top -l 1 -F -R | awk '/PhysMem/ {print "" $8+0}'`
   totalRAM=8192
   myPercentRAM=`expr $myRAM \* 100 / $totalRAM`
   
   echo "RAM: $myPercentRAM%"
   
   
   myCPU=`top -l 2 -F -R | awk '/CPU usage/ && NR>5 {printf "%d", $7+0}'`
   myCPU=`expr 100 - $myCPU`
   
   echo "CPU: $myCPU%"
   
   
   ioreg=`ioreg -w0 -l | grep "ExternalConnected|CurrentCapacity|MaxCapacity" | awk '{print $5}'`
   my_ac_adapt=`echo $ioreg | awk '{print $1}'`
   max_power=`echo $ioreg | awk '{print $2}'`
   cur_power=`echo $ioreg | awk '{print $3}'`
   bat_percent=`echo "scale=2;$cur_power / $max_power" | bc`
   bat_percent=`echo "$bat_percent * 100" | bc | sed 's/.00//'`
   
   if [ $my_ac_adapt == "Yes" ]
   then
   echo "Battery: $bat_percent% ↯"
   else
   echo "Battery: $bat_percent% ⚡"
   fi
I don't have a laptop so I can't verify if the battery percentage is actually working, but the rest seems to function properly...

#7
harmc0re

harmc0re

    InsanelyMac Protégé

  • Members
  • PipPip
  • 81 posts
  • Gender:Male
My Gmail counter is working properly now, like you said, I used my full email address. :(

The RAM and CPU are working good too,

Only the battery counter isn't.

The special thunderbolts are showing like this:
Posted Image

But I don't mind, I removed them. :D
But that still leaves me with no battery percentage.

Since you don't have a laptop, I will look myself to fix that.

Thank you so much for all the help! :)

#8
ruger42

ruger42

    InsanelyMac Geek

  • Donators
  • 127 posts
  • Gender:Male
  • Location:South Florida, USA

...But that still leaves me with no battery percentage.

Since you don't have a laptop, I will look myself to fix that.

Thank you so much for all the help! ;)

Glad to be of assistance, it was a fun learning experience...

Here's another script I found online that you can try for battery percentage:
ioreg -l | grep -i capacity | tr '\n' ' | ' | awk '{printf("%.2f%%", $10/$5 * 100)}'
Unfortunately I think you're out of luck with Hotmail - in order to get the number of unread emails you'd need to use IMAP, but Hotmail doesn't support that protocol, only POP3 which (from what I've read) doesn't provide the necessary functionality to retrieve such information. You can always forward your Hotmail messages to Gmail... :angel:

If you do manage to figure it out let us know!

#9
harmc0re

harmc0re

    InsanelyMac Protégé

  • Members
  • PipPip
  • 81 posts
  • Gender:Male

Glad to be of assistance, it was a fun learning experience...

Here's another script I found online that you can try for battery percentage:

ioreg -l | grep -i capacity | tr '\n' ' | ' | awk '{printf("%.2f%%", $10/$5 * 100)}'
Unfortunately I think you're out of luck with Hotmail - in order to get the number of unread emails you'd need to use IMAP, but Hotmail doesn't support that protocol, only POP3 which (from what I've read) doesn't provide the necessary functionality to retrieve such information. You can always forward your Hotmail messages to Gmail... :rolleyes:

If you do manage to figure it out let us know!

Okay, I found out that I had to change the output encoding setting in GeekTool to UTF8, allowing me to show the special symbols ↯ ⚡ and also the ° for degrees :D

Posted Image

And even better news: your battery meter works! :D

Sort of...

I had to switch the %10 and %5 because my battery percentage went from 100% to 101%, 102% when discharging :P
(Which would be cool if it was true :))

I also changed the "%.2f%%" to "%.0f" because I didn't want to show decimals, and I already added a % later in the script.

So this is my modified line of code:
%.0f", $5/$10 * 100)}' _linenums:0'>ioreg -l | grep -i capacity | tr '\n' ' | ' | awk '{printf("%.0f", $5/$10 * 100)}'

Thanks x100!

Yeah, about Hotmail, I was also thinking that. ):
I have been looking for a script that could ask Mail.app if there are any new emails in Hotmail,
but I only found scripts that tell me the total number of new emails so far.

But I'll keep looking. :)





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

© 2014 InsanelyMac  |   News  |   Forum  |   Downloads  |   OSx86 Wiki  |   Mac Netbook  |   PHP hosting by CatN  |   Designed by Ed Gain  |   Logo by irfan  |   Privacy Policy