MacOS – OSX Services – character substitution + for _

automatormacosservices

I've created a service to use primarily in a browser for searching IMDB, so instead of copying and pasting the film's name I just select the text and run the service I created which will automatically search IMDB for the selected text. This works perfect.

enter image description here

I was hoping to do the same for Wikipedia and so i've been trying to modify the same service but have come across a problem. The search query places a "+" between each word.

This doesn't work for Wikipedia. They place underscores between their words.

enter image description here

I wondered how I can substitute the + for _?
This is what I currently have

open "https://en.wikipedia.org/wiki/$(ruby -rcgi -e 'print CGI.escape $<.read.chomp')"

Kinds regards and Merry Christmas 🙂

Best Answer

WikiPedia places underscores for its entry URLs, but you can search any string (actually with spaces) with this URL scheme:

http://en.wikipedia.org/w/index.php?search=[query]

Replace [query] with you search query.

Try this for example: Search for "stack overflow"

It redirects to correct page. If it can't determine which entry to show, it shows a list of possible entries you can select from: Search for "stack overfloww".

TL;DR

Moreover, if you separate words in query with +, it works just fine. So all you need to do is replace

                               ↓↓↓↓↓↓
open "https://en.wikipedia.org/wiki/$(ruby -rcgi -e 'print CGI.escape $<.read.chomp')"

with this:

                               ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
open "https://en.wikipedia.org/w/index.php?search=$(ruby -rcgi -e 'print CGI.escape $<.read.chomp')"