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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
#!/usr/bin/env python
"""
Create desktop shortcuts for Darwin / MacOS
"""
import os
import sys
import shutil
import subprocess
from pathlib import Path
from collections import namedtuple
from .utils import get_pyexe, get_homedir
def get_startmenu():
"get start menu location"
return ''
def get_desktop():
"get desktop location"
return Path(get_homedir(), 'Desktop').resolve().as_posix()
def get_folders():
"""get user-specific folders
Returns:
-------
Named tuple with fields 'home', 'desktop', 'startmenu'
Example:
-------
>>> from pyshortcuts import get_folders
>>> folders = get_folders()
>>> print("Home, Desktop, StartMenu ",
... folders.home, folders.desktop, folders.startmenu)
"""
UserFolders = namedtuple("UserFolders", ("home", "desktop", "startmenu"))
return UserFolders(get_homedir(), get_desktop(), get_startmenu())
def make_shortcut(script, name=None, description=None, icon=None, working_dir=None,
folder=None, terminal=True, desktop=True,
startmenu=False, executable=None, noexe=False):
"""create shortcut
Arguments:
---------
script (str) path to script, may include command-line arguments
name (str, None) name to display for shortcut [name of script]
description (str, None) longer description of script [`name`]
icon (str, None) path to icon file [python icon]
working_dir (str, None) directory where to run the script in
folder (str, None) subfolder of Desktop for shortcut [None] (See Note 1)
terminal (bool) whether to run in a Terminal [True]
desktop (bool) whether to add shortcut to Desktop [True]
startmenu (bool) whether to add shortcut to Start Menu [False] (See Note 2)
executable (str, None) name of executable to use [this Python] (see Note 3)
noexe (bool) whether to use no executable (script is entire command) [False]
Notes:
------
1. `folder` will place shortcut in a subfolder of Desktop and/or Start Menu
2. Start Menu does not exist for Darwin / MacOSX
3. executable defaults to the Python executable used to make shortcut.
"""
if not desktop:
return None
userfolders = get_folders()
if working_dir is None:
working_dir = ''
from .shortcut import shortcut
print(f" A: {executable=} {noexe=}")
scut = shortcut(script, userfolders, name=name, description=description,
working_dir=working_dir, folder=folder, icon=icon)
if noexe:
full_script =scut.script
executable = ''
else:
full_script = scut.full_script
if executable is None:
executable = get_pyexe()
print(f" B: {executable=} ")
executable = Path(executable).as_posix()
if Path(scut.full_script) == Path(executable):
executable = ''
print(f" C: {executable=} ")
print(f" : {sys.executable=} / {executable=}")
if not Path(scut.desktop_dir).exists():
os.makedirs(scut.desktop_dir)
osascript = f'{full_script} {scut.arguments}'
osascript = osascript.replace(' ', '\\ ')
dest = Path(scut.desktop_dir, scut.target).resolve().as_posix()
if Path(dest).exists():
shutil.rmtree(dest)
os.mkdir(dest)
os.mkdir(Path(dest, 'Contents'))
os.mkdir(Path(dest, 'Contents', 'MacOS'))
os.mkdir(Path(dest, 'Contents', 'Resources'))
opts = {'name': scut.name,
'desc': scut.description,
'script': full_script,
'workdir': scut.working_dir,
'args': scut.arguments,
'prefix': Path(sys.prefix).as_posix(),
'exe': executable,
'osascript': osascript}
info = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleGetInfoString</key> <string>{desc:s}</string>
<key>CFBundleName</key> <string>{name:s}</string>
<key>CFBundleExecutable</key> <string>{name:s}</string>
<key>CFBundleIconFile</key> <string>{name:s}</string>
<key>CFBundlePackageType</key> <string>APPL</string>
</dict>
</plist>
"""
# Build command string
if executable:
cmd = f"{executable} {full_script} {scut.arguments}"
else:
cmd = f"{full_script} {scut.arguments}"
# For GUI apps, create an Automator-style app that CrowdStrike trusts
if not terminal:
automator_dir = Path(dest, 'Contents', 'Resources')
workflow = f'''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>AMApplicationBuild</key>
<string>523</string>
<key>AMApplicationVersion</key>
<string>2.10</string>
<key>AMDocumentVersion</key>
<string>2</string>
<key>actions</key>
<array>
<dict>
<key>action</key>
<dict>
<key>AMActionVersion</key>
<string>1.0.2</string>
<key>AMApplication</key>
<array>
<string>Automator</string>
</array>
<key>AMParameterProperties</key>
<dict>
<key>source</key>
<dict/>
</dict>
<key>AMProvides</key>
<dict>
<key>Container</key>
<string>List</string>
<key>Types</key>
<array>
<string>com.apple.applescript.object</string>
</array>
</dict>
<key>ActionBundlePath</key>
<string>/System/Library/Automator/Run Shell Script.action</string>
<key>ActionName</key>
<string>Run Shell Script</string>
<key>ActionParameters</key>
<dict>
<key>COMMAND_STRING</key>
<string>{cmd} > /dev/null 2>&1 &</string>
<key>CheckedForUserDefaultShell</key>
<true/>
<key>inputMethod</key>
<integer>0</integer>
<key>shell</key>
<string>/bin/bash</string>
<key>source</key>
<string></string>
</dict>
<key>BundleIdentifier</key>
<string>com.apple.RunShellScript</string>
</dict>
</dict>
</array>
</dict>
</plist>'''
workflow_path = Path(automator_dir, 'document.wflow').as_posix()
with open(workflow_path, 'w') as f:
f.write(workflow)
# Update Info.plist for Automator app with stable bundle ID
import hashlib
# Create stable bundle ID based on app name and command
bundle_id_base = f"{scut.name}-{hashlib.md5(cmd.encode()).hexdigest()[:8]}"
info_automator = f'''<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
<string>Application Stub</string>
<key>CFBundleIconFile</key>
<string>{scut.name}</string>
<key>CFBundleIdentifier</key>
<string>com.pyshortcuts.{bundle_id_base}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>{scut.name}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>LSMinimumSystemVersion</key>
<string>10.5</string>
<key>LSUIElement</key>
<false/>
<key>NSMainNibFile</key>
<string>ApplicationStub</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
</plist>'''
with open(Path(dest, 'Contents', 'Info.plist'), 'w') as fout:
fout.write(info_automator)
# Copy Automator stub executable
automator_stub_paths = [
'/System/Library/CoreServices/Automator Application Stub.app/Contents/MacOS/Automator Application Stub',
'/System/Library/CoreServices/Automator Launcher.app/Contents/MacOS/Automator Launcher',
]
automator_stub = None
for stub_path in automator_stub_paths:
if Path(stub_path).exists():
automator_stub = stub_path
break
if not automator_stub:
raise FileNotFoundError(
'Could not find Automator Application Stub. '
'This may not be supported on your macOS version. '
'Please use terminal=True.'
)
stub_dest = Path(dest, 'Contents', 'MacOS', 'Application Stub').as_posix()
shutil.copy(automator_stub, stub_dest)
icon_dest = Path(dest, 'Contents', 'Resources', scut.name + '.icns').as_posix()
shutil.copy(scut.icon, icon_dest)
else:
# For terminal apps, use AppleScript to open Terminal
with open(Path(dest, 'Contents', 'Info.plist'), 'w') as fout:
fout.write(info.format(**opts))
# Create AppleScript launcher
cmd_escaped = cmd.replace('"', '\\"')
text = ['#!/usr/bin/osascript',
'tell application "Terminal"',
f' do script "{cmd_escaped}"',
'end tell',
'']
ascript_name = Path(dest, 'Contents', 'MacOS', scut.name).as_posix()
with open(ascript_name, 'w') as fout:
fout.write('\n'.join(text))
os.chmod(ascript_name, 0o755)
icon_dest = Path(dest, 'Contents', 'Resources', scut.name + '.icns').as_posix()
shutil.copy(scut.icon, icon_dest)
# Remove quarantine attributes so macOS will launch the app
try:
subprocess.run(['xattr', '-cr', dest], capture_output=True, check=False)
except Exception:
pass
# Try to ad-hoc sign to give it a stable code identity for TCC
if not terminal:
try:
subprocess.run(['codesign', '--force', '--deep', '--sign', '-', dest],
capture_output=True, check=False)
except Exception:
pass
return scut
|