-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
71 lines (57 loc) · 1.74 KB
/
utils.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
"""
utility functions for raycast script commands
"""
import subprocess
import sys
def exit_if_not_install(converter: str) -> None:
"""exit if ImageMagick or FFMpegis not installed"""
try:
subprocess.run([converter, "-version"], capture_output=True, text=True)
except FileNotFoundError:
print(f"{converter} is not installed")
sys.exit(1)
def is_format_supported(path: str) -> bool:
"""check if a file is supported by ImageMagick"""
process = subprocess.run(
["magick", "identify", path], capture_output=True, text=True
)
return process.returncode == 0
def get_finder_items() -> list[str]:
"""get selected files in Finder"""
items = (
subprocess.run(
["osascript", "get-finder-items.applescript"],
capture_output=True,
text=True,
)
.stdout.strip()
.split("\n")
)
# remove empty items
items = [item for item in items if item]
if not items:
print("No files selected in Finder")
sys.exit(1)
return items
def get_image_size(path: str) -> str:
"""get image width and height, in format of WxH"""
process = subprocess.run(
["magick", "identify", "-format", "%wx%h", path],
capture_output=True,
text=True,
)
if process.returncode != 0:
print("An error occurred while getting image size")
sys.exit(1)
return process.stdout.strip()
def get_folder() -> str:
"""get current folder in Finder"""
folder = subprocess.run(
["osascript", "get-folder.applescript"],
capture_output=True,
text=True,
).stdout.strip()
if not folder:
print("No folder selected in Finder")
sys.exit(1)
return folder