#!/usr/bin/env python
#
# LaunchBar Action Script
#
import sys
import urllib
import os

# Read BetterZip's per-install local-origin token so it knows this request comes from
# the user's own machine and skips the confirmation dialog. Absent token still works.
def read_token():
    path = os.path.expanduser('~/Library/Application Support/com.macitbetter.betterzip/url-token')
    try:
        with open(path) as f:
            return f.read().strip()
    except IOError:
        return ''

preset = '%preset-name%'
files = ';'.join(map(urllib.quote, sys.argv[1:]))
token = read_token()
fragment = ('#' + token) if token else ''
url = '"btrzp://compress/' + urllib.quote(preset) + '?' + files + fragment + '"'
os.system('open ' + url)
