Looking for a convenient way to convert from .webloc files to cross-platform .url files, I was reading this hint. But all the suggestions involved opening the .webloc file in Safari to get hold of the actual URL. Doing so however, is either very time consuming (especially if you have many .webloc files) or simply impossible if you're working off-line. The reason for using Safari, I realized, is that the URL is stored in the resource fork of the .webloc file (Apple, you were supposed to phase out resource forks!) and is not easy to get at. Luckily, Apple included a little gem in its Developer Tools, DeRez, which displays resource information as raw text in this format:
$ DeRez -e -only 'url ' macosxhints.webloc
data 'url ' (256, "macosxhints - Get the mo#11F5AF") {
$"6874 7470 3A2F 2F77 7777 2E6D 6163 6F73" /* http://www.macos */
$"7868 696E 7473 2E63 6F6D 2F" /* xhints.com/ */
};
The following AppleScript application lets you drag and drop any number of .webloc files over its icon and converts them to cross-platform .url files.
This script does three things for each file:
property devToolsPath : "/Developer/Tools/" -- path to free Apple Developer Tools
on open draggedFiles
local theFile, weblocPath, urlPath, theURL, cmd
repeat with theFile in draggedFiles
if (theFile as string) ends with ".webloc" then
-- get full paths
set weblocPath to quoted form of POSIX path of theFile
set urlPath to text 1 thru -9 of weblocPath & ".url'"
-- 1) get URL from .webloc file
set cmd to devToolsPath & "DeRez -e -only 'url ' " & weblocPath & ¬
" | grep '\\/\\* .* \\*\\/' | sed 's/^.*\\/\\* //;s/ \\*\\/$//' | tr -d '\\n'"
set theURL to do shell script cmd
if theURL begins with "http" then
-- 2) write URL to file
set cmd to "echo '[InternetShortcut]" & (ASCII character 10) & ¬
"URL=" & theURL & "' > " & weblocPath
do shell script cmd
-- 3) rename file using MvMac now that we have Developer Tools anyway
set cmd to devToolsPath & "MvMac " & weblocPath & " " & urlPath
do shell script cmd
end if
end if
end repeat
end open
The script conserves the resource fork, as it writes to the data fork and renames the file. When copying the file to a Windows user, the resource fork is lost but the plain file contains all the information needed by the browser. The script could easily be rewritten to get rid of the resource fork by simply writing the data to the .url path instead of to the .webloc path and deleting the .webloc file instead of renaming it.
Mac OS X Hints
http://hints.macworld.com/article.php?story=20040728185233128