Learning Applescript: Trouble linking to Safari

拟墨画扇 提交于 2019-12-25 05:07:47

问题


Run Down

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

What I Have

I currently have a script that can do these things:

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

  2. 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
    

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

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

  4. 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
    

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

  5. 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
    

    This closes the current tab.

  6. Next, I loop through with an Automator loop.

What I Would Like Help With

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

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

  2. Is this code efficient? Any errors yet?

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

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

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

  6. I think this is a good way to do things.

Why I Need This + Every Single Detail (Optional Section)

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

So there is a business contest at my college. I'm a freshman programmer, and have programmed a very simple mobile networking game. The issue is there is a cash prize foe $3,000 on a poll website. This website is Poll Everywhere. The judges did not realize that this website is not secure in the slightest. We reported it, and the judges thought it was not good enough of a reason to cancel the poll and maybe search for a new alternative. We reported that Poll Everywhere can be manipulated to work to any teams advantage who was smart enough to:

A. Turn off cookies for their browser

B. Use a private browser

But of course, the judges did not listen. They thought that if somebody stayed up all night, it might change it, but during a 2-3 hour period, it will have little effect. To deter their ruling, I would like to make an example program for them on my Mac that will blow their minds: A voting program. This program will open safari. Safari has already been configured to open the voting page in private browser mode. All my program has to do is open a new tab, find the button ID, and click on it. The faster it does this the better.

  • Final note: Was the back story too much? I know Stack overflow has a "spare the details" policy. I thought the elaborate version might help, but if it is a bad thing I will remove it.

回答1:


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.

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

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.



来源:https://stackoverflow.com/questions/36589438/learning-applescript-trouble-linking-to-safari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!