Recently, in Coda, I wanted to create a simple way to look up a word directly in the CodeIgniter documentation. While the reference books are nice, there are multiple PHP documentation sources one might want to look up while working. For example, one might want to look up a native PHP function or, if working in Drupal or WordPress, look up something at those particular websites. So, using example code from others, I wrote up a quick AppleScript that allows one to look up the selected word in the CodeIgniter docs (which is really just a Google site search on the codeigniter.com domain). Without further ado, here it is:
tell application "Coda 2"
try
tell selected split of selected tab of front window
if selected text is not equal to "" then
set someText to selected text
end if
set myQuery to do shell script "php -r 'echo trim(urlencode(" & "\"" & someText & "" & "\"));'"
end tell
end try
end tell
tell application "System Events"
try
if myQuery is not equal to "" then
open location "https://www.google.com/search?as_sitesearch=codeigniter.com%2Fuser_guide%2F&q=" & myQuery & "&sa=Go"
end if
on error
beep
end try
end tell
Very simple stuff! Basically, it looks in the active split and grabs the selected text if any is available. From there it uses PHP to encode the text for a URL (to deal with any problem special characters) and then, if there is resulting text, opens the location:
https://www.google.com/search?as_sitesearch=codeigniter.com%2Fuser_guide%2F&q=[selected_text]
Again, nothing special. I have no doubt that some nice bells and whistles could be added to this script. One thing that isn’t immediately obvious to me is how to select a word that the cursor is in using AppleScript.
AppleScripts in the Coda AppleScript menu can be put into folders so I can have a Documentation folder and then have as many sources as I need nested in a logical way.