Skip to content

Commit

Permalink
update workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
lichengzhang committed Aug 3, 2018
1 parent eb0976f commit cb07cc5
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 32 deletions.
42 changes: 22 additions & 20 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
<key>modifiers</key>
<integer>524288</integer>
<key>modifiersubtext</key>
<string></string>
<string>同步到单词本</string>
<key>vitoclose</key>
<false/>
</dict>
Expand Down Expand Up @@ -126,25 +126,6 @@
<string>Youdao</string>
<key>objects</key>
<array>
<dict>
<key>config</key>
<dict>
<key>browser</key>
<string></string>
<key>spaces</key>
<string></string>
<key>url</key>
<string>http://dict.youdao.com/search?q={query}</string>
<key>utf8</key>
<true/>
</dict>
<key>type</key>
<string>alfred.workflow.action.openurl</string>
<key>uid</key>
<string>6A03FDC5-89AC-4F9D-9456-3762ACA751FE</string>
<key>version</key>
<integer>1</integer>
</dict>
<dict>
<key>config</key>
<dict>
Expand All @@ -168,6 +149,25 @@
<key>version</key>
<integer>2</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>browser</key>
<string></string>
<key>spaces</key>
<string></string>
<key>url</key>
<string>http://dict.youdao.com/search?q={query}</string>
<key>utf8</key>
<true/>
</dict>
<key>type</key>
<string>alfred.workflow.action.openurl</string>
<key>uid</key>
<string>6A03FDC5-89AC-4F9D-9456-3762ACA751FE</string>
<key>version</key>
<integer>1</integer>
</dict>
<dict>
<key>config</key>
<dict>
Expand Down Expand Up @@ -196,6 +196,8 @@
<dict>
<key>alfredfiltersresults</key>
<false/>
<key>alfredfiltersresultsmatchmode</key>
<integer>0</integer>
<key>argumenttrimmode</key>
<integer>0</integer>
<key>argumenttype</key>
Expand Down
Binary file modified whyliam.workflows.youdao.alfredworkflow
Binary file not shown.
Empty file modified workflow/Notify.tgz
100755 → 100644
Empty file.
Empty file modified workflow/__init__.py
100755 → 100644
Empty file.
Empty file modified workflow/background.py
100755 → 100644
Empty file.
Empty file modified workflow/notify.py
100755 → 100644
Empty file.
22 changes: 15 additions & 7 deletions workflow/update.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,9 @@ def check_update(github_slug, current_version, prereleases=False):
releases = get_valid_releases(github_slug, prereleases)

if not len(releases):
raise ValueError('no valid releases for %s', github_slug)
wf().logger.warning('no valid releases for %s', github_slug)
wf().cache_data('__workflow_update_status', {'available': False})
return False

wf().logger.info('%d releases for %s', len(releases), github_slug)

Expand Down Expand Up @@ -418,9 +420,15 @@ def show_help(status=0):

action, github_slug, version = argv[1:]

if action == 'check':
check_update(github_slug, version, prereleases)
elif action == 'install':
install_update()
else:
show_help(1)
try:

if action == 'check':
check_update(github_slug, version, prereleases)
elif action == 'install':
install_update()
else:
show_help(1)

except Exception as err: # ensure traceback is in log file
wf().logger.exception(err)
raise err
82 changes: 79 additions & 3 deletions workflow/util.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@
end tell
"""

# AppleScript to save a variable in info.plist
AS_CONFIG_SET = """
tell application "Alfred 3"
set configuration "{name}" to value "{value}" in workflow "{bundleid}" {export}
end tell
"""

# AppleScript to remove a variable from info.plist
AS_CONFIG_UNSET = """
tell application "Alfred 3"
remove configuration "{name}" in workflow "{bundleid}"
end tell
"""


class AcquisitionError(Exception):
"""Raised if a lock cannot be acquired."""
Expand All @@ -53,6 +67,7 @@ class AcquisitionError(Exception):
.. py:attribute:: bundleid
Application's bundle ID, e.g. ``u'com.apple.Safari'``.
"""


Expand Down Expand Up @@ -96,6 +111,7 @@ def utf8ify(s):
Returns:
str: UTF-8 string or string representation of s.
"""
if isinstance(s, str):
return s
Expand Down Expand Up @@ -124,6 +140,7 @@ def applescriptify(s):
Returns:
unicode: Escaped string
"""
return s.replace(u'"', u'" & quote & "')

Expand All @@ -142,6 +159,7 @@ def run_command(cmd, **kwargs):
Returns:
str: Output returned by ``check_output``.
"""
cmd = [utf8ify(s) for s in cmd]
return subprocess.check_output(cmd, **kwargs)
Expand Down Expand Up @@ -190,6 +208,7 @@ def run_jxa(script, *args):
Returns:
str: Output of script.
"""
return run_applescript(script, *args, lang='JavaScript')

Expand All @@ -206,6 +225,7 @@ def run_trigger(name, bundleid=None, arg=None):
name (str): Name of External Trigger to call.
bundleid (str, optional): Bundle ID of workflow trigger belongs to.
arg (str, optional): Argument to pass to trigger.
"""
if not bundleid:
bundleid = os.getenv('alfred_workflow_bundleid')
Expand All @@ -221,6 +241,58 @@ def run_trigger(name, bundleid=None, arg=None):
run_applescript(script)


def set_config(name, value, bundleid=None, exportable=False):
"""Set a workflow variable in ``info.plist``.
.. versionadded:: 1.33
Args:
name (str): Name of variable to set.
value (str): Value to set variable to.
bundleid (str, optional): Bundle ID of workflow variable belongs to.
exportable (bool, optional): Whether variable should be marked
as exportable (Don't Export checkbox).
"""
if not bundleid:
bundleid = os.getenv('alfred_workflow_bundleid')

name = applescriptify(name)
value = applescriptify(value)
bundleid = applescriptify(bundleid)

if exportable:
export = 'exportable true'
else:
export = 'exportable false'

script = AS_CONFIG_SET.format(name=name, bundleid=bundleid,
value=value, export=export)

run_applescript(script)


def unset_config(name, bundleid=None):
"""Delete a workflow variable from ``info.plist``.
.. versionadded:: 1.33
Args:
name (str): Name of variable to delete.
bundleid (str, optional): Bundle ID of workflow variable belongs to.
"""
if not bundleid:
bundleid = os.getenv('alfred_workflow_bundleid')

name = applescriptify(name)
bundleid = applescriptify(bundleid)

script = AS_CONFIG_UNSET.format(name=name, bundleid=bundleid)

run_applescript(script)


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

path = run_command(cmd).strip()
if not path:
output = run_command(cmd).strip()
if not output:
return None

path = output.split('\n')[0]

cmd = ['mdls', '-raw', '-name', 'kMDItemCFBundleIdentifier', path]
bid = run_command(cmd).strip()
if not bid: # pragma: no cover
Expand Down
2 changes: 1 addition & 1 deletion workflow/version
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.32
1.35
Empty file modified workflow/web.py
100755 → 100644
Empty file.
Empty file modified workflow/workflow.py
100755 → 100644
Empty file.
10 changes: 9 additions & 1 deletion workflow/workflow3.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,11 @@ def session_id(self):

return self._session_id

def setvar(self, name, value):
def setvar(self, name, value, persist=False):
"""Set a "global" workflow variable.
.. versionchanged:: 1.33
These variables are always passed to downstream workflow objects.
If you have set :attr:`rerun`, these variables are also passed
Expand All @@ -533,9 +535,15 @@ def setvar(self, name, value):
Args:
name (unicode): Name of variable.
value (unicode): Value of variable.
persist (bool, optional): Also save variable to ``info.plist``?
"""
self.variables[name] = value
if persist:
from .util import set_config
set_config(name, value, self.bundleid)
self.logger.debug('saved variable %r with value %r to info.plist',
name, value)

def getvar(self, name, default=None):
"""Return value of workflow variable for ``name`` or ``default``.
Expand Down

0 comments on commit cb07cc5

Please sign in to comment.