January 11, 2005

Safari Applescript Tab Manipulation Code

I'm hip deep in writing something but I thought I would provide a bit of cheer for those delving into Applescript and looking to manipulate Safari.

In Apple's Applescript page devoted to Safari, they have a download link for a collection of sample scripts. One of them includes the following routine:

on new_tab()
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "New Tab" of menu "File" of menu bar 1
end tell
end tell
end new_tab

I saw that the ability to make tabs was nice but you needed to navigate as well as close them to. Here's what I've added so far:

on next_tab()
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "Select Next Tab" of menu "Window" of menu bar 1
end tell
end tell
end next_tab

on prev_tab()
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "Select Previous Tab" of menu "Window" of menu bar 1
end tell
end tell
end prev_tab

on close_tab()
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "Close Tab" of menu "File" of menu bar 1
end tell
end tell

end close_tab

What needs doing next is to be able to jump around between tabs without cycling through each one. This is for a long-term project that will allow me to pull job databases in a manner so that I can track them and it won't work so fast that some admin on the other end gets spooked and shifts things around to break my code.

Posted by TMLutas at January 11, 2005 10:00 PM