学习 Applescript:无法链接到 Safari [英] Learning Applescript: Trouble linking to Safari

查看:23
本文介绍了学习 Applescript:无法链接到 Safari的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

跑下来

您好,这是一个非常具体的情况问题.基本上,我在 Safari 上的 Automator 中运行 Applescript,结果为 Null.

Hello, this is a very specific and situation question. Basically, I'm running Applescript in Automator on Safari, and the result is Null.

我有什么

我目前有一个可以做这些事情的脚本:

I currently have a script that can do these things:

  1. 在我运行 Automator 程序之前,我已经打开了 Safari

  1. Before I even run the Automator program, I have Safari open

接下来我运行 Automator,它以以下代码开头:

Next I run the Automator, which starts with this code:

on run {input, parameters}

    tell application "Safari" to activate
    tell application "System Events"
        keystroke "t" using command down
    end tell
    return input
end run

此脚本将激活 Safari,并打开一个新选项卡.

This script will activate Safari, and open a new tab.

接下来我要暂停一下 Automator.这将使页面有时间加载

Next I have an Automator pause. This will give the page time to load

Safari 页面打开后,我运行以下代码:

After the Safari page has opened, I run this code:

on run
    clickID("75610556")
end run

on clickID(theId)
    tell application "Safari"
        do JavaScript "document.getElementById(‘" & theId & "‘).click();" in document 1
    end tell
end clickID

这个脚本应该选择了 ID 为75610556"的按钮,但它没有.这是我的程序的关键问题.

This script should of selected the button with the ID "75610556", but it did not. This is the key problem with my program.

接下来我运行以下代码:

Next I run the following code:

on run {input, parameters}

    tell application "Safari" to activate
    tell application "System Events"
        keystroke "w" using command down
    end tell

    return input
end run

这将关闭当前选项卡.

接下来,我使用 Automator 循环进行循环.

Next, I loop through with an Automator loop.

我需要什么帮助

我将逐步解决我对该计划的问题.

I will address my questions for the program step by step.

  1. 对于第一步,这是设置的好方法吗?Automator 甚至是正确的方法吗?

  1. For step one, is this a good way to set things up? Is Automator even a proper way to go?

这段代码有效吗?还有错误吗?

Is this code efficient? Any errors yet?

我真的希望有一个替代方案.我没有找到.

I really hope there is an alternative to this. I have not found one.

为什么这在 Safari 中不起作用?我觉得我做对了.

Why won't this work in Safari? I feel like I have done this right.

与第 2 条相同,只是好奇这是否是处理此问题的最佳方法

Same as number 2, just curious if this is the best way to handle this

我认为这是做事的好方法.

I think this is a good way to do things.

为什么我需要这个 + 每一个细节(可选部分)

如果您需要,这些是额外的详细信息.它将为您提供几乎所有细节,并解释为什么我需要这个.

These are extra details if you want them. It will give you nearly every detail, and explain why I need this.

所以我的大学有一个商业竞赛.我是一名大一程序员,编写了一个非常简单的移动网络游戏.问题是在民意调查网站上有 3,000 美元的现金奖励.这个网站是 Poll Everywhere.评委们丝毫没有意识到这个网站是不安全的.我们报告了它,评委认为这不足以成为取消投票并可能寻找新替代方案的理由.我们报告说,Poll Everywhere 可以被操纵以发挥任何足够聪明的团队优势:

A.关闭浏览器的 cookie

B.使用私人浏览器

当然,评委们没有听.他们认为如果有人熬夜,它可能会改变它,但在2-3小时内,它几乎没有影响.为了阻止他们的裁决,我想在我的 Mac 上为他们制作一个让他们大吃一惊的示例程序:投票程序.该程序将打开 safari.Safari 已配置为以私人浏览器模式打开投票页面.我的程序所要做的就是打开一个新选项卡,找到按钮 ID,然后单击它.这样做越快越好.

  • 最后一点:背景故事太多了吗?我知道堆栈溢出有一个保留细节"的政策.我认为精心制作的版本可能会有所帮助,但如果它是一件坏事,我会将其删除.

推荐答案

很遗憾我没有很多时间来回答你所有的问题,因为很晚了,我需要睡觉,但这里有一个例子你需要做的.不需要自动化,这对我来说似乎使您的过程复杂化.

Unfortunately I don't have a lot of time to answer all your questions as it's late and I need to get to bed, but here is an example of all you need to do. No need for automator, that just seems to complicate your process to me.

如果您需要更多的等待"时间,您可能需要稍微调整一些 sleep 设置,我在我的本地网络服务器上尝试了这个并且它工作,但它是本地的所以网站加载非常快.

You may want to tweak some of the sleep settings a little if you need more "wait" time, I tried this on my local web server and it worked, but it's local so the site loads very fast.

on run
    set i to 1
    repeat until i > 5
        do shell script "sleep 2"
        tell application "Safari"
            activate
            tell window 1
                set properties of current tab to {URL:"http://yoururl.com"}
            end tell

        end tell
        do shell script "sleep 3"

        tell application "Safari"
            tell document 1
                do JavaScript "document.getElementById(\"75610556\").click();"
            end tell
            quit
        end tell
        set i to i + 1
    end repeat
end run

另请注意,您需要将网址更改为您的网址,其中显示http://yoururl.com" 并且您可能希望更改 中的 5 重复直到 i >5 到更大的数字,让它重复更多.

Also note, you'll need to change the url to your url where it says "http://yoururl.com" and you'll probably want to change the 5 in repeat until i > 5 to a larger number to let it repeat more.

这篇关于学习 Applescript:无法链接到 Safari的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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