Last active
March 5, 2023 03:09
-
-
Save scriptingosx/cbb17d052ebc0106ec66 to your computer and use it in GitHub Desktop.
Create OS X inetloc files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import argparse | |
import urlparse | |
import urllib | |
import plistlib | |
import os.path | |
import subprocess | |
def parse_rdp_scheme(rdp_scheme): | |
params = {} | |
# sample input: full%20address=s:host.example.com&username=s:samplename&screen%20mode%20id=i:1 | |
#first split on & | |
for x in rdp_scheme.split('&'): | |
#then split on ':' | |
p = urllib.unquote(x).split(':') | |
# then remove type indicator '=?' | |
k = p[0].split('=')[0] | |
params[k] = p[1] | |
return params | |
# parse arguments | |
parser = argparse.ArgumentParser(description="creates internet location files") | |
parser.add_argument('uri') | |
parser.add_argument('outfile', nargs='?') | |
parser.add_argument('--version', action='version', version='%(prog)s 0.1') | |
args = parser.parse_args() | |
#split the uri parameter | |
parsed_url = urlparse.urlparse(args.uri) | |
scheme = parsed_url.scheme | |
hostname = parsed_url.hostname | |
#error on empty scheme | |
if scheme == '': | |
sys.stderr.write("could not parse uri scheme!\n") | |
exit(1) | |
#choose right file extension for scheme, default to 'inetloc' | |
extensions = { 'http': 'webloc', 'https': 'webloc', 'ftp': 'ftploc', 'vnc': 'vncloc' } | |
file_extension = extensions.get(scheme, 'inetloc') | |
# special treatment for rdp scheme: | |
# https://technet.microsoft.com/en-us/library/dn690096.aspx | |
if scheme.lower() == 'rdp': | |
if parsed_url.netloc is '': | |
sys.stderr.write("could not parse rdp scheme!\n") | |
exit(1) | |
rdp_params = parse_rdp_scheme(parsed_url.netloc) | |
hostname = rdp_params.get('full address', None) | |
#no hostname!? error | |
if hostname is None: | |
sys.stderr.write("could not parse hostname!\n") | |
exit(1) | |
if args.outfile is None: | |
# create default name | |
filename = "%s %s.%s" %(scheme.upper(), hostname.split('.')[0], file_extension) | |
else: | |
filename = args.outfile | |
#append file extension if not given | |
if not filename.endswith(file_extension): | |
filename += '.' + file_extension | |
plist_root = {'URL': args.uri} | |
plistlib.writePlist(plist_root, filename) | |
#if the SetFile binary exists, hide the file extension | |
set_file_cmd="/usr/bin/SetFile" | |
if os.path.exists(set_file_cmd): | |
command=[set_file_cmd, '-a', 'E', filename] | |
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
(stdout, stderr) = proc.communicate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment