Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crafting .webloc file

I'm writing a program (for Mac OS X, using Objective-C) and I need to create a bunch of .webloc files programmatically.

The .webloc file is simply file which is created after you drag-n-drop an URL from Safari's location bar to some folder.

Generally speaking, I need an approach to create items in a filesystem which point to some location in the Web. As I understand .webloc files should be used for this on Mac OS X.

So, is it possible to craft a .webloc file having a valid url and some title for it?

like image 339
Yurii Soldak Avatar asked Sep 28 '08 19:09

Yurii Soldak


2 Answers

It is little known - but there is also a simple plist based file format for weblocs.

When creating webloc files you DO NOT NEED to save them using the resource method the other three posters describe. You can also write a simple plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>URL</key>
    <string>http://apple.com</string>
</dict>
</plist>

The binary resource format is still in active use and if you want to read a plist file - then sure you need to read both file formats. But when writing the file - use the plist based format - it is a lot easier.

like image 67
schwa Avatar answered Sep 29 '22 10:09

schwa


A .webloc file doesn't have anything in its data fork; instead, it stores the URL it refers to as a resource in its resource fork. You can see this on the command line using the DeRez(1) tool

Here I've run it on a .webloc file that I dragged out of my Safari address bar for this question:

% DeRez "Desktop/Crafting .webloc file - Stack Overflow.webloc"
data 'drag' (128, "Crafting .webloc file -#1701953") {
    $"0000 0001 0000 0000 0000 0000 0000 0003"            /* ................ */
    $"5445 5854 0000 0100 0000 0000 0000 0000"            /* TEXT............ */
    $"7572 6C20 0000 0100 0000 0000 0000 0000"            /* url ............ */
    $"7572 6C6E 0000 0100 0000 0000 0000 0000"            /* urln............ */
};

data 'url ' (256, "Crafting .webloc file -#1701953") {
    $"6874 7470 3A2F 2F73 7461 636B 6F76 6572"            /* http://stackover */
    $"666C 6F77 2E63 6F6D 2F71 7565 7374 696F"            /* flow.com/questio */
    $"6E73 2F31 3436 3537 352F 6372 6166 7469"            /* ns/146575/crafti */
    $"6E67 2D77 6562 6C6F 632D 6669 6C65"                 /* ng-webloc-file */
};

data 'TEXT' (256, "Crafting .webloc file -#1701953") {
    $"6874 7470 3A2F 2F73 7461 636B 6F76 6572"            /* http://stackover */
    $"666C 6F77 2E63 6F6D 2F71 7565 7374 696F"            /* flow.com/questio */
    $"6E73 2F31 3436 3537 352F 6372 6166 7469"            /* ns/146575/crafti */
    $"6E67 2D77 6562 6C6F 632D 6669 6C65"                 /* ng-webloc-file */
};

data 'urln' (256, "Crafting .webloc file -#1701953") {
    $"4372 6166 7469 6E67 202E 7765 626C 6F63"            /* Crafting .webloc */
    $"2066 696C 6520 2D20 5374 6163 6B20 4F76"            /*  file - Stack Ov */
    $"6572 666C 6F77"                                     /* erflow */
};

The only resources that probably needs to be in there are the 'url ' and 'TEXT' resources of ID 256, and those probably don't need resource names either. The 'urln' resource might be handy if you want to include the title of the document the URL points to as well. The 'drag' resource tells the system that this is a clipping file, but I'm unsure of whether it needs to be there in this day and age.

To work with resources and the resource fork of a file, you use the Resource Manager — one of the underlying pieces of Carbon which goes back to the original Mac. There are, however, a couple of Cocoa wrappers for the Resource Manager, such as Nathan Day's NDResourceFork.

like image 42
Chris Hanson Avatar answered Sep 29 '22 10:09

Chris Hanson