def bookmarks2items(bookmarks): items = [] for bookmark in bookmarks: item = Item(uid=alp.bundle(), arg=bookmark[0], valid=True, title=bookmark[2], subtitle=bookmark[1][0], icon='logo.png') items.append(item) return items
def checkCache(delete=False): filepath = alp.cache() f = '%s_requests_cache.sqlite' % alp.bundle() fullpath = os.path.join(filepath,f) if os.path.exists(fullpath): if ((datetime.now() - datetime.fromtimestamp(os.path.getmtime(fullpath))) > timedelta(hours=6)) or delete: try: os.remove(fullpath) alp.log('Successfully removed requests cache') except: alp.log('Problem: Could not remove requests cache') return
def bookmarks2items(bookmarks): items = [] for bookmark in bookmarks: item = Item( uid=alp.bundle(), arg=bookmark[0], valid=True, title=bookmark[2], subtitle=bookmark[1][0], icon='logo.png') items.append(item) return items
def setKeyChainPassword(password): keychain = alp.Keychain(alp.bundle()) settings = alp.readPlist(SETTINGS_PATH) username = settings["username"] if settings["passwordSet"] == "true": keychain.modifyPassword(username, password) else: keychain.storePassword(username, password) settings["passwordSet"] = "true" settings["credentialsChanged"] = "true" alp.writePlist(settings, SETTINGS_PATH)
def context_menu(key="",search=""): """Returns the context menu for a result item""" # This method takes only the key (id) of the actioned item as an argument. # So we need to load the last results, and find the item of that key. import os import json import time bid = alp.bundle() + str(time.time()) # Load the parsed results from the latest Inspire search. lastf = os.path.join(alp.cache(),"lastresults.json") with open(lastf,"r") as f: items = json.load(f) # Lookup the item from the results. for i in items: if 'id' in i: if i['id'] == key: item = i break # Populate the context menu action list. actions = [] # Link to the Inspire record page. actions.append( alp.Item( title = item['title'], subtitle = "Open INSPIRE record page in browser", arg = encode_arguments(type='inspirerecord',value=item['id']), uid = bid+"inspirerecord" ) ) # Author search. authors = item['author'].split(" and ") if len(authors) == 1: actions.append( alp.Item( title = item['author'], subtitle = "Find more papers of author", valid = "no", autocomplete = "find a "+ item['author'] + alfred_delim, uid = bid + "authors" ) ) else: actions.append( alp.Item( title = authors_to_lastnames(item['author']), subtitle = "Find more papers of authors", valid = "no", autocomplete = search + alfred_delim + key + alfred_delim + item['author'] + alfred_delim, uid = bid + "authors" ) ) # Link to resolve the DOI. if 'doi' in item: url = "http://dx.doi.org/" + item['doi'] actions.append( alp.Item( title = bibitem_to_journaltext(item), subtitle = "Open DOI in browser", arg = encode_arguments(type='url',value=url), uid = bid + "doi" ) ) # Next, the option to open the PDF from arXiv. if 'eprint' in item: if item['archiveprefix'] != 'arXiv': urlprefix = item['archiveprefix'] + "/" prefix = urlprefix else: urlprefix = "" prefix = 'arXiv:' url = "http://arxiv.org/pdf/" + urlprefix + item['eprint'] filename = os.path.join( get_local_dir(), (item['eprint'] + " " + authors_to_lastnames(item['author']) + " - " + item['title'] + '.pdf').replace('/','_').replace(':','_') ) actions.append( alp.Item( title = prefix + item['eprint'], subtitle = "Download and open PDF", arg = encode_arguments(type='getpdf',value=[url,filename]), uid = bid + "arxivpdf" ) ) # The option to lookup references. actions.append( alp.Item( title = "References", subtitle = "Find papers that this paper cites", valid = "no", autocomplete = "citedby:" + key + alfred_delim, uid = bid + "refs" ) ) # The option to lookup citations. actions.append( alp.Item( title = "Citations", subtitle = "Find papers that cite this paper", valid = "no", autocomplete = "refersto:" + key + alfred_delim, uid = bid + "cites" ) ) # The option to copy the bibtex of the current item to the clipboard. actions.append( alp.Item( title = "BibTeX", subtitle = "Copy BibTeX to clipboard", uid = bid+"bibtex", arg = encode_arguments( type = 'clipboard', value = bibitem_to_bibtex(item), notification = { 'title':'Copied BibTeX to clipboard', 'text':'BibTeX for ' + key + ' has been copied to the clipboard' } ) ) ) # And return. return actions
def context_ads_menu(key="", search=""): """Returns the context menu for ads result item""" # This method takes only the key (id) of the actioned item as an argument. # So we need to load the last results, and find the item of that key. # ADS should have adsurl import os import json import time bid = alp.bundle() + str(time.time()) # Load the parsed results from the latest Inspire search. lastf = os.path.join(alp.cache(), "lastresults.json") with open(lastf, "r") as f: items = json.load(f) # Lookup the item from the results. for i in items: if 'id' in i: if i['id'] == key: item = i break # Populate the context menu action list. actions = [] # Link to the ADS record page. # if 'adsurl' in item: actions.append( alp.Item(title=item['title'], subtitle="Open ADS record page in browser", arg=encode_arguments(type='url', value=item['adsurl']), uid=bid + "adsrecord")) # Author search. authors = item['author'].split(" and ") if len(authors) == 1: actions.append( alp.Item(title=item['author'], subtitle="Find more papers of author", valid="no", autocomplete="author: " + item['author'] + alfred_delim, uid=bid + "authors")) else: actions.append( alp.Item(title=authors_to_lastnames(item['author']), subtitle="Find more papers of authors", valid="no", autocomplete=search + alfred_delim + key + alfred_delim + item['author'] + alfred_delim, uid=bid + "authors")) # Link to resolve the DOI. if 'doi' in item: url = "http://dx.doi.org/" + item['doi'] actions.append( alp.Item(title=bibitem_to_journaltext(item), subtitle="Open DOI in browser", arg=encode_arguments(type='url', value=url), uid=bid + "doi")) # Next, the option to open the PDF from arXiv. if 'eprint' in item: #if item['archiveprefix'] != 'arXiv': # urlprefix = item['archiveprefix'] + "/" # prefix = urlprefix #else: urlprefix = "" prefix = 'arXiv:' url = "http://arxiv.org/pdf/" + urlprefix + item['eprint'] filename = os.path.join( get_local_dir(), (item['eprint'] + " " + authors_to_lastnames(item['author']) + " - " + item['title'] + '.pdf').replace('/', '_').replace(':', '_')) actions.append( alp.Item(title=prefix + item['eprint'], subtitle="Download and open PDF", arg=encode_arguments(type='getpdf', value=[url, filename]), uid=bid + "arxivpdf")) # The option to lookup references. actions.append( alp.Item(title="References", subtitle="Find papers that this paper cites", valid="no", autocomplete="references(bibcode:" + key + ")" + alfred_delim, uid=bid + "refs")) # The option to lookup citations. actions.append( alp.Item(title="Citations", subtitle="Find papers that cite this paper", valid="no", autocomplete="citations(bibcode:" + key + ")" + alfred_delim, uid=bid + "cites")) # The option to copy the bibtex of the current item to the clipboard. actions.append( alp.Item(title="BibTeX", subtitle="Copy BibTeX to clipboard", uid=bid + "bibtex", arg=encode_arguments(type='clipboard', value=bibitem_to_bibtex(item), notification={ 'title': 'Copied BibTeX to clipboard', 'text': 'BibTeX for ' + key + ' has been copied to the clipboard' }))) # And return. return actions
import alp from alp import Item query = "{query}" item = Item(uid=alp.bundle(), arg='', valid=True, title=query, subtitle='set username for delicious', icon='logo.png') alp.feedback([item]) # ========================= import os from alp import core query = "{query}" infoPath = os.path.abspath("./info.plist") plist = core.readPlist(infoPath) plist['username'] = query core.writePlist(plist, infoPath)
query = alp.args()[0].strip() else: query = '' # Sync & load favorites # rsync(alp.storage('favorites.json'), 'favorites.json') favs = alp.jsonLoad('favorites.json', default=[]) if type(favs) is DictType: favs = favs.values() if not favs: item = alp.Item( title='No Favorites', subtitle= 'No favorites were found. Add favorites via the File Actions panel.', uid=alp.bundle() + ' none', valid=False) alp.feedback(item) # SCRIPT EXITS # Remove duplicate favorites (sets can't have duplicates) favs = list(set(favs)) # Remove non-existent favorites new_favs = [] for fav in favs: if path.exists(fav): new_favs.append(fav) favs = new_favs alp.jsonDump(favs, 'favorites.json')
def getKeyChainPassword(): keychain = alp.Keychain(alp.bundle()) settings = alp.readPlist(SETTINGS_PATH) return keychain.retrievePassword(settings["username"])
from os import path from types import * import alp if len(alp.args()) > 0: query = alp.args()[0].strip() else: query = '' # Sync & load favorites favs = alp.jsonLoad('favorites.json', default=[]) if type(favs) is DictType: favs = favs.values() if not favs: item = alp.Item(title='No Favorites', subtitle='No favorites were found. Add favorites via the File Actions panel.', uid=alp.bundle() + ' none', valid=False) alp.feedback(item) # SCRIPT EXITS # Remove duplicate favorites (sets can't have duplicates) favs = list(set(favs)) # Remove non-existent favorites new_favs = [] for fav in favs: if path.exists(fav): new_favs.append(fav) favs = new_favs alp.jsonDump(favs, 'favorites.json')
import alp from alp import Item query = "{query}" item = Item( uid=alp.bundle(), arg='', valid=True, title=query, subtitle='set username for delicious', icon='logo.png') alp.feedback([item]) # ========================= import os from alp import core query = "{query}" infoPath = os.path.abspath("./info.plist") plist = core.readPlist(infoPath) plist['username'] = query core.writePlist(plist, infoPath)