Capture all tabs in Safari as URLs to the clipboard

Sometimes you’re doing research and have a pile of tabs (and windows) open and need to get them into a document to share with coworkers… so I went digging and tweaking and found an Applescript that does the job:

tell application “Safari”
set docText to “”
set windowCount to count (every window where visible is true)
repeat with x from 1 to windowCount
set tabCount to number of tabs in window x
repeat with y from 1 to tabCount
set tabName to name of tab y of window x
set tabURL to URL of tab y of window x as string
set docText to docText & tabName & ” – ” & tabURL & linefeed as string
end repeat
set the clipboard to the docText
end repeat
end tell

You can save it as a standalone script and run it from there, or stick into a script item in Automator and run it as a service.. or even call it from the command line as an Osascript.

Looking up Mac specs..

Did I mention I love TextExpander? Yeah.. so, to make a long story short: clients ask me questions about their computers, upgrades, lifespans, etc. I don’t normally remember all the details of their Macs so I get them to send me the Serial Number (Open the Apple menu, click About This Mac and it should be right there.)

Once you have it you can go by Apple’s support site and look it up… but that takes too long.

Make a new TextExpander snippet with Applescript as the content type:
property theURL : ""
set theURL to "http://support.apple.com/specs/#" & (the clipboard)
do shell script "open " & theURL

Then you just copy the serial number, and anywhere you can type use !specs to invoke it and up pops open Apple spec page a few seconds later.

Controlling iTunes from the command line

I had a little fun the other day, making a script that would pause iTunes, from the command line. In the end I made ones to pause, play, and go forward a track.

All of them use a slightly modified version of a line of Applescript:

tell application “iTunes” to play

You can also tell it “to next track, to pause, to stop, to previous track”, which all work as expected.

To get it to work on the command line you’ll need to turn it into a script, which is a plain text file with some command line options in it. To get Applescript to work on the command line we need to use osascript, with the “-e” option.

Here’s what you should have in your file:

#!/bin/bash
osascript -e “Tell application “iTunes” to pause”

Note the the quotes and slashes? You need to wrap the Applescript command in quotes, and since you have quotes in the middle of it, you need to escape them using the slashes so the whole applescript is parsed.

Next step is to save the script to somewhere useful. In my case I have set my command line environment to check for scripts in ~/bin/ , so I saved it there as ‘pause’. After that you will need to make it an executable, so pop into the terminal and do “chmod +x ~/path-to-your-file/pause”, at which point you run it.

Go play something in iTunes, and then go over to the Terminal and type “pause” and hit enter. If your script is in your environment path then iTunes just paused…

Here are the other scripts I used, just to make your life easier 😉

#!/bin/bash
osascript -e “Tell application “iTunes” to play”

#!/bin/bash
osascript -e “Tell application “iTunes” to next track”

Feedback is appreciated!

AppleScript 2.0!

Thanks to df for pointing out that Apple has posted release notes for AppleScript in Leopard.We can now ask if an application is running, without AppleScript launching it to find out. ;-)Some nice additions to running AppleScript on the Command Line:

  • use # to comment out a line
  • start the script with #!/usr/bin/osascript, and make it executable, will enable it to be run in the shell
  • osadecompile is a command line script to display compiled scripts as text

Now osascript also supports additional arguments on the command line, so now you can run a script and provide strings for it to use. see the osascript man page for details, and an example. This feature was available in Tiger, I just never noticed until now!