The following code will happily take a selected postcode (that’s British for ‘zip’, by the way) from the clipboard and launch a web page showing a street map for it.
set s to the clipboard as string
set postCode to (change " " into "+" in s)
set streetMapUrl to ("http://www.streetmap.co.uk/streetmap.dll?postcode2map?code=" & postCode & "&title=Street+Map+for+" & s & "&nolocal=y")
open location streetMapUrl
-- opens a streetmap.co.uk page for the selected postcode...
What I really want to do, though, is highlight/select (I mean: what you do /before/ you press Cmd-C) the postcode in any text environment on screen, e.g. Address Book, and then have AppleScript take that highlighted text in as its passed variable.
Any idea how to do this? The Finder dictionary says that Copy isn’t implemented yet…
By the way, I’m using ‘change’ here from SatimageOSAX as a search and replace function.
I won’t say that it’s impossible to get the selection in any/every application via AppleScript but, if it is possible, I doubt that it will be an easy task. I can say with certainty that it’s a task that I won’t attempt unless Apple or some other developer makes it painless.
If you have Apple’s new System Events for GUI scripting installed (available here http://www.apple.com/applescript/GUI/), then you can just do this to get the selected text from any application to the clipboard:
tell application “System Events”
set theProcess to every process whose frontmost is true
tell process theProcess
key down {command}
keystroke "c"
key up {command}
end tell
Everyone should have OnMyCommand installed. Using OMCEdit, add a new Command, set Activation Mode to “Selected or Clipboard Text”, Execution Mode to “Silent (popen)”, Escape Special Characters to “None”, and the Command itself to:
osascript -l AppleScript -e 'open location ("http://www.streetmap.co.uk/streetmap.dll?postcode2map?code=" & (change " " into "+" in "__OBJ_TEXT__") & "&title=Street+Map+for+" & (change " " into "+" in "__OBJ_TEXT__") & "&nolocal=y")'
Enter the text above as one long line. Save the Command. Now, in just about any app, highlight the text you want, then Control-Click on it and select your Command from the “On My Command” contextual menu.
OnMyCommand is incredibly powerful and I can’t recommend it enough.
Thanks very much, folks - I’ve gone with the GUI Processes code, as I’m running the script from Youpi Key - less keystrokes than CMM and select from menu, and I’m lazy that way…
Um, if you’re running this from Youpi Key, why don’t you just make a Sequence that first sends a Command-C keyboard emulation, then runs an AppleScript? No need for GUI Scripting, since Youpi Key can do that for you…
Hah! Yes, I could have done that - it’s just that I’m trying to learn AppleScript and wanted to know if it could be done in script. since it seemed like it was an obvious thing one ought to be able to do. Plus I just didn’t think of that… {8-<>