Kevin
Kevin

Reputation: 1106

AppleScript : find and set window

I'm try to create a small script to find a safari window based on its URL.

I made this

property checkURL : "https://www.apple.com"
repeat with i in {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    try
        tell application "Safari" to set theTab to (first tab of window i whose URL contains checkURL)
        tell front window of application "Safari"
            set current tab to theTab
        end tell
    on error
        --
    end try
end repeat

I'm sur I got it working and one stage but it doesn't seems to work anymore

return :

tell application "Safari"
    get tab 1 of window 1 whose URL contains "https://www.apple.com"
        --> tab 1 of window id 434
    set current tab of window 1 to tab 1 of window id 434
    get tab 1 of window 2 whose URL contains "https://www.apple.com"
        --> error number -1719 from tab 1 of window 2 whose URL contains "https://acmp.corp.apple.com"
    get tab 1 of window 3 whose URL contains "https://www.apple.com"
        --> error number -1719 from window 3
    get tab 1 of window 4 whose URL contains "https://www.apple.com"
        --> error number -1719 from window 4
    get tab 1 of window 5 whose URL contains "https://www.apple.com"
        --> error number -1719 from window 5
    get tab 1 of window 6 whose URL contains "https://www.apple.com"
        --> error number -1719 from window 6
    get tab 1 of window 7 whose URL contains "https://www.apple.com"
        --> error number -1719 from window 7
    get tab 1 of window 8 whose URL contains "https://www.apple.com"
        --> error number -1719 from window 8
    get tab 1 of window 9 whose URL contains "https://www.apple.com"
        --> error number -1719 from window 9
    get tab 1 of window 10 whose URL contains "https://www.apple.com"
        --> error number -1719 from window 10
end tell

PS: I know I should replace the 1/10 list with a list based on the number of window like

tell application "Safari"
    set numberOfSafariWindows to length of (get current tab of windows)
end tell
set nlist to {}
repeat with n from 0 to numberOfSafariWindows
    set nlist2 to {n + 1}
    set nlist to nlist & nlist2
end repeat

Upvotes: 0

Views: 202

Answers (2)

Chino22
Chino22

Reputation: 165

Avoid counting when not useful.

set theUrl to "https://www.apple.com"
tell application "Safari"
    repeat with thisWindow in every window
        repeat with thisTab in (every tab of thisWindow whose URL contains theUrl)
            set current tab of thisWindow to thisTab
        end repeat
    end repeat
end tell

Upvotes: 1

Kevin
Kevin

Reputation: 1106

I changed the code and it's seems to work

tell application "Safari"
    --Variables
    set windowCount to number of windows
    set docText to ""
    --Repeat for Every Window
    repeat with x from 1 to windowCount
        set tabcount to number of tabs in window x
        --Repeat for Every Tab in Current Window
        repeat with y from 1 to tabcount
            --Get Tab Name & URL
            set tabName to name of tab y of window x
            set tabURL to URL of tab y of window x
            if tabURL contains "https://www.apple.com" then
                tell application "Safari"
                    tell window x
                        set current tab to tab y
                    end tell
                end tell
            end if
        end repeat
    end repeat
end tell

Upvotes: 0

Related Questions