Friday, January 28, 2011

Changing the default style in Kod

Kod is a promising text editor for OS X.

One of the great things is that, you can style the editor with CSS.

mkdir ~/.kod
cp /Applications/Kod.app/Contents/Resources/style/default.css ~/.kod/

defaults write se.hunch.kod style/url ~/.kod/default.css

kod ~/.kod/default.css


then don't forget to reload the style in Kod, View menu/Reload style

Thursday, January 27, 2011

Simulating mouse click on Snow Leopard

After googling for 1 hour i came up with the following python script.

import sys
import time
from Quartz import *

def createMouseEvent(type, posx, posy):
theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, theEvent)

if __name__ == '__main__':
x = int(sys.argv[1])
y = int(sys.argv[2])

createMouseEvent(kCGEventLeftMouseDown, x, y);
createMouseEvent(kCGEventLeftMouseUp, x, y);


Usage: python script.py 100 100
It'll move the mouse to (100, 100)

Here is the same script on github. click.py

I used the following sources
metapep's blogpost
pepijndevos's PyMouse
GeekOrgy's Python script
a stackoverflow entry

thanks!

Tuesday, January 25, 2011

Telling time in different timezones with bash on OSX

You can use the TZ variable to affect the execution of date command.

Current time in Japan Standard Time (JST)
TZ=Asia/Tokyo date

Pacific Standard Time (PST)
TZ=America/Los_Angeles date

Eastern Standard Time (EST)
TZ=America/New_York date

Central Standard Time (CST)
TZ=America/Chicago date

Coordinated Universal Time (UTC) / Greenwich Mean Time (GMT)
TZ=Europe/London date

Central European Time (CET)
TZ=Europe/Budapest date


Here is how i use it. telltime.sh

Escaping special HTML characters in JSP

If you have a string that contains special characters such as '&' and you want to display this in a JSP page, you may get a ' The content of elements must consist of well-formed character data or markup.'

This code will fail if linkParm contains a '&'
<a href="main.do${userBean.linkParm}">
main link
</a>


The short answer is
<a href="main.do<c:out value='${userBean.linkParm}'/>">
main link
</a>


This seems overkill, indeed.

Thursday, January 20, 2011

EJS default escaping

In EJS (Embedded JavaScript) escaping is a default behaviour.

// escape by default
<%= VARIABLE_NAME %>

This can easily mess up a couple of things (including JSON, HTML rendering), luckily you can turn it off by using

// render out string
<%- VARIABLE_NAME %>

Monday, January 17, 2011

Thursday, January 6, 2011

Iterating through an array in bash

locations=( Roppongi Shibuya Ebisu )

for name in ${locations[@]}
do
echo $name
done

Generating random locations around a location

Handy.

def generateLocation(startlat, startlon, maxdist):

# converting them radians
startlat *= pi / 180
startlon *= pi / 180
rand1 = random.random()
rand2 = random.random()

maxdist = maxdist / earthradius
dist = acos(rand1*(cos(maxdist) - 1) + 1)
brg = 2*pi*rand2

lat = asin(sin(startlat)*cos(dist) + cos(startlat)*sin(dist)*cos(brg))
lon = startlon + atan2(sin(brg)*sin(dist)*cos(startlat), cos(dist)-sin(startlat)*sin(lat))

if lon < -pi:
lon = lon + 2 * pi

if lon > pi:
lon = lon - 2 * pi

lat *= 180 / pi
lon *= 180 / pi

return [lat, lon]

Tuesday, January 4, 2011

Turn off SpringSource Tool Suite Dashboard

SpringSource Tool Suite 2.3.2 boots really slowly and it can be even slower if it opens the Dashboard at startup.
Lucky you can turn it off, not trivial though.

Dashboard has 4 tabs on the bottom (Dashboard, Knowledge Base, Extensions and Configuration).
At the Configuration tab there is a checkbox, Show On Startup.