Creating Desktop Shortcut/Alias programmatically

Does anybody have an idea of how to create a desktop shortcut and alter it’s icon programmatically (Xojo, declares, etc). I’m looking for a Windows solution, but cross-plat is even better.

What I want to do is search the current user’s desktop for an alias to the currently running program (but the name of the alias may not be known, so I’ll compare paths). If I cannot find a matching alias, I want to provide the ability to create one, with a custom icon. Does anybody have an idea?

Thanks

I wrote a class to do this. You can get it here. Basically it writes a vbs script then executes it in a shell. There are probably better ways, but this works for me.

Not necessarily better, but more compact:

Function CreateShortcut(scTarget as FolderItem, scName as String) As FolderItem
  //Creates a shortcut (.lnk file) in the users %TEMP% directory named scName and pointing to scTarget. Returns
  //a FolderItem corresponding to the shortcut file. You must move the returned Shortcut file to the desired directory.
  //On error, returns Nil.
  
  #If TargetWin32 Then
    Dim lnkObj As OLEObject
    Dim scriptShell As New OLEObject("{F935DC22-1CF0-11D0-ADB9-00C04FD58A0B}")
    
    If scriptShell <> Nil then
      lnkObj = scriptShell.CreateShortcut(SpecialFolder.Temporary.AbsolutePath + scName + ".lnk")
      If lnkObj <> Nil then
        lnkObj.Description = scName
        lnkObj.TargetPath = scTarget.AbsolutePath
        lnkObj.WorkingDirectory = scTarget.AbsolutePath
        lnkObj.Save
        Return SpecialFolder.Temporary.TrueChild(scName + ".lnk")
      Else
        Return Nil
      End If
    Else
      Return Nil
    End If
  #endif
End Function

P.S. I forget who I stole this method from, but I didn’t write it myself

Thanks. I’ll try both!

Old thread, I know. I simply need to create a shortcut.

Tried both methods.

Wayne’s has an error "Object does not support this property or method : ‘oShellLink.WindowStyle’

Andrew’s has an OLE error that the path of shortcut should end with lnk or url, yet I make sure of it.

I will appreciate a reliable method, maybe a plugin.

TIA

Hi Michel,

Here is a program that uses Andrew’s code to create a second shortcut on the desktop if Xojo2016r2 is installed.

Create Link Program

This seems to work on Windows 10 and Xojo 2016r2.

I couldn’t seem to get this error. Could you let me know if this works on your computer?

Thank you.

Hi Eugene,

It works perfectly. Thank you :slight_smile:

MBS Plugins also have classes for alias files on Mac and newer CFBookmarkMBS module.

http://monkeybreadsoftware.net/module-cfbookmarkmbs.shtml

For Windows we also have a class for creating shortcut files. And we have methods for hard and soft links.

http://monkeybreadsoftware.net/class-windowsshortcutmbs.shtml

Well, Christian, I love your products, but finding anything on your site is even worse than the proverbial needle in the haystack. That is where I looked first, before giving up, after wasting about half an hour chasing the uncooperative wild goose.

Thank you for the links, though.

Going on monkeybreadsoftware.net and entering Alias in the search finds it right away.
Not sure where you looked for it.

something it is faster to just ask Christian

1 Like

Hello Christian. Using cfbookmarkmbs class to create an Alias is the same things to have a shell command like:

ln -s /users/user/documents/Imyfolder /users/user/Desktop/myfolder.alias ?

I got troubles, because Alias created by the class are not recognized from Dropbox. Alias created by cfbookmarkmbs are seen like simple files but ones created by the cmmand ‘ln’ are seen like folders and are put in synch with Dropbox…

I’m stuck on this I m asking help to understand how to solve this issue. Thanks.

I am not sure this is the same. The command you show is a symbolic link, which can easily break if it is move, or if the target moves.

I believe MBS creates real OS X aliases, which follow the target if it is moved.

Now, the issue with DropBox may also come from the fact that intrinsically, DropBox is NOT OS X. Like most cloud systems, it is probably Linux. And Linux does snot know about osx aliases. Likewise, it does not deal very well with Windows shortcuts.

Hello Michael. probably you are right, but the state of the art is creating a ‘symbolik link’ (at this point I think Alias is another thing)
works and cfbookmarkmbs.CreateBookmarkData don’t.

ANy hint ?

Then create symbolic link with a shell and be done with it.

You don’t seem to quite understand that Symbolic Link is a Unix/Linux thing, whereas an Alias is a feature of macOS which does much more, as it is attached to the target in a way that makes it able to follow it if it is moved, or even renamed. I would tend to believe the state of the art is the most powerful, which is macOS alias.

But, most importantly, what do you want to do with Dropbox and symbolic links ? How are you stuck ?

The point is. Sandbox App doesn’t permit to use shell command execution. I need to create a symlink which is different from OSC Alias…the only way I found is cfbookmark but It doesn’t give me the expected result…

Please be aware that we offer a lot of functions depending on platform or what you need (alias, soft link, hard link)

MacAliasMBS class:
http://www.monkeybreadsoftware.net/class-macaliasmbs.shtml

CFBookmarkMBS module:
http://www.monkeybreadsoftware.net/module-cfbookmarkmbs.shtml
(see CFBookmarkMBS.WriteBookmarkDataToFile to write alias file)

NSFileManagerMBS with createSymbolicLink and linkItem functions:
https://www.monkeybreadsoftware.net/class-nsfilemanagermbs.shtml

See also WindowsJunctionMBS for creating symbolic link for Windows:
https://www.monkeybreadsoftware.net/module-windowsjunctionmbs.shtml

Uh ? This topic has been explored several times in the forum. For my part, I have half a dozen apps currently in the MAS that use plain out of the box shell in sandbox to call Unix commands. A couple of them are actually simple UI wrappers for command line.

However, you need to launch the shell with the Cocoa API if you want to use helpers, for instance. Sam Rowlands has posted the declare somewhere, but for myself, I simply use the MBS plugin LaunchServicesFindApplicationForInfoMBS.

The best solution for me is try to use NSFIlemanagerMBS. Hope App Wrapper doesn’t cut off the code…It is for sure that this code:

[code] mShell = New Shell
c = “ln -s '” + Replaceall(ff.shellpath,"","") + “’ " + DropBoxFolder.shellpath + “/’” + s +”’"

mShell.Execute©[/code]
Is cutted by App wrapper…so sincerely I don’t know how to use shell commands in the App Store…
@Christian S I want to notice that:

createSymbolicLink(path as string, destPath as string, byref error as NSErrorMBS) as boolean
doesn’t work as expected because I have an error…
Instead if I use (note the parameters switch):

createSymbolicLink(destPath as string,path as string, byref error as NSErrorMBS) as boolean

It works.
Now I have only to test if App Wrapper likes the code…
Thanks

did I mix the parameters?
oh sorry.
I’ll fix that soon.