iEFdev

Code, Computers & Random Junk

Converting Files Before #mv2linux

A few things todo before the move is to convert some Mac-files into “normal” files. That is, .webloc and .textClipping.

Text Clippings

They’re great. Really, but also a PITA to convert. A .textClipping is not a text file. The text/content is embedded in the resource.

If you don’t know what it is… It’s basically - you mark/select some text and drag the selction to your Desktop, or into a folder. The file (*.textClipping) can be used like you want it, like drag into a document and the content is pasted into it, etc.

There’s a tool to work with. It’s an old gem included in Xcode or the CLI-tools called DeRez - which is great and can extract the content, be used for scripting etc. However… A .textClipping can store both plain text and html/rich text so when scripting out the content, the results may vary.

Lucky for me there wasn’t too many of them in my system. I actually expected more, but there was some 500+ .textClipping files and I decided after a few days of looking and testing that I should do these manually. It took a couple of three days - sitting a few hours each night. I just made a list of all in a file to see where each were located. Some were tricky, and some could just be deleted.

Now I only need to stay away from creating new ones (old habits). smirk

Weblinks, url shortcuts

On most systems you can just drag the url from the address bar in your browser, and when it’s landing on the desktop or in a folder you get a shortcut. OS X have .webloc, Windows are using .url and Linux/*BSD are using the standard FreeDesktop with .desktop.

The three different ones look something like this.

Example: https://duckduckgo.com

DuckDuckGo.webloc (OS X)

<?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>https://duckduckgo.com/</string>
</dict>
</plist>

DuckDuckGo.url (Windows)

[InternetShortcut]
URL=https://duckduckgo.com/

DuckDuckGo.desktop (X desktops)

[Desktop Entry]
Encoding=UTF-8
Type=Link
Icon=text-html
Name=DuckDuckGo
URL=https://duckduckgo.com/

Like with .texClipping some older files of .webloc embeds the code/link in to the resource. Sometimes if not already in XML-format it can be converted in Terminal, and for the older ones you can use the same tool as mentioned above: DeRez.

The difficult part here is not to convert the files, but to decide what file format to use. On could go with the .url and make them useful in both Win, OSX and Linux. But I wanted to use the .desktop - thinking, if the new ones later will be in that format I’ll go there now. And I have no plans to use Windows (especially now when Win 10 is out).

Only thing about using .desktop I find somewhat annoying, is that there is a .directory extension to use with links to folders, and that .desktop are being use for programs etc. I’d really like to see another extension for web URL’s. But, since it’s standard - I’ll go with that.

Now, to convert these I needed a script. There’s no way that I was going to sit and manually convert 6000+ .webloc files. So, after some testing running single files, then testing on a few isolated directories - I finally converted all. All went fine and only 2 files I had to manually edit/convert afterwards.

I had setup so the ones that didn’t make it was put in a log file, and for all the ones that was successfully converted - the original .webloc was deleted.

Here’s the script.

linurlslink
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env bash
#
# /usr/local/xbin
#
# Description: Script to convert OS X .webloc's into Linux .desktop files
# (To use .desktop in Firefox/OS X, just open an info window, changing the
# program to FF and apply/change all. :))
#
# Disclainer: You use this script AT YOUR OWN RISK, and it comes with NO WARRANTY.
# It's been made for my own personal use.
#

# Put a log in current directory of the ones who didn't make it.
dtLog='./'$(basename $0)'_log.txt';

echo -e "/* These \"weblocs\" didn't convert into \".desktop\" */\n\n- - -\n\n" > "${dtLog}";

# Find all weblocs...
find . -name "*.webloc" | while read _file; do

  # Clean up names (:/ -> -). More chars?
  cleanUp=`basename "$(echo "${_file}")" | sed 's/[\/\:]/\-/g'`;

  # cleaning any xtra ":" just in case. "/" displays as ":" in Terminal.
  fileOut=`dirname "$(echo "${_file}")/${cleanUp}" | sed 's/[\:]/\-/g'`;

  # New extension
  dtFile=`echo "${fileOut}" | sed 's/webloc/desktop/'`;
  dtName=`basename "${fileOut}" | sed 's/.webloc//'`;

  # make sure the file(s) are XML, and get the url
  plutil -convert xml1 "${_file}" 2> /dev/null;
  dtUrl=`cat "${_file}" | grep -i '<string>.*<\/string>' | sed 's/^.*<string>//;s/<\/string>$//'` 2> /dev/null;

  # Having some old ones where they are in "/rsrc"... Try DeRez before dumping them to the log.
  if [[ -z "${dtUrl}" ]] ; then
      dtUrl=`DeRez -e -only url "${_file}" | sed -e 's/.*\/\* \(.*\) \*\//\1/' | sed -e :a -e '$!N; s/\n//; ta' -e 's/^.*{\(.*\)};/\1/'`;
  fi

  # Pass path/filename to logfile if it fails.
  if [[ -z "${dtUrl}" ]] ; then
      echo "${_file}" >> "${dtLog}";
  else

      # Just follow the freedesktop standard...
      cat << WEBLOC > "${dtFile}"
[Desktop Entry]
Encoding=UTF-8
Type=Link
Icon=text-html
Name=$dtName
URL=$dtUrl
WEBLOC

      # Only delete old webloc if the new has been created.
      if [ -f "${dtFile}" ]; then
          # Delete the webloc fileOut
          echo -n 'Deleting: ';
          rm -v "${_file}";
      fi
  fi
done

# Add date to the log
echo -e "\n\n" >> "${dtLog}";
date "+Log created: %F at %X" >> "${dtLog}";

# Reveal the logfile in Finder
open -R ${dtLog};

Use it at your own risk. No warranties.


So, what it does… It finds all files and first convert them to XML, if they’re not. Then using cat and sed to get the value in/of <string>. If they return empty, then DeRez is used to see if the URL can be extracted. And finally, if it fails - the path/location of the file is added to the log file so I can deal with them later. If a new .desktop was created, then the .webloc is getting deleted.

DeRez(1)


These ones are not too bad if I make new ones before the move. They’re just XML files and I can manually edit them if I need to. The 6000+ files was necessary though, since many of them were in the old(er) format - embed as resource. Some were as old as from 1999/2000 (and forward) - when I bought my PowerMac G4. I hade a Mac Performa 5400 before that, but I can’t remeber how I did with the links when I moved into the new Mac.

Anyway… Both .textClipping and .webloc files needs to be converted before (you move), unless you find a version of DeRez you can compile on your GNU/Linux machine to work with. I didn’t, and I don’t expect the binary will work outside OS X. There is no other way to deal with .textClipping files, except in OS X.


Happy hacking…

/Eric

Comments