Skip to content

Commit

Permalink
Don't trust syncfolder setting #8
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Dec 4, 2017
1 parent b4067f9 commit fb14fa8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 63 deletions.
Binary file not shown.
47 changes: 36 additions & 11 deletions src/fixum.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ def touch(path):

def read_plist(path):
"""Convert plist to XML and read its contents."""
cmd = [b'plutil', b'-convert', b'xml1', b'-o', b'-', path]
if isinstance(path, unicode):
path = path.encode('utf-8')

cmd = ['plutil', '-convert', 'xml1', '-o', '-', path]
xml = subprocess.check_output(cmd)
return plistlib.readPlistFromString(xml)

Expand Down Expand Up @@ -146,18 +149,40 @@ def get_workflow_info(dirpath):

def get_workflow_directory():
"""Return path to Alfred's workflow directory."""
# It appears that `syncfolder` may be set but not used
# https://github.com/deanishe/alfred-fixum/issues/8
# So don't trust `syncfolder` and fall back to the default
# location, and if that fails, the grandparent of the working
# directory.
candidates = []

# User-specified sync directory
prefs = read_plist(ALFRED_PREFS)
syncdir = prefs.get('syncfolder', '~/Library/Application Support/Alfred 3')
syncdir = prefs.get('syncfolder')
if syncdir:
candidates.append(syncdir)

# Default location
candidates.append('~/Library/Application Support/Alfred 3')

# Workflows are run with their own directory as the working path,
# so try grandparent as last resort (if workflow is being run
# from Alfred)
if os.getenv('alfred_version'):
candidates.append(
os.path.dirname(os.path.dirname(os.path.abspath(os.curdir))))
else:
log.warning('workflow is not being run from Alfred')

syncdir = os.path.expanduser(syncdir)
wf_dir = os.path.join(syncdir, 'Alfred.alfredpreferences/workflows')
# log.debug('Workflow sync dir : %r', wf_dir)
candidates = [os.path.expanduser(p) for p in candidates]

if os.path.exists(wf_dir):
# log.debug('Workflow directory retrieved from Alfred preferences')
return wf_dir
for path in candidates:
log.debug('looking for workflows in %r ...', path)
wf_dir = os.path.join(path, 'Alfred.alfredpreferences/workflows')
if os.path.exists(wf_dir):
return wf_dir

log.debug('Alfred.alfredpreferences/workflows not found')
log.debug('workflow directory not found')
return None


Expand Down Expand Up @@ -264,7 +289,7 @@ def main(wf):
dry_run = opts['--nothing']
log.info('=' * 50)
log.debug('opts=%r', opts)
log.info('looking for workflows using an outdated (buggy) version '
log.info('looking for workflows using an outdated version '
'of Alfred-Workflow...')

# subprocess.call(['open', '-a', 'Console', wf.logfile])
Expand All @@ -275,7 +300,7 @@ def main(wf):
print('ERROR: could not find workflow directory')
return 1

log.info('workflow directory: %s', root)
log.info('workflow directory: %r', root)

blacklisted = load_blacklist()

Expand Down
104 changes: 52 additions & 52 deletions src/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,44 @@
<dict>
<key>config</key>
<dict>
<key>lastpathcomponent</key>
<false/>
<key>onlyshowifquerypopulated</key>
<true/>
<key>removeextension</key>
<key>concurrently</key>
<false/>
<key>text</key>
<string>{query}</string>
<key>title</key>
<string>Fixum</string>
<key>escaping</key>
<integer>102</integer>
<key>script</key>
<string>mode=$1
datadir="$alfred_workflow_data"
cachedir="$alfred_workflow_cache"
blacklist="${datadir}/blacklist.txt"
logfile="${cachedir}/net.deanishe.alfred.fixum.log"
# create data &amp; cache directories, logfile and blacklist
test -d "$cachedir" || mkdir -p "$cachedir"
test -f "$logfile" || touch "$logfile"
test -d "$datadir" || mkdir -p "$datadir"
test -f "$blacklist" || cp blacklist.default.txt "$blacklist"
# script actions
[[ "$mode" = dryrun ]] &amp;&amp; /usr/bin/python fixum.py --nothing
[[ "$mode" = fix ]] &amp;&amp; /usr/bin/python fixum.py
[[ "$mode" = blacklist ]] &amp;&amp; open "$blacklist"
[[ "$mode" = log ]] &amp;&amp; open -a Console "$logfile"
exit 0</string>
<key>scriptargtype</key>
<integer>1</integer>
<key>scriptfile</key>
<string></string>
<key>type</key>
<integer>5</integer>
</dict>
<key>type</key>
<string>alfred.workflow.output.notification</string>
<string>alfred.workflow.action.script</string>
<key>uid</key>
<string>90302262-60E4-4C1C-AAEA-2A5C3F4C025A</string>
<string>97033D94-9B6F-446C-94E5-AB677B5ABB4F</string>
<key>version</key>
<integer>1</integer>
<integer>2</integer>
</dict>
<dict>
<key>config</key>
Expand Down Expand Up @@ -116,44 +137,23 @@
<dict>
<key>config</key>
<dict>
<key>concurrently</key>
<key>lastpathcomponent</key>
<false/>
<key>escaping</key>
<integer>102</integer>
<key>script</key>
<string>mode=$1
datadir="$alfred_workflow_data"
cachedir="$alfred_workflow_cache"
blacklist="${datadir}/blacklist.txt"
logfile="${cachedir}/net.deanishe.alfred.fixum.log"
# create data &amp; cache directories, logfile and blacklist
test -d "$cachedir" || mkdir -p "$cachedir"
test -f "$logfile" || touch "$logfile"
test -d "$datadir" || mkdir -p "$datadir"
test -f "$blacklist" || cp blacklist.default.txt "$blacklist"
# script actions
[[ "$mode" = dryrun ]] &amp;&amp; /usr/bin/python fixum.py --nothing
[[ "$mode" = fix ]] &amp;&amp; /usr/bin/python fixum.py
[[ "$mode" = blacklist ]] &amp;&amp; open "$blacklist"
[[ "$mode" = log ]] &amp;&amp; open -a Console "$logfile"
exit 0</string>
<key>scriptargtype</key>
<integer>1</integer>
<key>scriptfile</key>
<string></string>
<key>type</key>
<integer>5</integer>
<key>onlyshowifquerypopulated</key>
<true/>
<key>removeextension</key>
<false/>
<key>text</key>
<string>{query}</string>
<key>title</key>
<string>Fixum</string>
</dict>
<key>type</key>
<string>alfred.workflow.action.script</string>
<string>alfred.workflow.output.notification</string>
<key>uid</key>
<string>97033D94-9B6F-446C-94E5-AB677B5ABB4F</string>
<string>90302262-60E4-4C1C-AAEA-2A5C3F4C025A</string>
<key>version</key>
<integer>2</integer>
<integer>1</integer>
</dict>
</array>
<key>readme</key>
Expand All @@ -165,27 +165,27 @@ It is primarily a workaround to fix bugs that are preventing the workflows from
<key>4FFEB019-EBD2-452B-AC8B-54E7C33ABE9F</key>
<dict>
<key>xpos</key>
<integer>20</integer>
<integer>40</integer>
<key>ypos</key>
<integer>30</integer>
<integer>40</integer>
</dict>
<key>90302262-60E4-4C1C-AAEA-2A5C3F4C025A</key>
<dict>
<key>xpos</key>
<integer>370</integer>
<integer>390</integer>
<key>ypos</key>
<integer>30</integer>
<integer>40</integer>
</dict>
<key>97033D94-9B6F-446C-94E5-AB677B5ABB4F</key>
<dict>
<key>xpos</key>
<integer>200</integer>
<integer>220</integer>
<key>ypos</key>
<integer>30</integer>
<integer>40</integer>
</dict>
</dict>
<key>version</key>
<string>0.7</string>
<string>0.8</string>
<key>webaddress</key>
<string></string>
</dict>
Expand Down

0 comments on commit fb14fa8

Please sign in to comment.