-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudacity_present.py
executable file
·120 lines (94 loc) · 2.77 KB
/
audacity_present.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
114
115
116
117
118
119
120
#!/usr/bin/env python
import os
import subprocess
import time
import psutil
import typer
import audacity_funcs as af
"""
audacity_present.py
"""
def is_audacity_window_open():
"""
Checks whether Audacity window is open.
"""
script = """
tell application "System Events"
set audacityWindows to (name of windows of process "Audacity")
if length of audacityWindows is greater than 0 then
return true
else
return false
end if
end tell
"""
result = subprocess.run(["osascript", "-e", script], capture_output=True, text=True)
return result.stdout.strip() == "true"
def is_audacity_running():
"""
Returns true if Audacity is running.
"""
for proc in psutil.process_iter(["pid", "name"]):
try:
# Check if the process name is "Audacity"
if "Audacity" in proc.info["name"]:
return True
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
return False
def start_audacity():
"""
Starts Audacity.
"""
os.system('open -a "Audacity"')
def bring_audacity_window_to_front_as():
"""
Brings Audacity window to the front and opens new project
using AppleScript.
"""
script = """
tell application "Audacity"
activate
end tell
tell application "System Events"
-- Wait a bit for Audacity to become active
delay 1
-- Simulate Cmd+N to open a new project
keystroke "n" using {command down}
end tell
"""
subprocess.run(["osascript", "-e", script])
def close_audacity_window_as():
script = """
tell application "Audacity" to activate
tell application "System Events"
keystroke "w" using command down
end tell
"""
subprocess.run(["osascript", "-e", script])
def assert_audacity_running(verbose: bool = True):
if is_audacity_running():
if verbose:
print("Audacity is running.")
else:
if verbose:
print("Audacity is not running. Starting it.")
start_audacity()
time.sleep(2) # give it time to start
def assert_audacity_window(verbose: bool = True):
if is_audacity_window_open() and af.is_project_empty():
if verbose:
print("An Audacity window is open. Will use this.")
else:
if verbose:
print("Bringing Audacity window to the front with a new project.")
bring_audacity_window_to_front_as()
time.sleep(1) # give it time
def assert_audacity(verbose: bool = True):
assert_audacity_running(verbose)
assert_audacity_window(verbose)
def main():
print("This main is just for testing purposes.")
assert_audacity()
if __name__ == "__main__":
typer.run(main)