I made a script to find an tab based on it's URL and set a new URL (the tab is not focused)

tell application "Safari"
    set topWindows to every window whose name is not ""
    set numWindows to the number of topWindows
    set didFind to false
    set targetUrl to "https://www.apple.com"

    repeat with x from 1 to numWindows
        set numTabs to the number of tabs in window x
        repeat with y from 1 to numTabs
            set tabUrl to the URL of tab y of window x
            if tabUrl contains targetUrl then
                set didFind to true
                tell window x to set current tab to tab y
            end if
        end repeat
    end repeat

end tell

set myLink to "https://www.apple.fr"

tell application "Safari"
    tell front window
        set URL of tab y to myLink
    end tell
end tell

The correct tab is found, the index is found, but it's seems to always open the URL in a the latest tab instead of the existing tab

how can I fix this?

  • Related: apple.stackexchange.com/a/333845/263848 – JBis Dec 8 '18 at 14:51
  • Couple suggestions: 1. Try to use good variable names. Instead of x, y, etc. use actual names. 2. When using indexed loops (` x from 1 to numWindows) traditionally the the variable i` is used to represent the "index". 3. Unless you need to change the content of the the array you are iterating through or need to limit it some how, use for each loops. This eliminates the use of the index. So instead of repeat with x from 1 to the number of theWindows use repeat with aWindow in theWindows. 4. Use functions – JBis Dec 8 '18 at 19:28
  • @JBis By "functions", do you in fact mean "handlers" ? – CJK Dec 9 '18 at 11:23
set targetUrl to "https://www.apple.com"
use currentApp : application "Safari"
set _T to a reference to (every tab of every window of currentApp whose ¬
    URL contains targetUrl)

if (count _T) > 0 then return viewTab(_T)

to viewTab(aURL)
    set [aURL] to aURL
    set W to the first window of currentApp whose tabs contains aURL

    set current tab of W to aURL
    activate currentApp
    set the index of W to 1
end viewTab


  • I don't manage to set the second URL using your script though, the second URL should be referenced with aURL right? – Kevin Dec 8 '18 at 16:47
  • @Kevin viewTab is a function that accepts a URL as a parameter/argument. Any URL passed to it will make the tab with that URL active. You have a couple issues with your script which I'll address in a later comment. – JBis Dec 8 '18 at 16:53
  • @JBis You said: "viewTab is a function that accepts a URL as a parameter/argument", which isn't correct from what I'm seeing, as a) you send as its parameter a reference to a collection of collections of tabs, namely _T; and b) if you did send a simple URL as the parameter, set current tab of W to aURL would throw an error, as it is expecting a tab object. – CJK Dec 9 '18 at 11:27
  • @CJK Ah, i see what it's doing now. May i ask why you pass by reference instead of value? – JBis Dec 9 '18 at 14:36
  • 1
    @CJK Ah, knew there had to be some background in other languages. 99% of people who use AppleScript don't have deeper understanding of anything relating to computer science, because most who do wouldn't use AppleScript. Many will know the basic loops, functions, conditionals, etc. But passing by references vs value is more of an "advanced" topic in my mind. Also the way you code is...well...good. – JBis Dec 9 '18 at 15:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .