How to Copy a URL and text in one step

Posted:
in Genius Bar edited January 2014
OK - for what it's worth, do you find yourself thrashing back and forth between a page that you want to post a link to, and the page at AI where you are writing your post? And all because the Mac clipboard can't hold both the URL of the link AND the selected text that you want to quote in your post?



Example: I want to select some text from an article on the site "www.maciscool.com" and then switch to AI, and paste in BOTH the URL AND the text I selected in one swoop.



Here is a trivial AppleScript to do it:



Code:




tell application "Safari"

set thePost to URL of front document

set theSelection to do JavaScript "window.getSelection()+'';" in front document

if theSelection is not "" then set thePost to (thePost & return & return & theSelection & return)

set the clipboard to "[_quote]" & return & thePost & return & "[_/quote]"

end tell









With this little scriptlet, all you have to do is select the text that you want to quote and run the script; then paste into your post. It even adds the quote tags for you.



1. Select the text between the horizontal lines above, and then choose Services ->Script Editor ->Make New AppleScript from the Safari menu.



2. Switch to Script Editor. Remove the underscores from in front of the both instances of the word "quote" in line 5 (had to do that to get it to post here) and save the script as an application in the /Library/Scripts/Applications/Safari folder in your home folder.



3. If you do not have the Script Menu showing in your menu bar (a little scroll icon in the row of icons on the upper right of the menu bar), then install it by double-clicking /Applications/AppleScript/AppleScript Utility and checking the box that says "Show Script Menu in Menu Bar".



4. The script that you installed should now appear in the Script Menu when Safari is frontmost.



5. To use, go to the web page that you want to refer to, select some text in the article, and choose the script from the script menu. Then go back to AI and hit command-V to paste. It will paste an opening quote tag, the URL of the page you did your selection on, some blank lines, the text you selected, and a closing quote tag.

Comments

  • Reply 1 of 13
    You kinda lost me after the first line... Maybe cause I am tired and my attention span is lacklustre this evening. Perhaps some Percolation would Perc me Up?!
  • Reply 2 of 13
    lundylundy Posts: 4,466member
    Quote:

    Originally posted by kmhtkmhtkmht

    You kinda lost me after the first line... Maybe cause I am tired and my attention span is lacklustre this evening. Perhaps some Percolation would Perc me Up?!



    The Mac clipboard can't hold two things at once. So if you want to copy a discontiguous selection, or two things from different text boxes on the screen, you can't do it.



    So if you want to post something like



    Quote:



    Hey guys, take a look at what this guy had to say about the new MacBook:



    http://www.somecrazyguy.com/macbook.html



    Quote:

    The new MacBook is pretty, but as usual with Apple, it's too expensive, has no upgradeable decent graphics card, the screen is too small, it's too heavy, it doesn't have a PCMCIA or floppy slot, blah blah blah blah blah....





    This paste has two things that need to be copied, and they aren't together - one is in the address bar and the other is part of the text on the page. Then ordinarily you'd have to copy the URL, go back to AI and paste the URL, then go back and copy the text of the article you want to quote, then go back to AI and paste that, then type in the "quote" and "/quote" tags....



    This lets you load the clipboard with everything in one shot, ready for pasting. Just select, run the script, switch to AI, and paste.



    It also means that you can go ONCE to the page you want to mention, and not have to keep it on another tab to go back to for the second copy.
  • Reply 3 of 13
    piotpiot Posts: 1,346member
    PopCopy 12 Bucks.
  • Reply 4 of 13
    I just use "CTRL" and Highlight the two bits of text that I want...
  • Reply 5 of 13
    lundylundy Posts: 4,466member
    Quote:

    Originally posted by kmhtkmhtkmht

    I just use "CTRL" and Highlight the two bits of text that I want...



    "CTRL" and a mouse click would invoke the contextual menu - like a right-click. So that makes no sense.



    That works only in Word, not in Safari. There is nothing that lets you select the contents of the address bar and some text in Safari - not control, not command, not option, not even shift.
  • Reply 6 of 13
    I am a switcher, forgive me.
  • Reply 7 of 13
    bergzbergz Posts: 1,045member
    Quicksilver's clipboard automatically keeps track of the last 10 "copies", and it's very simple to create a trigger that would, for instance, paste the last two copies at once wherever you place your cursor.



    --B
  • Reply 8 of 13
    lundylundy Posts: 4,466member
    I downloaded QuickSilver -



    - it hung downloading and installing the Clipboard plug-in

    - on a "Move To..", it didn't display any dialog for where to move to, nor tell me if it moved the file or not

    - it crashed and the OS killed it.



    So I junked it. Too much keyboarding for me anyway, I'm a mouser.



    Anyway, this script is a single-purpose shortcut, not a general purpose application enhancer. Copying and pasting a URL/text selection combination is so common that I thought it merited a script. Plus, it adds the URL without you having to explicitly select it, and it also adds the quote tags for vBulletin to format the pasted information as a quoted block.



    You can also use the same idea to automatically submit selected text to Wikipedia, rather than having to copy it, switch to Wikipedia, paste it and click Search...



    Code:




    tell application "Safari"

    set selectedText to do JavaScript "window.getSelection()+'';" in front document -- get the selected text

    end tell





    if selectedText is not "" then -- if there is a selection

    set selectedText to my formatForWiki(selectedText) -- format for Wikipedia

    set wikiURL to "http://en.wikipedia.org/wiki/"; & selectedText

    open location wikiURL -- launch Wikipedia in default browser

    end if



    -- Subroutine to replace blanks with underscores for Wikipedia

    to formatForWiki(theString)

    set AppleScript's text item delimiters to space

    set theWords to (words of theString)

    set AppleScript's text item delimiters to "_"

    set theString to theWords as text

    set AppleScript's text item delimiters to ""

    return theString

    end formatForWiki









    Very similar to the existing Contextual Menu Options in Safari for Go To Address, Search in Spotlight, Search in Google, and Look Up In Dictionary.
  • Reply 9 of 13
    bergzbergz Posts: 1,045member
    Quote:

    Originally posted by lundy

    I'm a mouser.



    See, that's a serious QS incompatibility. Sorry. Works great for some, doesn't sit well with others.



    --B
  • Reply 10 of 13
    MarvinMarvin Posts: 15,322moderator
    If you want something to make this even easier, you can get a contextual plugin called OnMyCommand. It lets you run custom scripts from anywhere and they show up in your contextual menu. So for example, you could be on Safari, select the text, right-click, select the script. I find it very handy for doing things all over the system.
  • Reply 11 of 13
    lundylundy Posts: 4,466member
    Good point. I remember using that a couple of years ago.
  • Reply 12 of 13
    mike555mike555 Posts: 10member
    you could get "JumpCut" it extends the clipboard , and is freeware and open source ... http://jumpcut.sourceforge.net/
  • Reply 13 of 13
    curiousuburbcuriousuburb Posts: 3,325member
    If the passage you want to quote has more than one link, how would the script pick which one? Could it get multiple URLs?
Sign In or Register to comment.