forked from corpnewt/MountEFI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reveal.py
executable file
·70 lines (65 loc) · 2.76 KB
/
reveal.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
import sys, os
sys.path.append(os.path.abspath(os.path.dirname(os.path.realpath(__file__))))
import run
class Reveal:
def __init__(self):
self.r = run.Run()
return
def get_parent(self, path):
return os.path.normpath(os.path.join(path, os.pardir))
def reveal(self, path, new_window = False):
# Reveals the passed path in Finder - only works on macOS
if not sys.platform == "darwin":
return ("", "macOS Only", 1)
if not path:
# No path sent - nothing to reveal
return ("", "No path specified", 1)
# Build our script - then convert it to a single line task
if not os.path.exists(path):
# Not real - bail
return ("", "{} - doesn't exist".format(path), 1)
# Get the absolute path
path = os.path.abspath(path)
command = ["osascript"]
if new_window:
command.extend([
"-e", "set p to \"{}\"".format(path.replace("\"", "\\\"")),
"-e", "tell application \"Finder\"",
"-e", "reveal POSIX file p as text",
"-e", "activate",
"-e", "end tell"
])
else:
if path == self.get_parent(path):
command.extend([
"-e", "set p to \"{}\"".format(path.replace("\"", "\\\"")),
"-e", "tell application \"Finder\"",
"-e", "reopen",
"-e", "activate",
"-e", "set target of window 1 to (POSIX file p as text)",
"-e", "end tell"
])
else:
command.extend([
"-e", "set o to \"{}\"".format(self.get_parent(path).replace("\"", "\\\"")),
"-e", "set p to \"{}\"".format(path.replace("\"", "\\\"")),
"-e", "tell application \"Finder\"",
"-e", "reopen",
"-e", "activate",
"-e", "set target of window 1 to (POSIX file o as text)",
"-e", "select (POSIX file p as text)",
"-e", "end tell"
])
return self.r.run({"args" : command})
def notify(self, title = None, subtitle = None, sound = None):
# Sends a notification
if not title:
return ("", "Malformed dict", 1)
# Build our notification
n_text = "display notification with title \"{}\"".format(title.replace("\"", "\\\""))
if subtitle:
n_text += " subtitle \"{}\"".format(subtitle.replace("\"", "\\\""))
if sound:
n_text += " sound name \"{}\"".format(sound.replace("\"", "\\\""))
command = ["osascript", "-e", n_text]
return self.r.run({"args" : command})