LoginSignup
4
6

More than 5 years have passed since last update.

AppleScriptでブラウザ(Safari)で開いているリンクとタイトルを取得

Posted at

肝はこの辺・・・

配列の定義と要素追加
--定義
set linkArray to {}

--要素の追加
set the end of linkArray to {urlTitle:tabName, urlLink:tabURL}
アプリケーション(Safari)に指示を出す
tell application "Safari"

end tell
1つのウインドウ内にあるタブの数
-- tell application "Safari"内で・・・
set tabcount to number of tabs in window inWindowNumber
指定回数のループ
--1〜タブ数までループ、変数yがインクリメントされる
repeat with y from 1 to タブ数

end repeat
コード全体
-- ========================================
-- 戻り値:配列
--   key : urlTitle
--   key : urlLink
-- ========================================
to tellSafariToGetURLsOfWindowByWindowNumber(inWindowNumber)
    set linkArray to {}
    tell application "Safari"
        set tabcount to number of tabs in window inWindowNumber
        try
            set tabcount to number of tabs in window inWindowNumber

            repeat with y from 1 to tabcount
                --Get Tab Name & URL
                set tabName to name of tab y of window inWindowNumber
                set tabURL to URL of tab y of window inWindowNumber
                set the end of linkArray to {urlTitle:tabName, urlLink:tabURL}
            end repeat
        end try
    end tell
    return linkArray
end tellSafariToGetURLsOfWindowByWindowNumber
実行
tellSafariToGetURLsOfWindowByWindowNumber(1)
結果

qiitaのページをタブで2つ開いたウインドウの結果

{{urlTitle:"ホーム - Qiita", urlLink:"http://qiita.com/"}, {urlTitle:"ホーム - Qiita", urlLink:"http://qiita.com/"}}
4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6