Skip to content

Instantly share code, notes, and snippets.

@Phuket2
Created August 24, 2018 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Phuket2/0bc297ebfedfa7bef6033c0c869b0c67 to your computer and use it in GitHub Desktop.
Save Phuket2/0bc297ebfedfa7bef6033c0c869b0c67 to your computer and use it in GitHub Desktop.
Launch Application - For Alfred Workflow
'''
Launch Application (Emulate standard Alfred operation)
+ Requires Alfred Powerpack and Version 3+
A script meant to be used in Alfred Script Filter(Input) within
a workflow that is connected to a 'Open File' (Action)
looks for entries with ext '.app' in the directories in the _dir_list.
Creates a list of dicts as per the Alfred Documentation and returns
a json string back to Alfred
No recursion is attempted, thats why the list of directories to search.
I looked at doing it recursively, but this did not seem like a great
idea because of older versions, lack of os.scandir in Python 2 etc.
Basically beyond me.
@Phuket2
'''
import os
import json
_app_dir = os.path.expanduser('/Applications')
_utility_dir = os.path.join(_app_dir, 'Utilities')
# Directories to look for apps, you can add more dirs to the list,
# they will be searched!
# Dirs added need to be absolute paths
_dir_list = [_app_dir, _utility_dir]
def make_item(file_name, dir_name):
return dict(
uuid=file_name,
title=os.path.splitext(file_name)[0],
subtitle=os.path.join(dir_name, file_name),
arg=os.path.join(dir_name, file_name),
autocomplete=file_name,
icon=dict(type='fileicon', path=os.path.join(dir_name, file_name)
)
)
def make_app_list(dir_list):
app_list = [make_item(fn, d) for d in dir_list for fn in os.listdir(d)
if os.path.splitext(fn)[1] == '.app']
app_list.sort(key=lambda x: x['title'])
return json.dumps(dict(items=app_list))
print(make_app_list(_dir_list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment