Skip to content

Instantly share code, notes, and snippets.

Last active March 5, 2023 03:09
What would you like to do?
Create OS X inetloc files
import sys
import argparse
import urlparse
import urllib
import plistlib
import os.path
import subprocess
def parse_rdp_scheme(rdp_scheme):
params = {}
# sample input:
#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('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")
#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:
if scheme.lower() == 'rdp':
if parsed_url.netloc is '':
sys.stderr.write("could not parse rdp scheme!\n")
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")
if args.outfile is None:
# create default name
filename = "%s %s.%s" %(scheme.upper(), hostname.split('.')[0], file_extension)
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
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