OS X – AppleScript to open Apps Hidden

I need to open Safari with a specific URL on command line. The problem is that this has to happen completly in background and should be started through an existing bashscript.
Here is my solutiuon, it should apply to any other app in OS X SnowLeopard. You need to call the bash schript like this:


./open_safari_with_url.sh "http://www.site_i_want_to_visit.com"

“open_safari_with_url.sh” looks like this:


#!/bin/bash
osascript -e "
tell application \"Safari\" 
	tell application \"Finder\"
		set visible of process \"Safari\" to false
	end tell
	launch
	set the URL of document 1 to \"$1\"
end tell"

[EDIT] It seems that ‘launch’ in AppleScripts starts the app in background. So we don’t need to tell Finder to hide Safari anymore.

tell application "Safari" 
	launch
	set the URL of document 1 to "http://whatever..."
end tell"

awk and word frequencys

I have a list that I use for making gnuplots. The structure is the following

wordId wordWeight

e.g. 101 34.342 = Word with id 101 has a calculated weight of 34.342 times.

The bad thing is that this list is unordered. Now I wan’t to order this list getting the greatest weight and their corresponding word id. Bash doens’t seem to be the best solution for this so I made up my first awk script.

Here is it, it prints the most common word from file toPlot.stats.

awk ‘ BEGIN{ max=0; w=-1 } { if ( $2 >= max) { max=$2; w=$1 } } END{ print “id”,w,”has  most often with count of”, max; }’ toPlot.stats

and the output:

id 1545 has greates weight with  28199.40186090438

My file has 1.722.913 lines, execution time:

real    0m1.618s
user    0m1.576s
sys     0m0.032s

open terminal here

Marc Liyanage wrote a nice Apple Script which easily allows you to switch from the Finder to the Terminal.
Just download it here and drag the script to the upper free space in the Finder.

Clicking it results in a new Terminal window which location is the same as the Finder location.
new Finder icon

Here’s how it looks.

P.S. this works under OS X 10.6 😉

first look at javafx

Today I had my first look at javafx. Real nice but you must get used to the syntax.
After 1/2 hour of reading and 1/2 hour of modifying a nice netbeans example (original version from Michal Skvor) i end up with this. Not perfect but it has shown me how the javafx basics work.

screenshot
winner screen

screenshot

And after I read this ‘Deploy A Rich Internet Application Developed With JavaFX Technology‘ (another 10 minutes) i was able to deploy it to the web.

Start it

If you like it let me know

You can cheat by pressing ‘r’=remove single block. ‘s’=remove more 😉

Source Code can be foundhere

sort a hashmap, fast

Imagine you have an hash map where key is an String and value is a integer.
Now you want to sort based on the value of each entry in this hashmap in descanding order.

Here is how I do it:

List<Map.Entry<String, Integer>> list = new Vector<Map.Entry<String, Integer>>(myHashMap.entrySet());
java.util.Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
      public int compare(Map.Entry<String, Integer> e, Map.Entry<String, Integer> e1) {
          return (e.getValue().equals(e1.getValue()) ? 0 : (e.getValue() > e1.getValue() ? 1 : -1));
}}); 
 

Now the list contains the sorted entrys from hashmap ‘THashMap myHashMap<String,Integer>’.
Iterate over it or copy it back to ‘myHashMap’ if you need to.

Hope this helps …

java decompilers

Because I have no recent Backup of the big java project I was working I have to decompile the class Files that are in this jar.
I found 3 different java decompilers for mac os x.

  1. MacJAD (google for Download)
    – onyl able to open single class files
  2. JarInspector (http://www.codeland.org/)
    + can open jar files
    + correct 
    – unable to decompile the anonymous inner classes I use
    – seems to be confused by nested catch and try blocks 
  3. JD-GUI (http://java.decompiler.free.fr/)  
    + can open jar files
    + 90% able to decompiling anonymous inner classes

A big problem all tools share is that if you have nested iterations, for/while loops they all rename the variable you iterate over to ‘i$’. So it’s possible if your code looks like this:

while(iterator.hashNext()) {
...
    for(int varA; ....){
        for(int varB; ....){.....}
    }
...
}

the decompiled code will end up looking this:

while(i$.hashNext()) {
...
    for(int i$; ....){
        for(int i$; ....){.....}
    }
...
}

which isn’t really funny …

My conclusion is that JarInspector seems to be the best but if you need the inner class support use JD-GUI for the parts of the code those are in.