-2

PreNote: I am open and hungry for any information, advice, tip etc.

Hello Everyone!

I am trying to create automation with applescript. This is my first personal applescript task but I have some valuable questions. Basically I am trying to catch live notifications from a website and display them in mac os notification.

I am trying to build process for a few days but I don't want to give a mess to you :) so I have roughly explained my process below.

(* 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

1 Answers1

1

First of all, here are some general tips though:

  1. Applescript won't accept $ at the start of variable names.

  2. The variable assignment you are looking for is set {variable} to {value}. You can optionally at the end of it clarify the variable's class using as {class} at the end of the assignment.

  3. Focusing a certain website does not happen with work on {URL} but as with most object oriented things in Applescript with the tell-statement. It will be shown in the full solution.

  4. Text concatenation in Applescript happens with &. So something like "Hello " & "World" is the standard way to do it.

  5. Modification of most things in Applescript happens with set.

  6. It is easier to use innerText instead of innerHTML as splitting text in Applescript is a bit of a pain.

  7. There is no goto but you could wrap the first few steps into a function, which are declared with on or to in Applescript.

Here is the full code with some documentation sprinkled in there:

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

Running this while having not used youtube on Safari in a while it displays this:

Desired Notification

Note that this isn't the most elegant solution, but I think it is readable and it (hopefully) works for your needs.

Dharman
  • 30,962
  • 25
  • 85
  • 135
gurkensaas
  • 793
  • 1
  • 5
  • 29