How can I get the path of the desktop directory?

Hi!

I try to copy a shortcut file into the desktop. I have made a lot of research about this.

On Linux, I check if XDG_DESKTOP_DIR is set and non empty. If not, I check if $HOME/.config/user-dirs.dirs exists; if so, I call “. $HOME/.config/user-dirs.dirs”. Then, I can use System.getenv(“XDG_DESKTOP_DIR”). Using directly XDG_DESKTOP_DIR is enough on GNOME but not on KDE (.config/user-dirs.dirs is useful in this case).

On Mac, I’m not sure $HOME/Desktop works for non English users. Does someone have a better idea? I don’t really know AppleScript…

On Windows, I have found this but there is a missing class StreamReader:

This does not work always:

FileSystemView filesys = FileSystemView.getFileSystemView();

File[] roots = filesys.getRoots();

filesys.getHomeDirectory();

I could use SHGetFolderLocation or SHGetFolderPath APIs but I prefer avoiding native calls. Does someone have a better idea?

The Java Web Start shortcut feature has too much bugs including this one and this other one, I cannot use it. It has never worked reliably on KDE. Thank you very much for your attention.

I think StreamReader should be InputStreamReader.

Cas :slight_smile:

StreamReader is defined in the code he posted :stuck_out_tongue:

static class StreamReader extends Thread {
    private InputStream is;
    private StringWriter sw;

    StreamReader(InputStream is) {
      this.is = is;
      sw = new StringWriter();
    }

    public void run() {
      try {
        int c;
        while ((c = is.read()) != -1)
          sw.write(c);
        }
        catch (IOException e) { ; }
      }

    String getResult() {
      return sw.toString();
    }
  }
}

Thanks… I was a bit… absent-minded :-X

What are you trying to do which requires you to find the desktop folder without asking the user where it is?

I’m trying to create a shortcut on the desktop. This feature is quite broken in Java Web Start. Mine is almost currently ready for Linux, Windows will require a bit more work and I have no idea of how to implement it on Mac.

Just to throw it out there, there are other ways to get the desktop on Windows via Batch to WShell or the registry here.

I personally would use the WShell method as I believe registry access is more likely to be locked down.

If all your checks fail you can also just fall back onto checking if ‘~/desktop’ folder exists as if it does then it’s probably going to be their desktop (although yes, this is far from certain but the fact the folder exists does make it more likely).

%USERPROFILE% is not present on very old Windows (95, 98). ‘~/desktop’ folder exists only on English desktops, I don’t know on which versions of Windows WShell is available. Maybe I should find an heuristic method if the registry method fails.

Fortunately, 60% of the people who try TUER are on Linux :slight_smile:

Edit.: WShell is present on Windows since Windows 98.

Why not have a couple of default checks, such as ‘~/Desktop/’ or the equivalent on Windows. If that doesn’t exist, then just ask the user where they’d like to put the shortcut (and you could write it somewhere in case they install a second game of yours and you could just look it up).

This way, you get the common case really easily but you have a valid fallback for the weird OS installs, old versions or non-English OS’s (which you won’t be able to get correct all of the time anyway).

Actually the registry method rarely fails. JL235’s suggestion would be an excellent fallback, I would like to use such a source code but in Java:

use Win32::OLE;

  my $wsh = new Win32::OLE 'WScript.Shell';

  my $desktop_path = $wsh->SpecialFolders('Desktop');

I don’t want to prompt the user.

I could use this:

WScript.Echo(WScript.SpecialFolders("Desktop"));

If I do this:

wscript //NoLogo //B

does a Windows user see a logo or a command display when I use WScript?

I have found no way of getting the desktop directory on Mac OS X. Is there a Mac user here?

Hi!

On Mac OS X, whatever the locale, it seems to be:
Mac OS X/Users//Library/Desktop
I can get this with System.getProperty(“user.home”)+"/Desktop".
On Mac OS 9, it seems to be:
Macintosh HD:Users::Desktop Folder

Using the registry on Windows might cause some localization problems and the use of “Shell Folders” registry key is deprecated since Windows Vista then I will use WShell.

In Vista/7 the directory ./Desktop always exists.

For different locales, it is hidden, and a symlink (with a localized name) to it is created.

What happens if you move your desktop somewhere else? Thank you for this explanation as I rarely use Windows, I’m a bit lost and I have no way of testing the WShell script.

If your making a game then will it even run on a machine still running Windows 95 or 98; will they even have a recent version of Java installed? I’d presume it’s there but fall back onto asking or doing nothing if it’s not.

What is the likely hood of a pre-Vista Windows user (English or non-English) having a folder called ‘Desktop’ in their home directory which isn’t their desktop? I’d bet it’s extremely low. If it’s there and you used it then I’d imagine it’s the right place 99.9% of the time (although again I’d only do this as a fall back option).

%USERPROFILE% is not localized, it cannot be used but you’re right, I’m even not sure it is possible to install Java 1.6 on Windows 95.

[quote=“JL235,post:14,topic:36009”]
Lots of people still use Windows XP and I have a lot of French users (as I’m French). I suggest using the following algorithm:

  • use WScript.SpecialFolders(“Desktop”) as it is the most reliable and not deprecated way of getting this piece of information since Windows 98
    if it does not work
    • use the registry method and replace the environment variables by their values (as I did for Linux)
      if it does not work
      • try to use System.getProperty(“user.home”)+"\Desktop" as Riven says this directory always exists on Vista and 7

Can I use Runtime.getRuntime().exec() to execute the WShell command lines? What displays WScript.Echo(WScript.SpecialFolders(“Desktop”)); on your machine?

Edit.: Java 1.6 cannot be installed on Windows 98 but still on Windows 2000.
Edit.2: I will use a simple bash script on Mac as it seems very complicated to create an Alias

The Mac’s desktop is always located at ‘~/Desktop’ for English versions, I’m not sure if it’s different for non-English but Mac has a pretty rigid folder convention. The is always located in /Users/<username/Desktop (you had an extra Library folder there, which is incorrect). Reading here (http://osx.tribe.net/thread/ea74316d-e1f6-4de7-a518-0836864bc7fb) it appears that within the Terminal, localized installs still use paths named Desktop and just display the translated version with the GUI folder environment.

It is not different for French users ;D

Creating an alias (equivalent of a shortcut file on Mac) is so complicated, I’m going to use a simple bash script.

Hi

As far as I know, my implementation works on Linux, Mac and Windows. It has not been tested on Unix. Thank you for your help :slight_smile: Look at my SVN repository to find the source code:
http://tuer.svn.sourceforge.net/viewvc/tuer/pre_beta/engine/DesktopIntegration.java?view=markup