从AppleScript应用程序运行python脚本 [英] Run python script from an AppleScript App

查看:98
本文介绍了从AppleScript应用程序运行python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以从 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)

推荐答案

  1. 在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 文件保存到用户 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:

  1. 用于显示对话框消息:

  1. 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆