Wishful Coding

Didn't you ever wish your computer understood you?

Open external links in running browser

As a web developer I have like 5 browsers installed which I use regularly. That is, not on daily basis, but for occasional testing and browsing. What annoys me most about this is that when I click a link in another application, no matter what browser I'm currently running, it opens a fresh Safari window. The ideal solution would be to open any urls with the browser I'm using at that moment, no mater which browser that is and which one is set as the default browser. I have written a piece of Applescript that will do just this, you might need to edit this for the browsers which you are using though. Below you'll find a zip file containing the app bundle, and all the scripts needed to generate it yourself.

Browser Loader

To edit the browsers you use, open "/Applications/Utilities/AppleScript Editor.app" and then open the app bundle, this will reveal the script below, where you can make adjustments to the browsers used and their order. If you want do do this yourself, or the app bundle does not work for you for some reason, this is the actual Applescript I used:
on open location the_url
        tell application "System Events"
                set browser_apps to the name of every process whose visible is true
                if "Opera" is in browser_apps then
                        tell application "Opera"
                                open location the_url
                                activate
                        end tell
                else if "firefox-bin" is in browser_apps then
                        tell application "Firefox"
                                open location the_url
                                activate
                        end tell
                else if "Google Chrome" is in browser_apps then
                        tell application "Google Chrome"
                                open location the_url
                                activate
                        end tell
                else
                        tell application "WebKit"
                                open location the_url
                                activate
                        end tell
                end if
        end tell
end open location
Copy this file to the Applescript editor and hit Save, before you save, choose to save as an app bundle and make sure to check the "Stay Open" box. Now we need to modify the app bundle to make it eligible for url opening. To do this, right-click on the app and select "Show Package Contents", go to "Contents" and open "Info.plist". Edit "Info.plist" to contain this xml code within the outer dict element. For an example, download the app bundle above.
<key>CFBundleIdentifier</key>
        <string>nl.pepijndevos.urlhandler</string>
        <key>CFBundleURLTypes</key>
        <array>
                <dict>
                        <key>CFBundleURLName</key>
                        <string>Applescript urls</string>
                        <key>CFBundleURLSchemes</key>
                        <array>
                                <string>http</string>
                                <string>https</string>
                                <string>file</string>
                        </array>
                        <key>LSIsAppleDefaultForScheme</key>
                        <true/>
                </dict>
        </array>
Now, when you click an url it will just open your browser. I'm not sure what to do exactly, but here is what I did
  1. Run the app once
  2. Set it as the default browser in Safari
[update] Using Linux? Try this script on Humbug.in This script has also appeared on lifehacker.com download.com and maxosxhints.com
Posted

Delicious bookmarks in Safari

I recently started using Delicious, and while they have a nice Firefox plugin and a bunch of bookmarklets, there is no easy way to get Delicious bookmarks into the Safari bookmarks bar. To get this done I wrote an Applescript with a little piece of Python to get Delicious bookmarks right in Safari. Here is how it looks:
Here is how to do it:
  1. Get the 2 scripts below and save them to a suitable location. You might want to place it in ~/Library/Scripts/Applications/Safari
  2. Edit the Applescript with the location of the Python script and the URL of your Delicious feed.
  3. Run the Applescript.
  4. Drag the resulting bookmarks folder or its content anywhere you want.
This will work for any RSS and maybe even Atom feed, you could even bookmark the feed of your Twitter account, for... *hum* easy access to individual tweets. This is the Python code:
import feedparser
print "<dl>"
for e in feedparser.parse( "http://feeds.delicious.com/v2/rss/pepijndevos" ).entries:
    print '<dt><a href="%s">%s</dt>' % (e.link, e.title.encode('ascii','ignore'))
print "</dl>"
Don't ask me why, but Safari needs broken html to import. Maybe because it is meant to import IE bookmarks? Anyway, this is the Applescript, the real thing:
do shell script "python ~/bin/feed2html.py > ~/Documents/delicious.html"
tell application "Safari"
        activate
        tell application "System Events"
                tell application process "Safari"
                        click menu item "Import Bookmarks…" of menu "File" of menu bar item "File" of menu bar 1
                        keystroke "g" using {shift down, command down}
                        keystroke "~/Documents/delicious.html"
                        keystroke return
                        keystroke return
                end tell
        end tell
end tell
Posted

Shorten Wordpress urls in Adium

I use Adium as my Twitter client, it includes a nice url shortener supporting multiple services, so what is my problem? Nothing, I just had some fun making use of the wp.me service. Every Wordpress blog contains a header containing the short url, so it was only 3 lines of Applescript and one line of Bash to get the short url of any wp blog and post it to Adium. If anyone with knowlegde of regex/grep comes around, he might get it to work with any shorturl/shortlink header.

The current line looks like this:
curl --head <url> -s | grep -o http://wp\.me/[a-zA-Z0-9\-]*
Update: sed seems to be able to do what I want.
curl --head http://pepijn.cqhosting.nl/ -s | \
sed -n 's/Link: <\(.*\)>; rel=shortlink/\1/p'
Use:
%_shorten{<url>}
Download the script: shorten.AdiumScripts
Posted

Automatically remove Trash from usb sticks

I think most Mac users have had this problem; You delete a bunch of files from your pen drive to make room for a large file, and it keeps complaining about lack of space. The problem is that when you remove files on Mac, they're just moved to a hidden folder named '.Trashes', so they are actually still there. A commonly suggested solution is to empty the Trash while the USB stick is plugged in, but this might be problematic in some cases where you want to keep your Trash, just in case...

My new solution is to set this little script as a folder action, it will remove the trash only from the USB stick as soon as you mount it(unmount proved problematic).
on adding folder items to this_folder after receiving these_items
    do shell script "rm -rf " & POSIX path of these_items & ".Trashes/"
end adding folder items to
Save this script to /Library/Scripts/Folder Action Scripts/ and now set it as the folder action for /Volumes.
You might need to show hidden files in Finder for /Volumes to be visible. To do this, follow this guide.
Posted