Kevin
Kevin

Reputation: 1106

AppleScript : Search safari tab and open tab

I would like to find a safari tab, and focus on this tab

This is not working, however the tab is found alright, I just can't open this tab.

tell application "Safari"
    repeat with t in tabs of windows
        tell t
            if name starts with "facebook" then open tab
        end tell
    end repeat
end tell

Also I wonder can I found some text from a another tab without focus on this tab ?

to getInputByClass75(theClass, num)
    tell application "Safari"
        set input to do JavaScript "
document.getElementsByClassName('" & theClass & "')[" & num & "].innerHTML;" in document 1
    end tell
    return input
end getInputByClass75
getInputByClass75("Order", 0)
set theText to Unicode text
set theSource to getInputByClass75("Order", 0)

property leftEdge75 : "<a class=\"columns\" href=\"/Web"
property rightEdge75 : "\">Display</a>"
set saveTID to text item delimiters
set text item delimiters to leftEdge75
set classValue to text item 2 of theSource
set text item delimiters to rightEdge75
set OrderLink to text item 1 of classValue
set text item delimiters to saveTID
OrderLink

Upvotes: 0

Views: 1340

Answers (1)

user309603
user309603

Reputation: 1548

The right phrase to bring a tab to foreground is

tell window x to set current tab to tab y

Try this

tell application "Safari"
    repeat with w in windows
        if name of w is not "" then --in case of zombie windows
            repeat with t in tabs of w
                if name of t starts with "facebook" then
                    tell w to set current tab to t
                    set index of w to 1
                end if
            end repeat
        end if
    end repeat
end tell

--

You can execute javascripts in background tabs. Specify the tab like this:

tell application "Safari"
    do JavaScript "document....." in tab 1 of window 1
end tell

Upvotes: 1

Related Questions