Tuesday, September 21, 2010

Mount Samsung GT-i5800 as an USB Drive

Today I wanted to sync some of my music files with my new GT-i5800. Unfortunately, it didn't worked with MissingSync via Wi-Fi, so I tried USB. Well, how do I mount my GT-i5800 as a USB drive on OS X? By default, USB settings are "Samsung Kies", which I do not need on OS X (and with MissingSync). This setting has to be changed to "Mass storage" before connecting the USB cable!
Settings > About phone > USB settings > (X) Mass storage (default: Samsung Kies)
or in german:
Einstellungen > Telefoninfo > USB-Einstellungen > (X) Massenspeicher
Only after changing this setting, you can connect the GT-i5800 as described in the Missing Sync manual: "pull down the notification window (aka ‘window shade’) and tap USB connected."

Well, and thanks to bTunes ($1.49), my Android device almost feels like an iPod :-D


Thursday, September 16, 2010

Android on Samsung GT-I5800 and Mac OS X

Eventually I had to admit being a geek. As a consequence, I'm an owner of a Samsung Galaxy 3 i5800 (or simply GT-I5800) now. I'm extremely impressed: This Android phone costs 200€ only, and frankly I don't know why to spent 1000€ for an iPhone. Moreover, to me the iPhone is a little bit too big. The i5800 is a little bit smaller, thus it better fits in my trouser pocket. OK, the camera (3.2 MP) is just good enough for funny apps such as the Barcode Scanner, but I do not really need a camera in my phone. As I'm an Mac OS X user, the most important thing is syncing with my MacBook Pro. This is really easy thanks to Missing Sync: Address Book contacts (with photos), iCal calendars, iTunes, iPhoto , arbitrary folders, SMS logs, phone logs, and other things (and in future versions bookmarks) are easily synced via WLan. It can be configured in order to sync automatically every n minutes via WLan or manually. IMHO, this app is worth it's 39$. Most of my friends have iPhones. So I knew about some great apps they demonstrated me on their gadgets. I'm really surprised finding all these apps available for Android as well and nicely working on my GT-I5800:
  • Dropbox for file syncing
  • 1Password password manager, can be used in combination with Dropbox!
  • Call-a-Bike, great for finding a free bike nearby.
  • Shazam to find out which song is currently played
  • DB Railnavigator
  • Remote for iTunes: With this app I have remote access to my iTunes library on my mac (via WLan), and thanks to my Airport Express I can now listen to my music w/o having to jump back to my computer (in another room) for selecting a new album. I can even select the speakers via this app.
Other applications for the daily information overkill:
  • I'm still not that happy with the browser (I tried the build in browser and Dolphin, and I probably will have to spend some time getting along with the latter one as it provides an ad-blocker and reader plugin).
  • For RSS, I installed gReader Pro. It uses my Google Reader settings, so that I do not have to configure all RSS feeds on the phone. Actually, it is one of the rare things I'm using my Google account for on my phone.
  • As a twitter client, I have installed Twidroyd Pro. Seems as if you have to pay a few bucks for good apps.
  • As I have mulitple mail accounts, I installed the K9 mail reader. It is free, and it does what it says.
However I'm not feeling comfortable with having my mail account passwords (and others) stored on the phone. Actually, the mail account password is one of the most sensible passwords. If one cracks the mail account, it is quite simple to change other passwords of web applications as most web apps sends you a new password if you have forgotten your old one. The lock method of Android 2.1 (which is not too secure, see this paper, however, PINs wouldn't solve that problem, would they?) may help, but I'm not really convinced about that issue. I definitely have to investigate further into that topic.

Monday, May 24, 2010

LaTeX: Listings and labels

I have blogged about the listing package here several times (nicely formatting, and hyphenations). This time I will write about creating labels inside a listing. First of all, the listing package allows to define a caption and a label for a listing, e.g.:
\begin{lstlisting}[caption={myListing},label={lst:myListing}]
void foo(int x) {
 doSomething();
 doMore();
}
\end{lstlisting}
Now the listing can be referenced via \ref{lst:myListing}. The listing package also nicely supports line numbers, there are a whole bunch of settings for that. But why do we need line numbers? In most cases, line numbers are used to refer to a certain line within a listing. E.g, we maybe want to write something like
In line 2 of the listing 1 we call method 'doSomething'.
Well, "listing 1" will look like "listing \ref{lst:myListing}" in the LaTeX source code, but how do we reference the line? Fortunately, the listing packages allows us to escape to latex inside a listing and add a label which can then be referenced:
\begin{lstlisting}[caption={myListing},label={lst:myListing},numbers=left,escapeinside={@}{@}]
void foo(int x) {
 @\label{lst:myListing_2}@doSomething();
 doMore();
}
\end{lstlisting}
Now we can reference the line: In line \ref{lst:myListing_2} ... Actually, you can use any label text, however I usually use the listings label with the line number or a small marker, in order to avoid conflicts with duplicate labels. As I do not want to write "line..." and "listing ..." over and over again, I use the autoref command from the hyperref package:
In \autoref{lst:myListing_2} of \autoref{lst:myListing}
autoref automatically adds a name to the reference counter. As there is no name defined for line numbers, it has to be defined previously:
\providecommand*{\lstnumberautorefname}{line} 
If you need names in a another language, you can add a translation:
\addto\extrasngerman{% 
\def\lstlistingautorefname{Quellcode}% 
\def\lstnumberautorefname{Zeile}% 
}
OK, we can add a line label and easily create a reference to that label. However, writing line labels means a lot of work, especially if you use a listing relative name pattern. So, let's add a macro to TeXShop, which uses some shell commands (mainly sed) and AppleScript in order to automatically create the label, even adding the line number to the label:
--Applescript direct
--
-- Create a label inside a listing, must be invoked at the position at which the label is
-- to be created.
-- Precondition: the listing is defined inside a lstlisting environment and 
--   a label is defined in the parameter list of the environment (label={..}).
--
-- New newly created label consists of the label of the listing with the line number
-- appended to the label. You can configure escape characters (as specified in
-- escapeinside={..}{..}) and the separator between listing label and line number.
--
-- E.g. label={myListing}, on line 5 leads to the following output:
-- @\label{lst: myListing!5}@
--
-- (C) 2010 Jens von Pilgrim, http://jevopi.blogspot.com

property texapp : "TeXShop"
property escapeLeft : "@"
property escapeRight : "@"
property separator : "_"
property maxLength : 2000

try
 tell application texapp
  
  if texapp = "TeXShop" then
   tell application "TeXShop" to set pos to the offset of the selection of the front document
   tell application "TeXShop" to set currentSelection to the content of the selection of the front document
  else if texapp = "iTeXMac" then
   -- ??
  end if
  
  set start to 1
  if (pos > maxLength) then
   set start to pos - maxLength
  end if
  
  set preceeding_text to (characters start thru pos of (the text of the front document)) as string
  set preceeding_text to my findAndReplace(preceeding_text, "\\", "_")
  
  set lineNumbers to do shell script ¬
   "echo " & the quoted form of preceeding_text & ¬
   "| sed -n '/label[:space:]*=[:space:]*{[^}]*}/=;$ {x;=;}'"
  
  
  
  set theLines to my splitLines(lineNumbers) -- as list
  set currentLine to last item of theLines as integer
  -- set startLine to item ((length of theLines) - 1) of theLines as integer
  -- length is not working when executed as macro... workaround:
  set startLine to first item of (rest of (reverse of theLines))
  
  set lstLine to currentLine - startLine
  
  set lastLabel to do shell script ¬
   "echo " & the quoted form of preceeding_text & ¬
   "| sed -n '/label[:space:]*=[:space:]*{[^}]*}/h;$ {x;p;}'" & ¬
   "| sed -n 's/.*label[:space:]*=[:space:]*{\\([^}]*\\)}.*/\\1/g;p'"
  
  set newSelection to escapeLeft & "\\label{" & lastLabel & separator & lstLine & "}" & escapeRight & currentSelection
  
  
  if texapp = "TeXShop" then
   tell application "TeXShop" to set the selection of the front document to newSelection
  else if texapp = "iTeXMac" then
   --tell application "iTeXMac" to insert new_section in the text of the front document
  end if
  
 end tell
on error errmesg number errn
 beep
 display dialog errmesg
 return
end try


on findAndReplace(strInString, strFind, strReplace)
 set ditd to text item delimiters
 set text item delimiters to strFind
 set textItems to text items of strInString
 set text item delimiters to strReplace
 if (class of strInString is string) then
  set res to textItems as string
 else -- if (class of TheString is Unicode text) then
  set res to textItems as Unicode text
 end if
 set text item delimiters to ditd
 return res
end findAndReplace


on splitLines(strInString)
 set ditd to text item delimiters
 set text item delimiters to "
"
 set textItems to text items of strInString
 set text item delimiters to ditd
 return textItems
end splitLines
Now things are really easy: Simply activate the macro at the appropriate location in the listing, and a new label will automatically be created. Note: The listing must have a label defined! You can configure the script by changing the properties at the beginning of the macro.

Tuesday, April 20, 2010

LaTeX: Lstinline and Hyphenations

I really like the LaTeX listing package. However, there is one serious problem: Hyphenations are not supported (Option breakline does not break words)! Fortunately I found a solution which is not 100% perfect, but almost (let's say 99% ;-) ). The idea is to use the replacement feature of listings in combination with the discretionary command. Actually, this is not my idea, its Andrei Alexandrescu's idea. I slightly modified his solution. So, here it is. Actually I define a new command for inline Java code -- you may do this using a style, but I prefer this one:
newcommand{\lstJava}[1]{%
\lstinline[language=Java,breaklines=true,mathescape,%
literate={\-}{}{0\discretionary{-}{}{}}]§#1§}
As '§' is rarely used in Java, I use this character as code delimiter in lstinline. Also, '$' is seldom found in Java code, so I activate mathescape by default as I use it from time to time (that is, more often then '$' in Java). Now, the trick is the literate option. It is used to replace a string in the code with another given string. So, the well known \- - command (telling LaTeX where to hyphenate a word) is replaced here! And it is replaced by the \discretionary command. This one tells LaTeX, how to hyphenate a word, which is especially useful in German writings, where "ck" becomes "k-k". We use this command here to output the hyphen only when necessary. With this command, hyphenation is working within lstinline -- however you will have to mark it manually.
Let's have a look at a rather long Java class, called \lstJava{ThisIsA\-VeryLong\-ClassName}.

Sunday, April 18, 2010

Create BibTeX Entry from Safari Document with BibDesk

I often have to reference webpages from my papers. I use bibtex and in order to reference a webpage I define a new bibtex entry of type webpage. Since I do not want to type in these entries manually, and thanks to BibDesk, a small applescript does the trick for me:
-- (C) 2010 Jens von Pilgrim
tell application "Safari"
 set theTitle to name of front document
 set theURL to URL of front document
end tell

set theDate to (year of (current date)) & "-"
set theMonth to (month of (current date)) * 1
if theMonth < 10 then
 set theDate to theDate & "0"
end if
set theDate to theDate & theMonth & "-"
set theDay to day of (current date)
if theDay < 10 then
 set theDate to theDate & "0"
end if
set theDate to (theDate & theDay) as text

display dialog "Key for webpage " & theTitle & ": " default answer ""
set dlgResult to result

if (button returned of dlgResult = "OK") then
 set theKey to text returned of dlgResult
 
 tell application "BibDesk"
  if (count of documents) = 0 then make new document
  
  tell document 1
   set newPub to make new publication at end of publications
   set type of newPub to "webpage"
   set cite key of newPub to theKey
   set the value of field "Key" of newPub to theKey
   set the value of field "Type" of newPub to "URL"
   set the value of field "Url" of newPub to theURL
   set the value of field "Lastchecked" of newPub to theDate
   set the value of field "Title" of newPub to "{" & theTitle & ", Project Website}"
   
   show newPub
  end tell
  
 end tell
end if
This script grabs the URL from the currently active Safari document and creates a new bibtex entry. Some field are automatically filled, including "Type" which is required by some styles I use.The script asks for the key of the new entry, as this cannot be determined automatically (at least, I don't want the script to make a false guess). I added the script to the BibDesk script folder (at ~/Library/Application Support/BibDesk/Scripts), so it is accessible from within BibDesk. Example: The script produces for http://bibdesk.sourceforge.net/ the following entry:
@webpage{BibDesk,
 Date-Added = {2010-04-18 13:40:38 +0200},
 Date-Modified = {2010-04-18 13:40:38 +0200},
 Key = {BibDesk},
 Lastchecked = {2010-04-18},
 Title = {{BibDesk, Project Website}},
 Type = {URL},
 Url = {http://bibdesk.sourceforge.net/}}

Convert Text to a (TeXShop) Convenient Label

TeXShop support smart selection of words. If you double-click a text, the whole word is selected. A word in TeXShop consists of the letters 'A/a' to 'Z/z' and underscores. Spaces and hyphen '-' are not interpreted as word but as whitespaces separating words. With BibDesk installed 1), you can press the F5 key within a \ref-command and a list will pop up with all the labels defined in your text.
If you want to quickly select a label in order to copy and paste it manually to a reference (e.g., if you do not use \ref but \autoref instead, the latter is not recognized as reference command by BibDesk), spaces and hyphens (and other punctuation characters) are really annoying as you cannot simply select the label using a double click. Even more annoying: If a label is selected from the proposal list (activiated via F5), only the label characters before the first space or punctuation character are pasted.
For that reason I do not use whitespaces or punctuation characters in labels. Since I do not want to convert all characters myself, I have added a new macro to TeXShop (with a nice shortkey on Cmd+'_') which does the trick:
--Applescript direct

-- Converts spaces and punctuation characters to underscores, useable for labels
-- (C) 2010 Jens von Pilgrim
tell application "TeXShop"
set snippet to the content of the selection of the front document
-- replace commands
set snippet to do shell script ¬
"echo " & the quoted form of snippet & ¬
" | sed -E 's/([[:space:]]|[[:punct:]])/_/g'"
set the selection of the front document to snippet
end tell
Note: In a previous posting, I published a macro creating section headers automatically. I have improved this macro in order to convert the whitespaces and punctuation characters in labels as well.
1)Thank you, Herb Schulz, for reminding me that it's BibDesk which adds the completion list to TeXShop ;-)

Tuesday, April 6, 2010

BBL to BIB with BibDesk

BibDesk manages my bibliography, just like iTunes manages my music. I have a single iTunes library, and I also have only a single master bibliography. However, when writing a paper, I want to create a new bib file with only the publications cited in the paper. BibTeX nicely creates a bibliographic reference file (*.bbl), but I want a bibliography file (*.bib) in order to be able to open the paper related publications with BibDesk. So, what I needed was a tool to convert the bbl file into a bib file. Fortunately, I found everything for that on my machine: AppleScript, grep, sed and, of course, BibDesk. I wrote a small script which simply creates a new bib file using a source bib file and a selected bbl file. Simply install the script listed below to your script folder (~/Library/Scripts) and name it "Create BIB from BBL.scpt" . Open your master bib file with BibDesk, then select a bbl file and activate the script(from the script menu in the menu bar). It will automatically create a new bib file in the folder of the bbl file, with the same name as the original bbl file (but with the extension bib).
-- Creates a new bib-file from an bbl-file using BibDesk -- (C) 2010 Jens von Pilgrim -- Version: 1.1, 20100615 tell application "Finder" set selectedItems to the selection end tell if ((count of selectedItems) = 0) then display dialog "Please select at least one bbl file" buttons {"OK"} return end if -- retrieve master bib file set sourceDoc to "" tell application "BibDesk" if (count of documents) = 0 then display dialog "Please open the source bibliography with BibDesk" buttons {"OK"} return end if if (count of documents) > 1 then set listOfNames to {} as list repeat with doc in documents set listOfNames to listOfNames & name of doc end repeat set selected to (choose from list listOfNames) as string repeat with i from 1 to the count of documents set doc to (item i of documents) set strDocName to name of doc as string if (strDocName is equal to selected) then set sourceDoc to document i log "source doc set" end if end repeat if sourceDoc is equal to "" then return end if else set sourceDoc to document 1 end if end tell -- log "copy items from master bib file" -- convert all selected bbl files repeat with theItem in selectedItems set theFile to theItem as alias set posixpath to POSIX path of theFile if (offset of ".bbl" in posixpath) > 0 then set destPosixpath to (my rename(posixpath, "bbl", "bib")) set destFile to POSIX file destPosixpath tell application "Finder" if (exists destFile) then set rep to display dialog "File " & destFile & " already exists. Overwrite?" buttons {"Yes", "No"} set skip to (button returned of rep = "No") else set skip to false end if end tell if (not skip) then -- log "examine bbl file " & quoted form of posixpath -- actually, this is the most important line: set allCites to do shell script "grep -E \"\\\\\\\\bibitem[:space:]*(\\[[^]]*\\])?[:space:]*{([^}]*)}\" " & (quoted form of posixpath) & " | sed -E \"s/\\\\\\\\bibitem[:space:]*(\\[[^]]*\\])?[:space:]*{([^}]*)}/\\2/\"" set numberOfItems to length of paragraphs of allCites if numberOfItems = 0 then display dialog "No bibitems found in " & posixpath & ", maybe the file does not contain any bibitems or the search pattern does not recognize your file format." buttons {"Too bad."} else set numberOfMissedItems to 0 set missedItems to "" tell application "BibDesk" set destDoc to make new document repeat with cite in paragraphs of allCites set bibs to search sourceDoc for cite set bFound to false repeat with bib in bibs set strFoundKey to cite key of bib as string set strCite to cite as string if strFoundKey is equal to strCite then -- log strCite & " found, add to new bib" set newBib to make new publication at end of publications of destDoc duplicate bib to newBib set bFound to true end if end repeat if not bFound then set numberOfMissedItems to numberOfMissedItems + 1 if (numberOfMissedItems > 1) then set missedItems to missedItems & ", " end if set missedItems to missedItems & cite end if end repeat save destDoc in destFile end tell if numberOfMissedItems > 0 then display dialog "Did not found " & numberOfMissedItems & " out of " & numberOfItems & " items: " & missedItems & "." buttons {"Uups"} end if end if end if else display dialog "Can only extract bib entries from BBL file, was " & posixpath end if end repeat -- this sub-routine just comes up with the new name on rename(item_name, item_ext, new_extension) set the trimmed_name to text 1 thru -((length of item_ext) + 2) of the item_name set target_name to (the trimmed_name & "." & new_extension) as string return the target_name end rename
If you don't have BibDesk, you might want to have a look at Michael Zhang's Perl Script. Instead of parsing the bbl file, Zhang's script parses the latex source. I prefer parsing the bbl file, as sometimes I use a whole bunch of latex sources, and BibTeX is quite good in gathering all citations into a single bbl file. You may also want to read the comments to Zhang's post for other solutions. If you don't have a master bib file, you may look at http://www.tex.ac.uk/cgi-bin/texfaq2html?label=makebib. There's a perl scrip provided reconstructing a bib file from a bbl file only. Update 2010-06-15: I updated the script as some BBL files were not recognized. If the script doesn't work, you may have a look at your BBL file. At the moment, the script searches for bibitems like "\bibitem{key}" or "\bibitem[abbr]{key}".