Yuri Posted July 23, 2017 Share Posted July 23, 2017 Hello everyone! I have a document with name link.csv, which is on the desktop, /Users/.../Desktop/links.csv There are some links in the document: https://www.google.com https://www.alexa.com https://www.facebook.com https://wikipedia.org https://www.youtube.com https://github.com etc 1.How can I open all links from csv. with Alfred? 2.How can I open first 5 links? 3. How can I delete first 5 links? Any help will be appreciated! Link to comment
deanishe Posted July 23, 2017 Share Posted July 23, 2017 3 hours ago, Yuri said: 1.How can I open all links from csv. with Alfred? All at the same time? 3 hours ago, Yuri said: 2.How can I open first 5 links? Always the first 5? Or sometimes the first 4? 3 hours ago, Yuri said: 3. How can I delete first 5 links? With a text editor. Link to comment
Yuri Posted July 23, 2017 Author Share Posted July 23, 2017 57 minutes ago, deanishe said: All at the same time? Yes! 58 minutes ago, deanishe said: Always the first 5? Or sometimes the first 4? Sometimes 4 58 minutes ago, deanishe said: With a text editor. Ok :-) Link to comment
deanishe Posted July 23, 2017 Share Posted July 23, 2017 Add a Keyword input with "Argument optional" then connect it to a Run Script action with Language set to "/usr/bin/python". Paste this script in the Script box: # encoding: utf-8 import csv import os import subprocess import sys csvfile = os.path.expanduser('~/Desktop/links.csv') count = 0 if len(sys.argv) > 1: count = int(sys.argv[1]) urls = [] with open(csvfile) as fp: reader = csv.reader(fp, delimiter=',') for i, row in enumerate(reader): if count and i == count: break urls.append(row[1]) for url in urls: subprocess.call(['/usr/bin/open', url]) If you enter a number after your keyword, it will open that many URLs. If you don't enter a number it will open them all. Link to comment
Yuri Posted July 24, 2017 Author Share Posted July 24, 2017 Deanishe thank you for your help but I don't know what I am doing wrong. For some reason, it doesn't work for me https://yadi.sk/d/SZRolxEh3LKzre In debug mode, It shows me this message: "[ERROR: action.script] Traceback (most recent call last): File "<string>", line 18, in <module> IndexError: list index out of range" Link to comment
deanishe Posted July 24, 2017 Share Posted July 24, 2017 Is it a CSV file or just a plain text file? I.e. does it have more than one column? Link to comment
Yuri Posted July 24, 2017 Author Share Posted July 24, 2017 (edited) The document has only one column. Maybe the plain text file will be better. Edited July 24, 2017 by Yuri Link to comment
deanishe Posted July 25, 2017 Share Posted July 25, 2017 (edited) Then use this script instead: # encoding: utf-8 import os import subprocess import sys csvfile = os.path.expanduser('~/Desktop/links.csv') count = 0 if len(sys.argv) > 1: count = int(sys.argv[1]) urls = [] with open(csvfile) as fp: for i, line in enumerate(fp): if count and i == count: break urls.append(line.strip()) for url in urls: subprocess.call(['/usr/bin/open', url]) Edited July 25, 2017 by deanishe Yuri 1 Link to comment
Yuri Posted July 25, 2017 Author Share Posted July 25, 2017 (edited) deanishe thank you, it works well! Edited July 25, 2017 by Yuri Link to comment
Yuri Posted July 27, 2017 Author Share Posted July 27, 2017 After I opened 10 links, how can I delete these 10 links inside csv document? Without text editor Because this could save time. Link to comment
deanishe Posted July 27, 2017 Share Posted July 27, 2017 Do you want the links to always be deleted or only sometimes? Link to comment
Yuri Posted July 27, 2017 Author Share Posted July 27, 2017 Always after the links begin to open in the browser. Link to comment
deanishe Posted July 27, 2017 Share Posted July 27, 2017 (edited) Use this script instead. It deletes the URLs after opening them. # encoding: utf-8 from collections import deque import os import subprocess import sys csvfile = os.path.expanduser('~/Desktop/links.csv') # load all URLs urls = deque() with open(csvfile) as fp: for line in fp: line = line.strip() if line: urls.append(line) count = 0 if len(sys.argv) > 1: count = int(sys.argv[1]) if not count or count > len(urls): count = len(urls) # open URLs i = 0 while True: if i == count: break url = urls.popleft() subprocess.call(['/usr/bin/open', url]) i += 1 # write remaining URLs back to file with open(csvfile, 'wb') as fp: fp.write('\n'.join(urls)) Edited July 27, 2017 by deanishe Yuri 1 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now