Skip to content

Commit

Permalink
Add OSX Screenshot with screencapture (#457)
Browse files Browse the repository at this point in the history
* osx screenshot added

* setter and getter for file_path

* Update screenshot.py

* Update screenshot.py

* changing method take_shot() to take()

* Fix README.rst

* Fix Screenshot facade

* Purge non-existing method from wifi facade

* Set default path to /Users/<user>/Pictures folder, remote hardcoded timeout

* Fix Screenshot example

* Fix plyer __init__.py

* Fix typos and missing values

* Fix missing import, protocol <-> path error in 'screencapture'
  • Loading branch information
KeyWeeUsr authored Oct 21, 2018
1 parent 3496bca commit bf40f6d
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Notifications X X X X
Number of Processors X
Orientation X
Proximity X
Screenshot X
Sms (send messages) X X
Spatial Orientation X X
Storage Path X X X X X
Expand Down
34 changes: 34 additions & 0 deletions examples/screenshot/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout


Builder.load_string('''
#:import screenshot plyer.screenshot
<ScreenshotDemo>:
orientation: 'vertical'
padding: '50dp'
spacing: '20dp'
Label:
size_hint_y: None
height: sp(40)
text: 'Screenshot Location: ' + str(screenshot.file_path)
Button:
text: 'Capture Screenshot'
on_release: screenshot.capture()
''')


class ScreenshotDemo(BoxLayout):
'''Root Widget.'''


class ScreenshotApp(App):

def build(self):
return ScreenshotDemo()


if __name__ == "__main__":
ScreenshotApp().run()
5 changes: 4 additions & 1 deletion plyer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'gyroscope', 'irblaster', 'light', 'orientation', 'notification',
'proximity', 'sms', 'tts', 'uniqueid', 'vibrator', 'wifi',
'temperature', 'humidity', 'spatialorientation', 'brightness',
'storagepath', 'processors', 'bluetooth')
'storagepath', 'processors', 'bluetooth', 'screenshot')

__version__ = '1.3.2.dev0'

Expand Down Expand Up @@ -110,3 +110,6 @@

#: Processors proxy to :class:`plyer.facades.Processors`
processors = Proxy('processors', facades.Processors)

#: Screenshot proxy to :class:`plyer.facades.Screenshot`
screenshot = Proxy('screenshot', facades.Screenshot)
3 changes: 2 additions & 1 deletion plyer/facades/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
'IrBlaster', 'Light', 'Orientation', 'Notification', 'Proximity',
'Sms', 'TTS', 'UniqueID', 'Vibrator', 'Wifi', 'Flash',
'Temperature', 'Humidity', 'SpatialOrientation', 'Brightness',
'Processors', 'StoragePath', 'keystore', 'Bluetooth')
'Processors', 'StoragePath', 'keystore', 'Bluetooth', 'Screenshot')

from plyer.facades.accelerometer import Accelerometer
from plyer.facades.audio import Audio
Expand Down Expand Up @@ -44,3 +44,4 @@
from plyer.facades.storagepath import StoragePath
from plyer.facades.bluetooth import Bluetooth
from plyer.facades.processors import Processors
from plyer.facades.screenshot import Screenshot
57 changes: 57 additions & 0 deletions plyer/facades/screenshot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'''
Screenshot
==========
The :class:`Screenshot` is used for capturing a digital image of what
is currently visible on the monitor.
The default path for taking screenshot is set in each platform implementation.
Simple Examples
---------------
To get the file path::
>>> screenshot.file_path
'/sdcard/test.jpg'
To set the file path::
>>> screenshot.file_path = '/Users/OSXUser/Pictures/screenshot.png'
To take screenshot::
>>> from plyer import screenshot
>>> screenshot.capture()
'''


class Screenshot(object):
'''
Screenshot facade.
'''

_file_path = ''

def __init__(self, file_path=None):
self._file_path = file_path

def capture(self):
self._capture()

@property
def file_path(self):
return self._file_path

@file_path.setter
def file_path(self, location):
'''
Location of the screenshot.
'''

self._file_path = location

# private

def _capture(self, **kwargs):
raise NotImplementedError()
27 changes: 27 additions & 0 deletions plyer/platforms/macosx/screenshot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import subprocess
from os.path import join
from plyer.facades import Screenshot
from plyer.utils import whereis_exe
from plyer.platforms.macosx.storagepath import OSXStoragePath


class OSXScreenshot(Screenshot):
def __init__(self, file_path=None):
default_path = join(
OSXStoragePath().get_pictures_dir().replace('file://', ''),
'screenshot.png'
)
super(OSXScreenshot, self).__init__(file_path or default_path)

def _capture(self):
subprocess.call([
'screencapture',
self.file_path
])


def instance():
if whereis_exe('screencapture'):
return OSXScreenshot()
else:
return Screenshot()

0 comments on commit bf40f6d

Please sign in to comment.