Skip to content

Instantly share code, notes, and snippets.

@aerobounce
Created July 28, 2022 18:22
Show Gist options
  • Save aerobounce/3312ad4a61d8fd8c4a8c6fb31387da9b to your computer and use it in GitHub Desktop.
Save aerobounce/3312ad4a61d8fd8c4a8c6fb31387da9b to your computer and use it in GitHub Desktop.
Python script that generates a simple application that executes a script.
#!/usr/bin/env python3
from os import chmod, makedirs, stat, system
from stat import S_IXGRP, S_IXOTH, S_IXUSR
from sys import argv
# Application info
APP_NAME = "Test"
VERSION = "1.0.0"
BUNDLE_IDENTIFIER = f"io.github.aerobounce.{APP_NAME}"
COPYRIGHT = "Copyright © 2020 aerobounce. All rights reserved."
# Script that the app generated will execute
MAIN_SCRIPT = """#!/usr/bin/env swift
print("Hello World!")
"""
# Paths
CWD = "/".join(argv[0].split("/")[:-1])
APP_PACKAGE = CWD + f"/{APP_NAME}.app"
BINARY = APP_PACKAGE + f"/Contents/MacOS/{APP_NAME}"
# Info.plist
#
# Other useful keys can be used here, such as:
# CFBundleDocumentTypes, UTImportedTypeDeclarations,
# NSAppleScriptEnabled, LSUIElement ...
#
PLIST = 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>{APP_NAME}</string>
<key>CFBundleIdentifier</key>
<string>{BUNDLE_IDENTIFIER}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>{APP_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>{APP_NAME + " " + VERSION}</string>
<key>CFBundleVersion</key>
<string>{VERSION}</string>
<key>NSHumanReadableCopyright</key>
<string>{COPYRIGHT}</string>
</dict>
</plist>
"""
def main():
_ = system(f"rm -rf {APP_PACKAGE}")
makedirs(APP_PACKAGE + "/Contents/MacOS")
with open(APP_PACKAGE + "/Contents/Info.plist", "w") as f:
_ = f.write(PLIST)
with open(APP_PACKAGE + "/Contents/PkgInfo", "w") as f:
_ = f.write("APPL????")
with open(BINARY, "w") as f:
_ = f.write(MAIN_SCRIPT)
old_mode = stat(BINARY).st_mode
chmod(BINARY, old_mode | S_IXUSR | S_IXGRP | S_IXOTH)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment