# Is it possible to Import urls not type “”

These days I often copy urls from Internet Explorer's address bar. When I paste the url into a Mathematica cell it is neither a string nor a expression.

I always manually add " " to get

 Import@"http://mathematica.stackexchange.com/search?q=load+file"


Another method is to use InputString[]

sometimes, I don't like the pop-up dialog.

Is there any more lazy way to make

Import@http://mathematica.stackexchange.com/search?q=load+file


successful, maybe by using a button, or InputField, or other setting, or some styled-cell?

-

You can create styled cells with a custom CellEvaluationFunction to do this (similar to this R cell answer). As a simple example:

URLCell[] := Module[{},
CellPrint@Cell[TextData[""], "Program",
Evaluatable -> True,
CellEvaluationFunction -> (Import@First@FrontEndExecute[FrontEndExportPacket[Cell[#], "InputText"]] &),
CellGroupingRules -> "InputGrouping"
];
SelectionMove[EvaluationNotebook[], All, EvaluationCell];
NotebookDelete[];
SelectionMove[EvaluationNotebook[], Next, CellContents]
]


Now call URLCell[] and it should replace that line with a cell where you can just paste the URL directly and evaluate:

You can reuse the same cell, or if you want to use it in a stylesheet, see the linked answer above.

-
ahh, interesting, cool.thanks –  HyperGroups May 23 '13 at 2:46
Looks more complicated than just type two double quotes. Not your fault, though +1. –  Sjoerd C. de Vries May 23 '13 at 19:07

Why not just make a palette button? It's simpler and more reusable than a custom cell style.

Simple way:

PasteButton[Defer[Import["\[SelectionPlaceholder]"]]]


More complicated way:

Button["Paste URL Import",
Module[{data, strip},
data = NotebookGet[ClipboardNotebook[]][[1, 1, 1]];
NotebookWrite[InputNotebook[], "Import[\"" <> data <> "\"]"]]]]


You can wrap these in a CreatePalette` if you wish, which will create palette. Then you ca use Palettes -> Install Palette... to install it for permanent use.

If you want to paste tabular data from a website, I recommend that you use Firefox (which allows you to Control-select an arbitrary rectangular part of a table), and then use the tool here to paste it. Search for "tabular" on that page.

-
yeahp, I forgot the Clipboard function, a good way. Ctrl+V should be instead by ButtonClick. –  HyperGroups May 24 '13 at 2:52

Good for you folks for doing it in Mathematica! But there is a good argument for making this kind of improvement to your system at a lower, system-wide level (no matter what XKCD says). If, for example, you're constantly wanting to insert the URL of the current web page into another document, it makes sense (to me) to make this happen for all applications that you work with (text editors, email, forms on web pages, and so on).

Naturally the solution will be platform specific - on Mac OS I use either Keyboard Maestro or TextExpander. I'm sure there are many more solutions for Windows. Also, the solution will probably involve some scripting - AppleScript, Perl, Python, or Ruby for Mac OS.

You can imagine how much easier writing is when you don't have to switch between applications, and when you can use any application in the same way.

(Sorry for large-ish GIF. It would be cool to embed YouTube movies...:)

-
well, seems i don't understand this answer clearly. I have a thought is that there is an dynamic variable q which monitor the clipboard. –  HyperGroups May 24 '13 at 3:57
@HyperGroups No, this is a non-Mathematica solution that works in any application - and just happens to work in Mathematica too. –  cormullion May 24 '13 at 6:35
May I ask how did you record this animation? –  shrx Jun 12 '13 at 19:01
@shrx I think this was either QuickTime or Screeny, then resampled in Mathematica (of course!)... –  cormullion Jun 12 '13 at 20:43