【问题标题】:Applescript for Safari AUTOMATION -- any advice, information, tip etc --Applescript for Safari AUTOMATION——任何建议、信息、提示等——
【发布时间】:2022-01-11 17:27:38
【问题描述】:

PreNote:我对任何信息、建议、提示等都很开放并渴望。

大家好!

我正在尝试使用 applescript 创建自动化。这是我的第一个个人 applescript 任务,但我有一些有价值的问题。基本上我正在尝试从网站捕获实时通知并将它们显示在 mac os 通知中。

我正在尝试构建过程几天,但我不想给你带来混乱:)所以我在下面大致解释了我的过程。

(* Variables used in whole process

set $webToCheck > This is Safari webpage which I want to run my script on it. It won't be front window, script should be run with its name or other property.

set $theClass > This is class name of DOM element to check if it is exist or not. This class is not always exist on DOM of $webpage. It comes with notifications so when use it in "do Javascript" I got error "variable is not defined"

set $num > number of class to use in "do Javascript"

set $input > variable to assign HTML text

set $modifiedInput > Text of input seperated from HTML tags

*)


        -- Step 1

tell application "Safari"

work on $webToCheck

        -- Step 2

repeat until $input is not empty

set input do Javascript

document.getElementsByClassName > $theClass, $num of $webToCheck

end repeat

        -- Step 3

modify text of $input to seperate from RAW HTML -- For example: <a class="" value=""> TEXT to be seperated </a>

Display notification $modifiedInput

        -- Step 4

Go back to step 1 or 2 to check and display notification again

【问题讨论】:

    标签: macos applescript automator javascript-automation


    【解决方案1】:

    虽然 Stack Overflow 不是代码编写服务,但我已尽力满足您的要求。首先,这里有一些一般性的提示:

    1. Applescript 不接受变量名开头的$

    2. 您要查找的变量赋值是set {variable} to {value}。您可以选择在赋值结束时使用as {class} 阐明变量的类。

    3. work on {URL} 不会聚焦某个网站,但与 Applescript 中大多数面向对象的事情一样,tell-statement。它将显示在完整的解决方案中。

    4. Applescript 中的文本连接使用&amp;。所以像"Hello " &amp; "World" 这样的东西是标准的做法。

    5. Applescript 中大部分内容的修改都发生在set

    6. 使用innerText 而不是innerHTML 更容易,因为Applescript 中的分割文本是a bit of a pain

    7. 没有goto,但您可以将前几步封装到一个函数中,在Applescript 中使用onto 声明。

    这是完整的代码,其中包含一些文档:

    global webToCheck, theClass, num, input --This says that all variables can be used even in functions.
    
    set webToCheck to "youtube.com" --Strings can only use double quotes.
    
    set theClass to "style-scope yt-alert-with-actions-renderer" --I will use an actual demo to prove that it is working
    
    set num to 0 as integer -- This is the type declaration I was talking about. For numbers we have integer, real(float) and number.
    
    set input to "" -- You don't have define everything at the top, but I will do so for this.
    
    on displayNotification()
        tell application "Safari"
            tell window 1 -- This will target only the first window. For multiple windows you would have to write a repeat with-loop(for-loop), which I'm not going to do, for the sake of simplicity.
                tell (first tab whose URL contains webToCheck) -- This targets just the first tab which contains the webToCheck variable.
                    set input to do JavaScript "document.getElementsByClassName('" & theClass & "')[" & num & "].innerText" -- This is the way I would go about writing the Javascript. I think you had something different in mind, but this works for my example.
                    display notification (paragraph 1 of input) with title webToCheck -- This displays the first line of my input since that is the relevant part. I also set a title so I doesn't just say "Script Editor"
                end tell
            end tell
        end tell
    end displayNotification
    
    
    repeat 4 times -- I think this is quite obvious. Adjust this to your needs.
        displayNotification()
        delay 4
    end repeat
    

    在一段时间没有在 Safari 上使用 youtube 的情况下运行它会显示:

    请注意,这不是最优雅的解决方案,但我认为它是可读的,并且(希望)可以满足您的需求。

    【讨论】: