从 AppleScript 应用程序运行 python 脚本 [英] Run python script from an AppleScript App
问题描述
我想知道是否可以在 AppleScript 应用程序 中或内部运行以下脚本.
I'm wondering if it's possible to run the script below from or inside an AppleScript App.
这一切背后的想法是,人们不需要运行和处理代码,说他们不需要从 SublimeText 左右运行它,而只需点击一个应用程序,然后让脚本运行并完成工作.如果这对你有意义?
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)
推荐答案
在 macOS 上打开 AppleScript 编辑器.
在新窗口中添加以下两行代码:
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"
注意:这假设 foo.py
文件保存在名为 Pricemacth 的文件夹中,该文件夹存在于用户 Desktop 文件夹.您需要将 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.
由于 desktop_folder
变量设置为 "$HOME/Desktop"
,您应该能够将其推广给多个用户而不会出现任何问题.您只需要确保名为 Pricematch 的文件夹位于他们的 Desktop 文件夹中,并且它包含要运行的 .py
文件.$HOME
部分将自动采用 User 文件夹,因此您无需为每个用户指定不同的用户名.
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.
如果您决定更改 .py
的位置,则需要相应地更新 AppleScript 中的路径.
If you decide to change the location of .py
you'll need to update the path in the AppleScript accordingly.
要保存 AppleScript:
To save the AppleScript:
- 从菜单栏中选择文件 > 另存为.
- 选择文件格式 > 应用
- 输入文件名.例如.
run-python-script.app
- 点击
保存
按钮.(它可以保存在任何文件夹位置)
- select File > Save As from the menu bar.
- Choose File Format > Application
- Enter a filename. E.g.
run-python-script.app
- Click
Save
button. (it can be saved in any folder location)
在Finder
中双击run-python-script.app
来运行它.
<小时>
编辑
回应评论:
当python脚本完成他的工作时,是否有可能以某种方式得到通知.
is it possible to somehow get a notification when python script finished he's job.
这里有一些建议 - 它假设 python 脚本返回零/成功退出状态.您可以尝试将 AppleScript 中的第二行更改为:
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'"
这篇关于从 AppleScript 应用程序运行 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!