-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaliaser.py
More file actions
40 lines (31 loc) · 1.29 KB
/
aliaser.py
File metadata and controls
40 lines (31 loc) · 1.29 KB
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
#!/usr/bin/env python3
import os
def make_alias (filepath: str, alias_dirpath: str, alias_name: str):
"""
Create a Unix alias of filepath: AppleScript command run through macOS command line
Partially adapted from https://apple.stackexchange.com/questions/278117/how-to-use-applescript-in-a-bash-script-to-create-an-alias-for-an-app
:param parent_path: original filepath
:param child_path: alias dirpath
:param alias_name: alias name
:return: path of the created alias file
"""
# Build alias path
alias_path = os.path.join (alias_dirpath, alias_name)
# Exception: original or target path not existing
if not os.path.exists (filepath) or not os.path.exists (alias_dirpath):
raise FileNotFoundError
# Exception: alias already exists
if os.path.exists (alias_path):
return alias_path
# Generate command
applescript_command = \
f'set filePath to "{filepath}"\n' \
f'set aliasPath to "{alias_dirpath}"\n' \
f'tell application "Finder"\n' \
f'make new alias to POSIX file filePath at POSIX file aliasPath\n' \
f'set name of result to "{alias_name}"\n' \
f'end tell'
os_command = f'osascript -e \'{applescript_command}\''
# Run command
os.popen (os_command)
return alias_path