This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
splsh.py
113 lines (90 loc) · 3.35 KB
/
splsh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -*- coding: utf-8 -*-
"""
pySplash – Wallpaper for your Mac
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ver: 0.1.1
url: http://egregors.com/lab/pySplash
source: https://github.com/Egregors/pySplash
img source: https://unsplash.it/
Unbelievable simple app to set the desktop random picture from
https://unsplash.com
Required:
- PyObjC
- Python 2.6+
- rumps (see: https://github.com/jaredks/rumps)
To build mac bundle use:
```python setup.py py2app```
"""
import os
import shutil
import urllib
import datetime
import rumps
from AppKit import NSScreen, NSWorkspace
from Foundation import NSURL
class SplshApp(rumps.App):
def __init__(self):
super(SplshApp, self).__init__('Splsh')
self.icon = 'img/icon.png'
try:
self.screen_width = int(NSScreen.mainScreen().frame().size.width)
self.screen_height = int(NSScreen.mainScreen().frame().size.height)
except:
self.screen_width = 1024
self.screen_height = 768
self.menu = [
'x'.join([str(self.screen_width), str(self.screen_height)]),
None,
rumps.MenuItem('Next', key='n'),
None,
rumps.MenuItem('Gray Mood'),
rumps.MenuItem('Blur'),
rumps.MenuItem('Clear Cache', key='c'),
None,
]
# Path to dir for downloaded images
self.media_dir = 'media/'
# Extra url parameters
self.gray_mood = False
self.blur = False
@rumps.clicked('Next')
def next_image(self, _):
if not os.path.exists(self.media_dir):
os.makedirs(self.media_dir)
url = 'https://unsplash.it/'
if self.gray_mood: url += 'g/'
url += '{w}/{h}/?random'
if self.blur: url += '&blur'
url = url.format(w=self.screen_width, h=self.screen_height)
file_name = self.media_dir + datetime.datetime.now().strftime("%H:%M:%S.%f") + '.jpg'
try:
self.icon = 'img/wait.png'
urllib.urlretrieve(url, file_name)
file_url = NSURL.fileURLWithPath_(file_name)
# Get shared workspace
ws = NSWorkspace.sharedWorkspace()
# Iterate over all screens
for screen in NSScreen.screens():
# Tell the workspace to set the desktop picture
(result, error) = ws.setDesktopImageURL_forScreen_options_error_(
file_url, screen, {}, None)
self.icon = 'img/icon.png'
except IOError:
print('Service unavailable, check your internet connection.')
rumps.alert(title='Connection Error', message='Service unavailable\n'
'Please, check your internet connection')
@rumps.clicked('Gray Mood')
def is_gray_mood(self, sender):
self.gray_mood, sender.state = not self.gray_mood, not sender.state
@rumps.clicked('Blur')
def is_blur(self, sender):
self.blur, sender.state = not self.blur, not sender.state
@rumps.clicked('Clear Cache')
def clear_cache(self, _):
""" Remove dir with all downloaded images.
"""
if os.path.exists(self.media_dir):
print('Removing MEDIA_DIR...')
shutil.rmtree(self.media_dir)
if __name__ == '__main__':
app = SplshApp().run()