Jump to content

Open links from csv document.


Recommended Posts

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 post
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 post
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 post

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 post

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 post

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 by deanishe
Link to post

 

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 by deanishe
Link to post

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...