Useful script examples for Safari

Aug 04, '10 07:30:00AM

Contributed by: nathanator11

I was working with AppleScript for Safari, and decided to make a script that demonstrated some of the handy things you could do. Hope they prove as useful to others as they were for me.

Here is the examples script. Feel free to pull parts out and place them in your own scripts as needed. There is code for manipulating tabs, setting bookmarks, and emailing web pages.

tell application "Safari"
  
  set currentTab to current tab of front window --if you have multiple windows, controlling/choosing them works much the same way as tabs
  set currentName to currentTab's name
  log currentName
  set currentURL to currentTab's URL
  log currentURL
  (*set currentText to currentTab's text
  log currentText*)
  (*set currentSource to currentTab's source
  log currentSource*)
  --these are commented out because they return, of course, very long strings
  set currentVisi to currentTab's visible
  log currentVisi
  
  set URL of currentTab to "http://www.google.com" --replcaces current URL. other properties are read-only.
  
  set twoName to (tab 2 of front window)'s name
  log twoName
  set lastName to (last tab of front window)'s name
  log lastName
  
  set currentTabNum to index of current tab of front window
  log currentTabNum --the selected tab can't be the first or last
  set nextTab to (tab (currentTabNum + 1) of front window)
  set nextName to nextTab's name
  log nextName
  set prevTab to (tab (currentTabNum - 1) of front window)
  set prevName to prevTab's name
  log prevName
  
  set tabsList to front window's tabs as list --gets all open tabs
  set tabsInfo to {} as list
  repeat with currTab in tabsList
    set currName to currTab's name
    set currURL to currTab's URL
    set currNum to currTab's index
    set tabsInfo to tabsInfo {} as list
  end repeat
  log tabsInfo
  log _name of item 2 of tabsInfo
  --this creates a list of records about the tabs you have open.
  
  close current tab of front window
  if exists tab 3 of front window then beep
  make new tab at (end of front window)
  make new tab at (tab 4 of front window) --this has to be a tab number that doesn't already exist
  
  open (((path to desktop as string) & "file.txt") as string) --obviously, this has to be a real file. can be alias or string
  
  email contents of (current tab of front window) --only works with some mail clients
  show bookmarks
  set URL of (make new tab at end of front window) to "topsites://" --you can nest things like using parentheses.
  
end tell


[crarko adds: I haven't tested this one beyond compiling the script. To dig further into scripting Safari use the AppleScript Editor to open up Safari's Dictionary. The Type Definitions and the Safari Suite are areas to look for Safari-specific data and operations that are scriptable.]

Comments (2)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20100803094600903