Skip to content

Commit

Permalink
macOS: Change the way wallpaper is set
Browse files Browse the repository at this point in the history
Remove additional macOS specific requirements
Remove the additional setup from ``setup.py``
Use ``subprocess`` and ``osascript`` to set and
get wallpaper on macOS

Fix NITDgpOS#326
  • Loading branch information
nkprince007 authored and abh3po committed Jan 11, 2017
1 parent 8a178bc commit b256bff
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 25 deletions.
1 change: 0 additions & 1 deletion mac-requirements.txt

This file was deleted.

1 change: 0 additions & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[pytest]
testpaths=tests/
addopts='--cov'

6 changes: 1 addition & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Module to perform build, package and install."""

import sys
import os
import json

Expand Down Expand Up @@ -55,10 +54,7 @@ def get_requirements(filename):
with open(settings_file_path, "w") as settings_file:
settings_file.write(json.dumps(file_data, indent=4, sort_keys=True))

requirements = []
requirements += get_requirements('requirements.txt')
if sys.platform.startswith('darwin'):
requirements += get_requirements('mac-requirements.txt')
requirements = get_requirements('requirements.txt')

setup(
# Name of application:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_wallpaper.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ def test_wallpaper_osx(self): # pragma: no cover
with tempfile.NamedTemporaryFile(mode="wb") as temp_wallpaper:
wallpaper.set(temp_wallpaper.name)
self.assertEqual(
wallpaper.get()[0], "/private"+temp_wallpaper.name)
wallpaper.get()[0], temp_wallpaper.name)
wallpaper.set(current_wallpaper)
35 changes: 18 additions & 17 deletions uiplib/Wallpaper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import sys

from subprocess import Popen, PIPE


class Wallpaper:
"""Wallpaper Class that holds set and get functions."""
Expand Down Expand Up @@ -38,14 +40,14 @@ def set_wallpaper_windows(self, filename): # pragma: no cover

def set_wallpaper_osx(self, filename): # pragma: no cover
"""Set a file as OSX Wallpaper."""
from appscript import app, mactypes # use applescript modules
command = ("osascript -e 'tell application \"System Events\" "
"to set picture of every desktop to \"{}\"'"
.format(filename))
process = Popen(command, stdout=PIPE, stderr=PIPE, shell=True)
_, error = process.communicate()

se = app('System Events') # fetch system events
desktops = se.desktops.display_name.get() # get all available displays
for d in desktops:
desk = se.desktops[d]
# set wallpaper for each display
desk.picture.set(mactypes.File(filename))
if len(error) != 0:
raise SystemExit(error.decode("utf-8"))

def get(self):
"""Get file address of current Wallpaper."""
Expand Down Expand Up @@ -78,13 +80,12 @@ def get_wallpaper_windows(self): # pragma: no cover

def get_wallpaper_osx(self): # pragma: no cover
"""Get current osx Wallpaper."""
from appscript import app, mactypes # use applescript modules

se = app('System Events') # fetch system events
desktops = se.desktops.display_name.get() # get all available displays
wallpaper_set = set([])
for d in desktops:
desk = se.desktops[d]
# set wallpaper for each display
wallpaper_set.add(desk.picture.get())
return list(wallpaper_set)
command = ("osascript -e 'tell application \"System Events\" "
"to get picture of every desktop'")
process = Popen(command, stdout=PIPE, stderr=PIPE, shell=True)
output, error = process.communicate()

if len(error) != 0:
raise SystemExit(error.decode("utf-8"))

return output.decode("utf-8").splitlines()

0 comments on commit b256bff

Please sign in to comment.