Cross-platform way to open URLs with non-default applications

I thought I might be lucky and have a solution for this in the thread about weblocs but no.

I need to catch some URLs for pre-processing, and then forward to the appropriate application . For this I need a cross-platform way of doing two things:

-That the app is able (through a user-initiated preference) to map these URLs to itself.
-That the app is then able to open an arbitrary application with the URL (defined in prefs)

The ideal here would be to know if the system exposes the “secondary handler” (or default) that goes into effect if you delete the primary handler, but something tells me this isn’t currently doable.

This is for a sort of download manager, by the way. An app that only logs and analyzes download activity but doesn’t do the downloads itself.

[quote=87982:@Eduardo Gutierrez de Oliveira]I thought I might be lucky and have a solution for this in the thread about weblocs but no.

I need to catch some URLs for pre-processing, and then forward to the appropriate application . For this I need a cross-platform way of doing two things:

-That the app is able (through a user-initiated preference) to map these URLs to itself.
-That the app is then able to open an arbitrary application with the URL (defined in prefs)

The ideal here would be to know if the system exposes the “secondary handler” (or default) that goes into effect if you delete the primary handler, but something tells me this isn’t currently doable.

This is for a sort of download manager, by the way. An app that only logs and analyzes download activity but doesn’t do the downloads itself.[/quote]

On Mac :

open -a firefox /Users/Mitch/Desktop/a.webloc or open -a firefox http://fontmenu.com

This opens the URL shortcut a.webloc or the FontMenu web site with Firefox. My system uses Chrome as default.

On Windows :

Start Chrome.exe "http://Fontmenu.com"

This opens a URL with Chrome. My system uses IE as default.

Hope this helps.

[quote=87986:@Michel Bujardet]On Mac :

open -a firefox /Users/Mitch/Desktop/a.webloc or open -a firefox http://fontmenu.com
This opens the URL shortcut a.webloc or the FontMenu web site with Firefox. My system uses Chrome as default.
On Windows :

Start Chrome.exe "http://Fontmenu.com"

This opens a URL with Chrome. My system uses IE as default.
Hope this helps.[/quote]

Nice. I hadn’t realized the shell launcher would have the necessary paths already.

I guess I’ll have to provide the user for a way to map specific URLs to applications, since I’d be taking over them as default handler. At least for Windows and Linux.

I found two functions are very useful for this on Mac, and they’re both available in Macoslib:

FindAppsForURL will list all applications capable of handling a specific URL Scheme or a specific file extension.
SendURLToApplication will open a URL with the specified application.

I’ll check if I can find equivalent os-specific calls for Windows. If I can avoid calling the shell it would be great :slight_smile:

I recall the API being able to launch a specific program with arguments like the shell does from my VB days. Now I don’t have the documentation at hand anymore. And no way to open the VB sources from that era, even if I had kept them :confused:

A quick search turned this out which seems to do what you want :
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx

[quote=87992:@Michel Bujardet]I recall the API being able to launch a specific program with arguments like the shell does from my VB days. Now I don’t have the documentation at hand anymore. And no way to open the VB sources from that era, even if I had kept them :confused:

A quick search turned this out which seems to do what you want :
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx[/quote]

I’ve checked in WFS without finding anything and I’m hesitant to use Google since I’m not 100% clear on declares on Windows and what is off-limits (if anything).

The link I gave you is on the Microsoft Developer Network and it describes the system call needed to do what you want, I believe. Now you need someone knowledgeable enough in declares to implement it for you.

I do not understand what could be off limits with declares if they are documented by Microsoft itself for their developers.

If it where me, I would not bother anyway : I frankly do not see what is your issue with shell. What the command start does is probably calling CreateProcess. Now if you can save yourself countless hours of research and implementation by using a shell, why insist on doing otherwise ? Don’t tell me its a question of speed : starting a program will always take more time, let alone because ultimately the program files have to come from a disk. A 50th of a second more will not make much difference…

This is going to sound idiotic but I actually missed the link you wrote. And I even quoted it! I’m sorry.

I meant that since I’m not familiar I wouldn’t be able to easily see if I’m using a framework or functions deprecated or frowned upon (I know this is less of an issue in Windows than in Mac) or one that is only supported only in recent versions.

I have no problems with the shell. I mentioned I wanted to avoid it this time. This is because my last large project was so shell-intensive that I ended up writing my own console helper that in turn called all the shell commands I needed. It was a self-imposed limitation, just to force myself to learn something new.

In this case, because I have no urgency and it would be rewarding to know a bit more about something I’m not familiar with :slight_smile:

I don’t think I would’ve even thought about this being a “plus”. I like and use the shell so much I have to force myself to not rely on it as after a while everything starts to look like a nail and the first way I think of solving stuff is piping dozens of shell commands…:smiley:

I should mention this is a hobby project, so there’s no requirements other than what I impose myself. And there’s no rush to deliver (nor benefit if I do). I originally thought of avoiding shell commands to ease putting the app in the MAS and then I just thought to do the same in Windows as well.