본문 바로가기
Programming/Hammerspoon

Arc 브라우저 제어하기

by NAMP 2023. 10. 20.

애플스크립트 실행

Applescript 를 사용하여, Arc 브라우저를 제어한다.
hs.osascript를 사용하여 애플스크립트를 실행한다.

먼저, Arc 브라우저의 버전정보를 가져오는 스크립트를 스크립트 편집기에서 실행해본다.

tell application "Arc"
  return version
end tell

이 스크립트를 Hammerspoon 에서 호출하는 코드를 작성한다.

function obj:getVersion()
  local success, res, desc = hs.osascript([[
    tell application "Arc"
      return version
    end tell
  ]]);
  return res;
end

탭 생성

function obj:makeNewTab(url)
  local success, res, desc = hs.osascript([[
  tell application "Arc"
    tell front window
      make new tab with properties {URL:"]] .. url .. [["}
    end tell
    activate
  end tell
  ]]);
  return success
end

make new tab with properties {URL:url}을 사용하여 새 탭을 생성한다.

탭 찾기

function obj:findTab(url)
  local success, res, desc = hs.osascript([[
    set _output to ""

    tell application "Arc"
      set _window_index to 1

      repeat with _window in windows
        set _tab_index to 1

        repeat with _tab in tabs of _window
          set _url to get URL of _tab

          if _url contains "]] .. url .. [[" then
            set _title to get title of _tab
            set _location to get location of _tab

            set _output to (_output & "{ \"title\": \"" & _title & "\", \"url\": \"" & _url & "\", \"windowId\": " & _window_index & ", \"tabId\": " & _tab_index & " , \"location\": \"" & _location & "\" },")

            tell window _window_index
              tell tab _tab_index to select
            end tell
            activate

            return _output
          end if

          set _tab_index to _tab_index + 1

        end repeat

      end repeat
    end tell

    return _output
    ]]);

  return res;
end

모든 윈도우, 탭을 확인해서, 원하는 URL 을 포함하고 있는지 확인한다.
있다면 해당 탭을 활성화한다.

탭 목록 가져오기

function obj:getTabs()
  local success, res, desc = hs.osascript([[
    on escape_value(this_text)
      set AppleScript's text item delimiters to the "\\"
      set the item_list to every text item of this_text
      set AppleScript's text item delimiters to the "\\\\\\"
      set this_text to the item_list as string
      set AppleScript's text item delimiters to ""
      return this_text
    end replace_chars

    set _output to ""

    tell application "Arc"
      set _window_index to 1

      repeat with _window in windows
        set _tab_index to 1

        repeat with _tab in tabs of _window
          set _url to get URL of _tab
          set _title to my escape_value(get title of _tab)
          set _location to get location of _tab

          set _output to (_output & "{ \"title\": \"" & _title & "\", \"url\": \"" & _url & "\", \"windowId\": " & _window_index & ", \"tabId\": " & _tab_index & " , \"location\": \"" & _location & "\" },")
          set _tab_index to _tab_index + 1
        end repeat

        set _window_index to _window_index + 1
      end repeat
    end tell

    return "[" & _output & "]"
    ]]);

  local parsed = hs.json.decode(res)
  return parsed;
end

모든 윈도우, 탭을 돌면서, 하나의 문자열을 만든다.
이후 json.decode를 통하여 table로 변환하여 반환한다.

탭에서, title, url, windowId, tabId 정보를 가져온다.

탭 활성화

function obj:activate(win_index, tab_index)
  print('index', win_index, tab_index)
  local success, res, desc = hs.osascript([[
    set win_index to ]] .. win_index .. [[ -- 뒤에 문자열 필요
    set tab_index to ]] .. tab_index .. [[ -- 뒤에 문자열 필요

    tell application "Arc"
      tell window win_index
        tell tab tab_index to select
      end tell
      activate
    end tell
  ]]);
end

win_index, tab_index 를 전달받아서, 해당 탭을 활성화한다.

탭 선택 목록 만들기

function obj:list_tab_choices()
  local tabChoices = {}
  local tabs = obj:getTabs()

  for _, t in pairs(tabs) do
    table.insert(tabChoices, {
      text = t['title'] .. "--" .. t['url'],
      subText = t['url'],
      uuid = t['windowId'] .. "-" .. t['tabId'],
      win_index = t['windowId'],
      tab_index = t['tabId']
    })
  end
  return tabChoices;
end

탭 선택화면을 만들기위한 table 정보를 생성한다.

참고

 

 

 

댓글