Problem creating a shorcut

M

Mike Driscoll

Hi,

I've had this niggling issue from time to time. I want to create a
shortcut on the user's desktop to a website that specifically loads
Firefox even if Firefox is not the default browser.

I usually use COM as it allows very specific settings of the shortcut,
such as the Working Directory and the Target Path. However, the
following will not work for some reason:

<code>

import win32com.client
import winshell

shell = win32com.client.Dispatch('WScript.Shell')
userDesktop = winshell.desktop()

shortcut = shell.CreateShortCut(userDesktop + '\\MyShortcut.lnk')
shortcut.Targetpath = r'"C:\Program Files\Mozilla Firefox\firefox.exe"
https:\www.myCompanyWebsite.com\auth\preauth.php'
shortcut.WorkingDirectory = r'C:\Program Files\Mozilla
Firefox'
shortcut.save()

</code>

This creates the following target path (which doesn't work):

"C:\"C:\Program Files\Mozilla Firefox\firefox.exe" https:
\www.myCompanyWebsite.com\auth\preauth.php"

If I leave the website off, it works. If I leave the path to Firefox
out, it works too. Is there another method I can use other than
creating the shortcut by hand and using the shutil module?

Thank you for any ideas.

Mike
 
M

Mike Driscoll

Either you copied wrong or the problem is:

"C:\"C:\Program Files...

Note the C:\ is specified twice in the string. I think it should read:


-Larry

Yeah, I know that it's in there twice and that that is the problem.
But I'm not causing that extra C:\. I run it exactly as above and that
is what I get for the output. I think it's the COM object. Maybe I
better just re-post this to the PyWin32 list...

Thanks,

Mike
 
G

Gabriel Genellina

I've had this niggling issue from time to time. I want to create a
shortcut on the user's desktop to a website that specifically loads
Firefox even if Firefox is not the default browser.

I usually use COM as it allows very specific settings of the shortcut,
such as the Working Directory and the Target Path. However, the
following will not work for some reason:

Try this different approach, using the IShellLink interface:
http://aspn.activestate.com/ASPN/do...2/win32com.shell_and_Windows_Shell_Links.html
 
C

Chris

Hi,

I've had this niggling issue from time to time. I want to create a
shortcut on the user's desktop to a website that specifically loads
Firefox even if Firefox is not the default browser.

I usually use COM as it allows very specific settings of the shortcut,
such as the Working Directory and the Target Path. However, the
following will not work for some reason:

<code>

import win32com.client
import winshell

shell = win32com.client.Dispatch('WScript.Shell')
userDesktop = winshell.desktop()

shortcut = shell.CreateShortCut(userDesktop + '\\MyShortcut.lnk')
shortcut.Targetpath = r'"C:\Program Files\Mozilla Firefox\firefox.exe"
https:\www.myCompanyWebsite.com\auth\preauth.php'
shortcut.WorkingDirectory = r'C:\Program Files\Mozilla
Firefox'
shortcut.save()

</code>

This creates the following target path (which doesn't work):

"C:\"C:\Program Files\Mozilla Firefox\firefox.exe" https:
\www.myCompanyWebsite.com\auth\preauth.php"

If I leave the website off, it works. If I leave the path to Firefox
out, it works too. Is there another method I can use other than
creating the shortcut by hand and using the shutil module?

Thank you for any ideas.

Mike

Don't set arguments in the path.

shortcut = shell.CreateShortCut(userDesktop + '\\MyShortcut.lnk')
shortcut.TargetPath = r'Program Files\Mozilla Firefox\firefox.exe'
shortcut.Arguments = r'https:\www.myCompanyWebsite.com\auth
\preauth.php'
shortcut.WorkingDirectory = r'C:\Program Files\Mozilla Firefox'
shortcut.save()
 
W

Wolfgang Draxinger

Mike said:
Hi,

I've had this niggling issue from time to time. I want to
create a shortcut on the user's desktop to a website that
specifically loads Firefox even if Firefox is not the default
browser.

I usually use COM as it allows very specific settings of the
shortcut, such as the Working Directory and the Target Path.
However, the following will not work for some reason:

<code>

import win32com.client
import winshell

shell = win32com.client.Dispatch('WScript.Shell')
userDesktop = winshell.desktop()

shortcut = shell.CreateShortCut(userDesktop +
'\\MyShortcut.lnk') shortcut.Targetpath = r'"C:\Program
Files\Mozilla Firefox\firefox.exe"
https:\www.myCompanyWebsite.com\auth\preauth.php'
shortcut.WorkingDirectory = r'C:\Program Files\Mozilla Firefox'
shortcut.save()

</code>

This creates the following target path (which doesn't work):

"C:\"C:\Program Files\Mozilla Firefox\firefox.exe" https:
\www.myCompanyWebsite.com\auth\preauth.php"

If I leave the website off, it works. If I leave the path to
Firefox out, it works too. Is there another method I can use
other than creating the shortcut by hand and using the shutil
module?

Thank you for any ideas.

I see four problems:

1) you should not hardcode the backslashes ('\'), instead use
os.sep for it.

2) In URIs there are no backslashes, only forward slashes. You
coded

https:\...

which is _WRONG_. URIs are <protocoll>://<host>/<resource>, where
for some protocolls <host> is empty (file protocoll e.g.).

3) You assume, that Firefox is always installed at C:\Program
Files\Mozilla Firefox\firefox.exe
However the path largely differs from system to system. On *nix
systems you normally have all programs in $PATH, so a non full
qualified path would be sufficient. On Windows this works, too,
_IF_ the installation directory of the to be used application
get's added to the PATH environment variable.

Wolfgang Draxinger
 
T

Tim Golden

Wolfgang said:
1) you should not hardcode the backslashes ('\'), instead use
os.sep for it.

With respect, the OP is creating a Windows desktop shortcut.
Unless Microsoft suddenly decide to change their use of the
backslash, I suggest that this is a needless generalisation.
3) You assume, that Firefox is always installed at C:\Program
Files\Mozilla Firefox\firefox.exe
However the path largely differs from system to system.

This is broadly true. However, it's fairly clear from the
phrase "the user's desktop" that the OP is working in some
kind of corporate environment where he can be fairly sure
where Firefox is installed by policy. In addition, it's not so
easy to find its path if it's not in the default place since apps
very rarely add their location to the system PATH these days
(and Firefox certainly doesn't). You could scan the registry for
its App Paths entry in the registry but I don't know if any other
way if it's not the default browser (which it's clear from the
original post it may not be).

Sorry to sound a bit negative, but I felt that a couple of your points,
while valid, were not altogether helpful to the situation the OP was in.

TJG
 
T

Tim Golden

Wolfgang said:
1) you should not hardcode the backslashes ('\'), instead use
os.sep for it.

With respect, the OP is creating a Windows desktop shortcut.
Unless Microsoft suddenly decide to change their use of the
backslash, I suggest that this is a needless generalisation.
3) You assume, that Firefox is always installed at C:\Program
Files\Mozilla Firefox\firefox.exe
However the path largely differs from system to system.

This is broadly true. However, it's fairly clear from the
phrase "the user's desktop" that the OP is working in some
kind of corporate environment where he can be fairly sure
where Firefox is installed by policy. In addition, it's not so
easy to find its path if it's not in the default place since apps
very rarely add their location to the system PATH these days
(and Firefox certainly doesn't). You could scan the registry for
its App Paths entry in the registry but I don't know if any other
way if it's not the default browser (which it's clear from the
original post it may not be).

Sorry to sound a bit negative, but I felt that a couple of your points,
while valid, were not altogether helpful to the situation the OP was in.

TJG
 
M

Mike Driscoll

I see four problems:

1) you should not hardcode the backslashes ('\'), instead use
os.sep for it.

2) In URIs there are no backslashes, only forward slashes. You
coded

https:\...

which is _WRONG_. URIs are <protocoll>://<host>/<resource>, where
for some protocolls <host> is empty (file protocoll e.g.).


That was an accident...my original code was correct, but I stupidly
decided to generalize my website's name and put the wrong slashes in.

3) You assume, that Firefox is always installed at C:\Program
Files\Mozilla Firefox\firefox.exe
However the path largely differs from system to system. On *nix
systems you normally have all programs in $PATH, so a non full
qualified path would be sufficient. On Windows this works, too,
_IF_ the installation directory of the to be used application
get's added to the PATH environment variable.


I don't assume it at all. At my place of business, that's where
Firefox is. If it's not installed there when the user logs in, one of
my scripts installs it automatically.
 
M

Mike Driscoll

Don't set arguments in the path.

shortcut = shell.CreateShortCut(userDesktop + '\\MyShortcut.lnk')
shortcut.TargetPath = r'Program Files\Mozilla Firefox\firefox.exe'
shortcut.Arguments = r'https:\www.myCompanyWebsite.com\auth
\preauth.php'
shortcut.WorkingDirectory = r'C:\Program Files\Mozilla Firefox'
shortcut.save()

Ah. I was unaware of the Arguments parameter. That works quite well.
Where does one find this information anyway? I think I found mine from
bits and pieces scattered in various tutorials.

Thanks a lot.

Mike
 
M

Mike Driscoll

I use Inno Setup on most of my applications and use it to create my desktop,
quicklaunch and Start shortcuts.  Would that be an option for you?

-Larry

I use Inno too for my applications. However, in this case, I have to
create the shortcut as part of the login process based on what group
the user is in. Chris's solution worked for my needs and I think
Gabriel's will work too, if I figure it out.

Mike
 
T

Tim Golden

Duncan said:
It's not too hard to get the required command line from the registry:

import _winreg
print _winreg.QueryValue(_winreg.HKEY_CLASSES_ROOT,
'FirefoxURL\shell\open\command')

and of course it throws an exception if firefox isn't installed.

Then just replace %1 with the url to get the actual command you need.

That's neat. You learn something new...

TJG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top