Reputation: 109
I'm wondering if it's possible to run the script below from or inside an AppleScript App.
The idea behind all this, is that people would not need to run and deal with the code, say they won't need to run it from SublimeText or so, instead just click an app and let the script run and to the job. If that make sens to you ?
from bs4 import BeautifulSoup
import requests
import csv
class_1 = {"class": "productsPicture"}
class_2 = {"class": "product_content"}
class_3 = {"class": "id-fix"}
# map a column number to the required find parameters
class_to_find = {
0 : class_3, # Not defined in question
1 : class_1,
2 : class_1,
3 : class_3, # Not defined in question
4 : class_2,
5 : class_2}
with open('urls.csv', 'r') as csvFile, open('results.csv', 'w', newline='') as results:
reader = csv.reader(csvFile)
writer = csv.writer(results)
for row in reader:
# get the url
output_row = []
for index, url in enumerate(row):
url = url.strip()
# Skip any empty URLs
if len(url):
#print('col: {}\nurl: {}\nclass: {}\n\n'.format(index, url, class_to_find[index]))
# fetch content from server
try:
html = requests.get(url).content
except requests.exceptions.ConnectionError as e:
output_row.extend([url, '', 'bad url'])
continue
except requests.exceptions.MissingSchema as e:
output_row.extend([url, '', 'missing http...'])
continue
# soup fetched content
soup = BeautifulSoup(html, 'html.parser')
divTag = soup.find("div", class_to_find[index])
if divTag:
# Return all 'a' tags that contain an href
for a in divTag.find_all("a", href=True):
url_sub = a['href']
# Test that link is valid
try:
r = requests.get(url_sub)
output_row.extend([url, url_sub, 'ok'])
except requests.exceptions.ConnectionError as e:
output_row.extend([url, url_sub, 'bad link'])
else:
output_row.extend([url, '', 'no results'])
writer.writerow(output_row)
Upvotes: 2
Views: 6713
Reputation: 25002
Open AppleScript Editor on macOS.
In a new window add the following two lines of code:
set desktop_folder to "$HOME/Desktop"
do shell script "python " & desktop_folder & "/Pricematch/foo.py"
Note: This assumes the foo.py
file is saved to a folder named Pricemacth which exists in the users Desktop folder. You'll need to replace the foo.py
part with the actual real filename.
Because the desktop_folder
variable is set to "$HOME/Desktop"
you should be able to roll this out to multiple users without any issues. You just need to make sure the folder named Pricematch is located in their Desktop folder and it contains the .py
file to be run. The $HOME
part will automatically assume the User folder, so you won't need to specify different usernames for each user.
If you decide to change the location of .py
you'll need to update the path in the AppleScript accordingly.
To save the AppleScript:
run-python-script.app
Save
button. (it can be saved in any folder location)Double click run-python-script.app
in the Finder
to run it.
EDIT
In response to the comment:
is it possible to somehow get a notification when python script finished he's job.
Here's a couple of suggestions - it assumes the python script returns a zero/success exit status. You could try changing line two in the AppleScript to:
For displaying a dialog message:
do shell script "python " & desktop_folder & "/Pricematch/foo.py && osascript -e 'tell application \"Finder\" to activate' -e 'tell application \"Finder\" to display dialog \"Finished Python Script\"'"
Or, for speaking something:
do shell script "python " & desktop_folder & "/Pricematch/foo.py && osascript -e 'set Volume 4' -e 'tell application \"Finder\" to say \"Finished!\"' -e 'set Volume 0'"
Upvotes: 4