Skip to content

Commit aac20ca

Browse files
committed
Added option to create shortcut in windows 'Send to'
1 parent 5196f9a commit aac20ca

File tree

6 files changed

+117
-10
lines changed

6 files changed

+117
-10
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,7 @@ dmypy.json
243243

244244
# End of https://www.toptal.com/developers/gitignore/api/python,pycharm
245245

246-
*.xml
246+
*.xml
247+
248+
# specific files
249+
test.py

README.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,53 @@ pip install qrshare
1818

1919
### Send to
2020

21+
> Windows only
22+
23+
Creating a shortcut in `shell:sendto` provides for easier use of convenience
24+
25+
#### commandline
26+
27+
```bash
28+
qrshare config --sendto
29+
```
30+
31+
#### manually
32+
2133
Press `Windows` + `r` and enter `shell:sendto`
2234

23-
> C:\Users\(user)\AppData\Roaming\Microsoft\Windows\SendTo
35+
> %USERPROFILE%\AppData\Roaming\Microsoft\Windows\SendTo
2436
25-
Create shortcut with command `qrshare` in folder
37+
Create shortcut with command `qrshare serve` in folder
2638

2739
now option qrshare should appear when you right click to a file
2840

2941
### Commandline
3042

43+
> `qrshare --help`
44+
3145
```bash
32-
qrshare --help
46+
Usage: __main__.py [OPTIONS] COMMAND [ARGS]...
47+
48+
Options:
49+
--help Show this message and exit.
50+
51+
Commands:
52+
config change user configurations
53+
serve serve given list paths as per given options
3354
```
3455

56+
> `qrshare config --help`
57+
3558
```bash
36-
Usage: __main__.py [OPTIONS] PATHS...
59+
Options:
60+
--sendto reset windows 'Send To' shortcut
61+
--help Show this message and exit.
62+
```
63+
64+
> `qrshare serve --help`
3765
38-
Options:****
66+
```bash
67+
Options:
3968
-p, --password TEXT when provided every device require authentication
4069
--port INTEGER waitress server port
4170
--help Show this message and exit.

qrshare/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .auth import Authentication
22
from .app import App
3+
from .shortcut import create_shortcut
34

45
__version__ = '0.3.3'

qrshare/__main__.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22

33
import click
44

5-
from qrshare import App
5+
from qrshare import App, create_shortcut
66

77

8-
@click.command()
8+
@click.group()
9+
def cli():
10+
pass
11+
12+
13+
@cli.command()
914
@click.argument('paths', nargs=-1, required=True)
1015
@click.option('-p', '--password', type=str, help='when provided every device require authentication')
1116
@click.option('--port', default=4000, type=int, help='waitress server port')
1217
def serve(paths, password, port):
18+
"""
19+
serve given list paths as per given options
20+
"""
1321
try:
1422
app = App([Path(path).resolve() for path in paths], password, port)
1523

@@ -24,5 +32,25 @@ def serve(paths, password, port):
2432
input('Press enter to exit')
2533

2634

35+
@cli.command()
36+
@click.option('--sendto', is_flag=True, help='reset windows \'Send To\' shortcut')
37+
def config(sendto):
38+
"""
39+
change user configurations
40+
"""
41+
if sendto:
42+
# windows send to path
43+
path = Path.home() / Path(r'AppData\Roaming\Microsoft\Windows\SendTo')
44+
45+
print(f'Creating shortcut with command \'qrshare\' in {path}...', end=' ')
46+
47+
try:
48+
create_shortcut(path, 'qrshare', 'serve')
49+
except Exception as e:
50+
print(e)
51+
else:
52+
print('done')
53+
54+
2755
if __name__ == '__main__':
28-
serve()
56+
cli()

qrshare/shortcut.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import subprocess
2+
from pathlib import Path
3+
import platform
4+
5+
base_batch_script = r"""
6+
@echo off
7+
8+
set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
9+
10+
echo Set oWS = WScript.CreateObject("WScript.Shell") > %SCRIPT%
11+
echo sLinkFile = "{dest}\{command}.lnk" >> %SCRIPT%
12+
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
13+
echo oLink.TargetPath = "{command}" >> %SCRIPT%
14+
echo oLink.Arguments = "{args}" >> %SCRIPT%
15+
echo oLink.Save >> %SCRIPT%
16+
17+
cscript /nologo %SCRIPT%
18+
del %SCRIPT%
19+
"""
20+
21+
22+
def create_shortcut(dest: Path, command: str, args: str = ''):
23+
"""
24+
A minimal function that creates a shortcut at the given destination
25+
26+
:supports: Windows
27+
28+
:param dest: shortcut destination directory
29+
:param command: shortcut command, maybe a file or callable command
30+
:param args: commandline arguments
31+
:return: None
32+
"""
33+
if platform.system() == 'Windows':
34+
script = base_batch_script.format(dest=str(dest), command=command, args=args)
35+
36+
# create temp bat file
37+
script_file = Path.home() / '.qrshare_shortcut_script.bat'
38+
with script_file.open('w') as f:
39+
f.write(script)
40+
41+
subprocess.run(str(script_file), stdout=subprocess.PIPE)
42+
43+
# delete temp batch file
44+
script_file.unlink()
45+
else:
46+
raise Exception(f'{platform.system()} is not supported')

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
long_description_content_type='text/markdown',
1919

2020
entry_points={
21-
"console_scripts": ["qrshare=qrshare.__main__:serve"]
21+
"console_scripts": ["qrshare=qrshare.__main__:cli"]
2222
},
2323
install_requires=requirements,
2424
package_data={

0 commit comments

Comments
 (0)