AppleScript에서 스크롤 막대 위치를 변경하는 방법? AppleScript .scpt 파일이 있습니다.

타사 응용 프로그램 인 FastScripts를 통해 키보드 바로 가기에 할당 된 AppleScript .scpt 파일이 있습니다.

이 스크립트는 새로운 Chrome 탭에서 특정의 고정 된 사전 결정된 URL을 엽니 다. 이 새 탭은 현재 열려있는 Google 크롬 탭 바로 다음에 삽입 (Google 크롬이 현재 열려있는 경우).

다음은이 코드입니다.

activate application "Google Chrome"

set myURL to "https://weather.com/weather/hourbyhour/l/12345"

tell application "Google Chrome"
    activate
    tell front window to make new tab at after (get active tab) with properties {URL:myURL}
end tell

이 스크립트에서 구현하고자하는 추가 작업은 Chrome에서 열린 웹 페이지의 스크롤 막대를 자동으로 아래쪽으로 약간 이동하는 것입니다. URL 변수와 마찬가지로 스크롤 양은 구체적이고 고정적이며 사전 결정됩니다.


저는 서면으로 스크롤 량을 표현할 수있는 적절한 방법을 모르겠습니다. (물론 화면 해상도, 모니터 크기, 창 크기 및 해당 페이지에 대한 Chrome의 확대 / 축소 설정에 따라 다릅니다. 내 독특한 환경 때문에 페이지 아래로 약 15 % 정도라고 말할 수 있습니다. 이 설명의 의미를 명확히하기 위해 “페이지 아래로 100 %”는 가장 낮은 스크롤 바 위치에 있습니다 (스크롤바는이 위치로 설정할 수 있습니다). ⌘ 명령 + ).

내가 원하는 정확한 스크롤 량은 내 질문의 맥락에서 특히 관련이 없다 (스크롤 량에 영향을 줄 수있는 많은 변수를 고려한). 페이지의 맨 위 또는 맨 아래에있는 것과는 대조적으로 스크롤 양은 사용자 정의 숫자라는 점에 유의해야합니다 (하나의 키보드 단축키를 key code AppleScript로이 두 위치로 쉽게 스크롤 할 수 있습니다.)

원하는 솔루션이 무엇이든지간에 원하는 스크롤바 위치를 미세 조정하고 조정할 수있는 기능을 제공하여 최대한 수동으로 스크롤 할 수있는 위치에 가깝게 할 수 있기를 바랍니다.



답변

당신은 자바 스크립트 스크롤 양을 설정하십시오.

정의 된 비율로 스크롤을 조정하려면 :

set myURL to "https://weather.com/weather/hourbyhour/l/12345"
set scrollAmount to "16" --- % down the page

tell application "Google Chrome"
    activate
    tell front window to set curTab to make new tab at after (get active tab) with properties {URL:myURL}
    tell curTab
        repeat while (loading)
            delay 1
        end repeat
        -- set the vertical scroll
        execute javascript "h=document.documentElement.scrollHeight-document.documentElement.clientHeight; window.scrollTo(0,h*" & scrollAmount & "/100)"
    end tell
end tell

페이지의 요소 맨 위에서 세로 스크롤을 조정하려면 스크립트에서 다음을 사용하여 요소를 가져올 수 있습니다. document.getElementById('twc-scrollabe') twc-scrollable 테이블의 식별자입니다.

그 후, 자바 스크립트 루프를 사용하여 페이지의 표 상단을 가져옵니다.

set myURL to "https://weather.com/weather/hourbyhour/l/12345"
tell application "Google Chrome"
    activate
    tell front window to set curTab to make new tab at after (get active tab) with properties {URL:myURL}
    tell curTab
        repeat while (loading)
            delay 1
        end repeat
        -- scroll to the top of the table
        execute javascript "e=document.getElementById('twc-scrollabe');tTop=0; do {tTop +=e.offsetTop || 0; e=e.offsetParent} while(e); window.scrollTo(0,tTop);"
    end tell
end tell

이 문제 자바 스크립트 (다른 URL에서) 페이지의 소스에서 식별자를 찾아서 스크립트에서 변경해야합니다. 요소에 식별자가 없으면 다른 메소드를 사용하여 요소를 가져와야합니다. JavasScript 코드는 사이트의 페이지에.


답변

의견에서 제공 한 추가 정보를 바탕으로 다음 예제를 제공하겠습니다. 암호 내가 어떻게 할 수 있는지. (적어도 더 좋은 방법을 알게 될 때까지.)

set myURL to "https://weather.com/weather/hourbyhour/l/12345"

tell application "Google Chrome"
    set bounds of front window to {0, 23, 1440, 850}
    tell front window to make new tab at after (get active tab) with properties {URL:myURL}
    repeat while (loading of active tab of front window is true)
        delay 1
    end repeat
    activate
    tell application "System Events"
        repeat 9 times
            key code 125 -- # Down Arrow
            delay 0.1
        end repeat
    end tell
end tell

노트:

  • 그만큼 ~의 delay 명령 ~ 안에 repeat while ... 고리 페이지가 실제로 얼마나 빨리로드되는지에 따라 적절하게 조정될 수 있습니다. 행사 트리거가 불필요하게 트리거되지 않도록 행사 . 페이지로드가 완료되어야합니다. 그렇지 않으면 다음 페이지가로드됩니다. repeat 고리 ~하지 않을 것이다 스크롤 적절한 금액. (나는 분명히 용어를 사용하고있다. 스크롤 비 유적으로.)

  • 에서 tell application "System Events" 문 / 블록 , 시간을 조정해야 할 수도 있습니다 repeat 고리 트리거. 그만큼 delay 명령 이걸로 repeat 고리 그렇지 않으면 필요하다. key code 행사 이 경우 페이지에 제대로 등록되지 않습니다.