Skip to content

Commit

Permalink
Update Alfred-Workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Jul 31, 2020
1 parent 896c145 commit 97407f4
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 59 deletions.
File renamed without changes.
Binary file not shown.
10 changes: 8 additions & 2 deletions src/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,11 @@
<key>runningsubtext</key>
<string>Convertifying…</string>
<key>script</key>
<string>/usr/bin/python convert.py "$1"</string>
<string>echo . &gt;&amp;2
echo "http_proxy=$http_proxy"&gt;&amp;2
echo "https_proxy=$https_proxy"&gt;&amp;2
/usr/bin/python convert.py "$1"</string>
<key>scriptargtype</key>
<integer>1</integer>
<key>scriptfile</key>
Expand Down Expand Up @@ -476,6 +480,8 @@ variables={allvars}</string>
<dict>
<key>argument</key>
<string>{var:query}</string>
<key>passthroughargument</key>
<false/>
<key>variables</key>
<dict/>
</dict>
Expand Down Expand Up @@ -653,7 +659,7 @@ UPDATE_INTERVAL is the number of minutes between exchange rate updates.</string>
<string>APP_KEY</string>
</array>
<key>version</key>
<string>3.7.0</string>
<string>3.7.1</string>
<key>webaddress</key>
<string></string>
</dict>
Expand Down
5 changes: 1 addition & 4 deletions src/workflow/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ def _job_pid(name):
if _process_exists(pid):
return pid

try:
os.unlink(pidfile)
except Exception: # pragma: no cover
pass
os.unlink(pidfile)


def is_running(name):
Expand Down
18 changes: 10 additions & 8 deletions src/workflow/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ def install_notifier():
# z.extractall(destdir)
tgz = tarfile.open(archive, 'r:gz')
tgz.extractall(destdir)
assert os.path.exists(n), \
'Notify.app could not be installed in %s' % destdir
if not os.path.exists(n): # pragma: nocover
raise RuntimeError('Notify.app could not be installed in ' + destdir)

# Replace applet icon
icon = notifier_icon_path()
Expand Down Expand Up @@ -253,8 +253,9 @@ def png_to_icns(png_path, icns_path):
try:
iconset = os.path.join(tempdir, 'Icon.iconset')

assert not os.path.exists(iconset), \
'iconset already exists: ' + iconset
if os.path.exists(iconset): # pragma: nocover
raise RuntimeError('iconset already exists: ' + iconset)

os.makedirs(iconset)

# Copy source icon to icon set and generate all the other
Expand Down Expand Up @@ -283,8 +284,9 @@ def png_to_icns(png_path, icns_path):
if retcode != 0:
raise RuntimeError('iconset exited with %d' % retcode)

assert os.path.exists(icns_path), \
'generated ICNS file not found: ' + repr(icns_path)
if not os.path.exists(icns_path): # pragma: nocover
raise ValueError(
'generated ICNS file not found: ' + repr(icns_path))
finally:
try:
shutil.rmtree(tempdir)
Expand Down Expand Up @@ -332,8 +334,8 @@ def ustr(s):
print('converting {0!r} to {1!r} ...'.format(o.png, icns),
file=sys.stderr)

assert not os.path.exists(icns), \
'destination file already exists: ' + icns
if os.path.exists(icns):
raise ValueError('destination file already exists: ' + icns)

png_to_icns(o.png, icns)
sys.exit(0)
Expand Down
2 changes: 1 addition & 1 deletion src/workflow/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ def install_update():
path = retrieve_download(Download.from_dict(dl))

wf().logger.info('installing updated workflow ...')
subprocess.call(['open', path])
subprocess.call(['open', path]) # nosec

wf().cache_data(key, no_update)
return True
Expand Down
134 changes: 113 additions & 21 deletions src/workflow/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@
# "com.runningwithcrayons.Alfred" depending on version.
#
# Open Alfred in search (regular) mode
JXA_SEARCH = "Application({app}).search({arg});"
JXA_SEARCH = 'Application({app}).search({arg});'
# Open Alfred's File Actions on an argument
JXA_ACTION = "Application({app}).action({arg});"
JXA_ACTION = 'Application({app}).action({arg});'
# Open Alfred's navigation mode at path
JXA_BROWSE = "Application({app}).browse({arg});"
JXA_BROWSE = 'Application({app}).browse({arg});'
# Set the specified theme
JXA_SET_THEME = "Application({app}).setTheme({arg});"
JXA_SET_THEME = 'Application({app}).setTheme({arg});'
# Call an External Trigger
JXA_TRIGGER = "Application({app}).runTrigger({arg}, {opts});"
JXA_TRIGGER = 'Application({app}).runTrigger({arg}, {opts});'
# Save a variable to the workflow configuration sheet/info.plist
JXA_SET_CONFIG = "Application({app}).setConfiguration({arg}, {opts});"
JXA_SET_CONFIG = 'Application({app}).setConfiguration({arg}, {opts});'
# Delete a variable from the workflow configuration sheet/info.plist
JXA_UNSET_CONFIG = "Application({app}).removeConfiguration({arg}, {opts});"
JXA_UNSET_CONFIG = 'Application({app}).removeConfiguration({arg}, {opts});'
# Tell Alfred to reload a workflow from disk
JXA_RELOAD_WORKFLOW = 'Application({app}).reloadWorkflow({arg});'


class AcquisitionError(Exception):
Expand Down Expand Up @@ -148,17 +150,16 @@ def applescriptify(s):
.. versionadded:: 1.31
Replaces ``"`` with `"& quote &"`. Use this function if you want
to insert a string into an AppleScript script:
>>> query = 'g "python" test'
>>> applescriptify(query)
>>> applescriptify('g "python" test')
'g " & quote & "python" & quote & "test'
Args:
s (unicode): Unicode string to escape.
Returns:
unicode: Escaped string
unicode: Escaped string.
"""
return s.replace(u'"', u'" & quote & "')
Expand All @@ -173,11 +174,11 @@ def run_command(cmd, **kwargs):
all arguments are encoded to UTF-8 first.
Args:
cmd (list): Command arguments to pass to ``check_output``.
**kwargs: Keyword arguments to pass to ``check_output``.
cmd (list): Command arguments to pass to :func:`~subprocess.check_output`.
**kwargs: Keyword arguments to pass to :func:`~subprocess.check_output`.
Returns:
str: Output returned by ``check_output``.
str: Output returned by :func:`~subprocess.check_output`.
"""
cmd = [utf8ify(s) for s in cmd]
Expand All @@ -197,6 +198,7 @@ def run_applescript(script, *args, **kwargs):
script (str, optional): Filepath of script or code to run.
*args: Optional command-line arguments to pass to the script.
**kwargs: Pass ``lang`` to run a language other than AppleScript.
Any other keyword arguments are passed to :func:`run_command`.
Returns:
str: Output of run command.
Expand Down Expand Up @@ -242,8 +244,8 @@ def run_trigger(name, bundleid=None, arg=None):
.. versionadded:: 1.31
If ``bundleid`` is not specified, reads the bundle ID of the current
workflow from Alfred's environment variables.
If ``bundleid`` is not specified, the bundle ID of the calling
workflow is used.
Args:
name (str): Name of External Trigger to call.
Expand All @@ -264,11 +266,29 @@ def run_trigger(name, bundleid=None, arg=None):
run_applescript(script, lang='JavaScript')


def set_theme(theme_name):
"""Change Alfred's theme.
.. versionadded:: 1.39.0
Args:
theme_name (unicode): Name of theme Alfred should use.
"""
appname = jxa_app_name()
script = JXA_SET_THEME.format(app=json.dumps(appname),
arg=json.dumps(theme_name))
run_applescript(script, lang='JavaScript')


def set_config(name, value, bundleid=None, exportable=False):
"""Set a workflow variable in ``info.plist``.
.. versionadded:: 1.33
If ``bundleid`` is not specified, the bundle ID of the calling
workflow is used.
Args:
name (str): Name of variable to set.
value (str): Value to set variable to.
Expand Down Expand Up @@ -297,6 +317,9 @@ def unset_config(name, bundleid=None):
.. versionadded:: 1.33
If ``bundleid`` is not specified, the bundle ID of the calling
workflow is used.
Args:
name (str): Name of variable to delete.
bundleid (str, optional): Bundle ID of workflow variable belongs to.
Expand All @@ -313,6 +336,71 @@ def unset_config(name, bundleid=None):
run_applescript(script, lang='JavaScript')


def search_in_alfred(query=None):
"""Open Alfred with given search query.
.. versionadded:: 1.39.0
Omit ``query`` to simply open Alfred's main window.
Args:
query (unicode, optional): Search query.
"""
query = query or u''
appname = jxa_app_name()
script = JXA_SEARCH.format(app=json.dumps(appname), arg=json.dumps(query))
run_applescript(script, lang='JavaScript')


def browse_in_alfred(path):
"""Open Alfred's filesystem navigation mode at ``path``.
.. versionadded:: 1.39.0
Args:
path (unicode): File or directory path.
"""
appname = jxa_app_name()
script = JXA_BROWSE.format(app=json.dumps(appname), arg=json.dumps(path))
run_applescript(script, lang='JavaScript')


def action_in_alfred(paths):
"""Action the give filepaths in Alfred.
.. versionadded:: 1.39.0
Args:
paths (list): Unicode paths to files/directories to action.
"""
appname = jxa_app_name()
script = JXA_ACTION.format(app=json.dumps(appname), arg=json.dumps(paths))
run_applescript(script, lang='JavaScript')


def reload_workflow(bundleid=None):
"""Tell Alfred to reload a workflow from disk.
.. versionadded:: 1.39.0
If ``bundleid`` is not specified, the bundle ID of the calling
workflow is used.
Args:
bundleid (unicode, optional): Bundle ID of workflow to reload.
"""
bundleid = bundleid or os.getenv('alfred_workflow_bundleid')
appname = jxa_app_name()
script = JXA_RELOAD_WORKFLOW.format(app=json.dumps(appname),
arg=json.dumps(bundleid))

run_applescript(script, lang='JavaScript')


def appinfo(name):
"""Get information about an installed application.
Expand All @@ -325,11 +413,15 @@ def appinfo(name):
AppInfo: :class:`AppInfo` tuple or ``None`` if app isn't found.
"""
cmd = ['mdfind', '-onlyin', '/Applications',
'-onlyin', os.path.expanduser('~/Applications'),
'(kMDItemContentTypeTree == com.apple.application &&'
'(kMDItemDisplayName == "{0}" || kMDItemFSName == "{0}.app"))'
.format(name)]
cmd = [
'mdfind',
'-onlyin', '/Applications',
'-onlyin', '/System/Applications',
'-onlyin', os.path.expanduser('~/Applications'),
'(kMDItemContentTypeTree == com.apple.application &&'
'(kMDItemDisplayName == "{0}" || kMDItemFSName == "{0}.app"))'
.format(name)
]

output = run_command(cmd).strip()
if not output:
Expand Down
2 changes: 1 addition & 1 deletion src/workflow/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.37.2
1.40.0
Loading

0 comments on commit 97407f4

Please sign in to comment.