Easy conversion of multiple Safari .webloc to PC .url files

Jul 30, '04 09:28:00AM

Contributed by: osteslag

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:

  1. Extracts the URL from the .webloc file's resource fork
  2. Writes URL to the file's data fork
  3. Renames the file
Here's the script:
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.

The output from the DeRez command is piped to grep which pipes all lines containing "/* [any text] */" to sed which extracts the [any text] between the comment marks and pipes the text to tr to get rid of the newlines. This may be optimized further, but performance isn't really an issue here.

I'm using MvMac (Dev Tools required, which you need anyway for DeRez) to rename the file, but I'm sure there's a Finder command for this as well. However, I'm not really an AppleScript programmer, so I do most of my stuff in Terminal's command line interface (I have a shell script that does all of the above if anybody's interested).

Comments (30)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040728185233128