Jump to content

justmytwospence

Member
  • Posts

    19
  • Joined

  • Last visited

Posts posted by justmytwospence

  1. wordlist.csv is indeed in the workflow folder. Its actually up and running now - but I'm still not sure the way I am passing parameters to the python script is the best way. Right now, the workflow runs this command using bash:

    ./randomGen.py {query}
    

    I can pass it two arguments from Alfred this way, which are interpreted as argv[1] and argv[2] by the python sys module. I am jerry-rigging default parameters using try/except, which is functional but seems clunky. Additionally, it means that I can't give the second Alfred argument unless I also give the first (otherwise it will be argv[1] instead of argv[2]).

     

    #!/usr/bin/env python 
    
    import urllib2
    import sys
    
    try:
    	dictionary = str(sys.argv[2]).lower() + '.txt'
    except:
    	dictionary = 'british.txt'
    
    with open(dictionary, 'r') as handle:
    	wordlist = handle.readlines()
    	for x in range(0,len(wordlist)):
    		wordlist[x] = wordlist[x].strip()
    
    def generate_password(length = 4):
    	maximum = len(wordlist)
    	random_request = "http://www.random.org/integers/?num=" + str(length + 1) + "&min=0&max=" + str(maximum) + "&col=1&base=10&format=plain&rnd=new"
    	random_page = urllib2.urlopen(random_request)
    	random_integers = random_page.readlines()
    
    	for x in range(0, length):
    		random_integers[x] = int(random_integers[x].rstrip('\n'))
    
    	i = 0
    	password = []
    	while i < length:
    		new_word = wordlist[random_integers[i]]
    		password.append(new_word)
    		i = i + 1
    
    	password.append(random_integers[i][:2])
    	
    	password = '-'.join(password)
    	return(password)
    
    try:
    	query = int(sys.argv[1])
    	print(generate_password(query))
    except:
    	print(generate_password())
    

    How do y'all who use python with Alfred deal with parameters?

  2. I've figured out a way to make it work with a try/except. Can anyone confirm that it is the simplest/best way?

    #!/usr/bin/env python 
    
    import urllib2
    import sys
    
    f = open('wordlist.csv', 'r')
    wordlist = f.readlines()
    for x in range(0,len(wordlist)):
    	wordlist[x] = wordlist[x].rstrip(',\n')
    
    def generate_password(length = 4):
    	maximum = len(wordlist)
    	random_request = "http://www.random.org/integers/?num=" + str(length + 1) + "&min=0&max=" + str(maximum) + "&col=1&base=10&format=plain&rnd=new"
    	random_page = urllib2.urlopen(random_request)
    	random_integers = random_page.readlines()
    
    	for x in range(0, length):
    		random_integers[x] = int(random_integers[x].rstrip('\n'))
    
    	i = 0
    	password = []
    	while i < length:
    		new_word = wordlist[random_integers[i]]
    		password.append(new_word)
    		i = i + 1
    
    	password.append(random_integers[i][:2])
    	
    	password = '-'.join(password)
    	return(password)
    
    try:
    	query = int(sys.argv[1])
    	print(generate_password(query))
    except:
    	print(generate_password())
    
  3. Hi all,

     

    I'm trying to get my first workflow ever up and running. Its a modification of Jeremy Pippin's password generator, but this one has a larger dictionary, random word selection from random.org's API, and a parameter for the number of words you want.

    Right now nothing happens when I run it using Alfred with no {query}. I'm sure its something simple - I'm not sure that I'm checking for the existence of {query} properly.
     

    The python script is below. I have the workflow set up to copy the output to the clipboard and paste into the frontmost app.
    "zoe",
    "zomba",
    "zone",
    "zoo",
    "zoom",
    "zorn",
    "zurich",
     ]
    
    import urllib2
    
    def generate_password(length = 5):
    	maximum = len(wordList)
    	i = 0
    	password = []
    	while i < length:
    		random_request = "http://www.random.org/integers/?num=1&min=0&max=" + str(maximum) + "&col=1&base=10&format=plain&rnd=new"
    		random_page = urllib2.urlopen(random_request)
    		random_integer = int(random_page.read().rstrip('\n'))
    		new_word = wordList[random_integer]
    		password.append(new_word)
    		i = i + 1
    
    	number_request = "http://www.random.org/integers/?num=1&min=0&max=99&col=1&base=10&format=plain&rnd=new"
    	number_page = urllib2.urlopen(number_request)
    	number_integer = number_page.read().rstrip('\n')
    	password.append(number_integer)
    	
    	password = '-'.join(password)
    	return(password)
    
    query = {query}
    if 'query' in globals():
    	print(generate_password(query))
    else:
    	print(generate_password())
    

    Thanks for helpin' out a noobie!

  4. Hi, I am using Alfred 2.0 on OSX 10.8.2.

    I can show the trash by typing "trash," but I can't empty the trash with the "emptytrash" keyword, even though it is checked in preferences. Any idea what could be wrong?

     

    Thanks.

×
×
  • Create New...