Say I have 5 tabs open in Safari.
After 10 min of executing the script I want to locate the tab that has “compound” in the name in the background (i.e. not activate Safari yet since I am chugging away at other stuff and only want to reload the page)
When it’s there I want to bring it to the front out of all the other tabs (not literally if possible) and reload it.
So far I have been able to identify if the window (not the tab) has the website as the front tab with:
tell application "Safari"
tell application "System Events"
tell process "Safari"
set windowName to name of windows
end tell
end tell
end tell
Event Log:
tell application “System Events”
get name of every window of process “Safari”
{“The Compound”, “Downloads”, “MacScripter BBS | Applescript Forums”…}
end tell
and I have been able to get a site to reload with:
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
keystroke "r" using command down
end tell
end tell
I can’t seem to get AS to get all the names of the tabs of a window
once the correct window is found to execute a reload on that window.
Any ideas?
Thx,
Chip
AppleScript: 2.1
Browser: Safari 2.0.1 build 412.5
Operating System: Mac OS X (10.4)
This may not be exactly what you want, Chip - but hopefully it’ll get close enough to give you some ideas:
to chooseItem from l
set l to (choose from list l with prompt "Choose a tab to refresh:")
if l is false then error number -128
l's item 1
end chooseItem
tell application "System Events" to tell process "Safari" to tell group 2 of window 1
set currentTarget to my (chooseItem from name of buttons)
click button currentTarget
end tell
tell application "Safari" to tell document 1
repeat until name is currentTarget
delay 0.2
end repeat
set URL to URL
repeat while name is currentTarget
delay 0.1
end repeat
repeat while name starts with "Loading"
delay 0.2
end repeat
activate (* or beep, etc. to indicate when ready *)
end tell
How about this development from kai’s idea? I can’t test it thoroughly because my Internet machine doesn’t have GUI scripting, but it seems to be going through all the right motions on the one that does. If Safari’s front window has more than one tab shown, you should get a ‘choose from list’ dialog, followed by a reload of the chosen tab. With one or zero tabs shown, the script should simply reload the front document.
to chooseItem from l
tell application (path to frontmost application as Unicode text)
set l to (choose from list l with prompt "Choose a tab to refresh:")
end tell
if (l is false) then error number -128
l's item 1
end chooseItem
tell application "System Events" to tell process "Safari"
set frontmost to true
set windowName to name of front window
set groupList to groups of front window
repeat with thisGroup in groupList
if (name of thisGroup's buttons contains windowName) then
set tabGroup to thisGroup's contents
set tabNames to name of tabGroup's buttons
if ((count tabNames) > 1) then
set currentTarget to my (chooseItem from tabNames)
click button currentTarget of tabGroup
end if
exit repeat
end if
end repeat
keystroke "r" using command down
end tell
As ever, your consideration of a wider range of possible scenarios sets a shining example to us all, Nigel.
Your version seems to require Safari to be frontmost, and I haven’t quite worked out if there’s a simple way around that. However, you did inspire me to tweak my own effort, to deal with a greater variety of situations. It should now:
to chooseItem from l
set l to (choose from list l with prompt "Choose a tab to reload:")
if l is false then error number -128
l's item 1
end chooseItem
to reloadPage(currentTarget)
tell application "Safari" to tell document 1
repeat until name is currentTarget
delay 0.2
end repeat
set URL to URL
repeat while name is currentTarget
delay 0.1
end repeat
repeat while name starts with "Loading"
delay 0.2
end repeat
activate
end tell
end reloadPage
tell application "System Events" to tell process "Safari" to tell window 1 to if exists then
set winName to name
tell group -2 to if (exists) and winName is in name of buttons then
set btnList to a reference to (buttons whose name does not start with "Untitled")
set btnCount to count btnList
if btnCount > 0 then
if btnCount is 1 then
set currentTarget to name of btnList's item 1
else
set currentTarget to my (chooseItem from btnList's name)
end if
click button currentTarget
return my reloadPage(currentTarget)
end if
end if
tell winName to if it does not start with "Untitled" then my reloadPage(it)
end if
Edit 1: Script modified to take account of the point raised (below) by Nigel, regarding the varying group elements of a Safari window. Edit 2: Further modification to (hopefully) cover Nigel’s subsequent test - with tabbed browsing disabled and the status bar hidden.
OK. This next version actually rewrites Safari’s source code so that… Na. Just kidding.
The reason I used a repeat in my script to discover which group contained the tabs (ie. had a button with with the same name as Safari’s front window) is that ‘group 2’ didn’t work for me. It depends on the bookmarks bar not being displayed. If it is, then the tabs are ‘group 3’. I believe I’m gradually building up a picture of your Safari preferences, Watson…
:lol::lol::lol: Not that I’d put it past you, mind…
Ah yes - excellent point, Nigel! (I’ve just been doing some UI scripting on a couple of apps that have pretty much fixed window elements - and I’d neglected to consider how Safari’s view options might impact on this.) However, there might be another way around the problem, such as… counting the groups from the other end, for example.
I’ve edited the above script accordingly - so I hope it works a bit better.
Hi, kai. Ah ” it still has problems here if tabbed browsing isn’t enabled AND the status bar is hidden. Under these circumstances, the items in the bookmarks bar are offered for selection if it’s shown, or there’s an “NSReceiverEvaluationScriptError: 4” error if it isn’t. I can’t find any other combination of settings that will break your script, though.
Looks like you got me again, Nigel! (I did test for a variety of different conditions - but obviously missed those particular combinations.)
It occurs to me that one possible way forward could be a combination of both your approach and mine. Hopefully, using your window name checking technique on the window’s penultimate group might still avoid a repeat loop. (Again, to avoid reposting the script, I’ve simply modified the one above…)
BTW, what’s your charge for such comprehensive beta-testing?
Woohoo! Thanks, Nigel - another triumph for team effort!
A golden opportunity surely must be just around the corner (that is, unless Shakespeare country has suddenly been taken over by myopic meatballs)… :rolleyes: