Applescript to copy selected text plus page URL in Safari

I do a lot of online research. I would like to be able to select text then run an applescript to add the selected text plus the page URL to the clipboard. I have found a couple scripts that come close to what I want to do, but they don't work. They get the URL and usually some other information, but instead of the slected text, I get "missing value". Does anyone know how I can make this work?


The scripts I found that do kind of what I want, but don't work are:


http://allancraig.net/index.php?option=com_content&view=article&id=113:saving-se lected-text-and-url-from-safari-to-a-file&catid=40:applescript&Itemid=91


http://hints.macworld.com/article.php?story=20080207080505152


This script works, but it copies URL and page title rather than URL and selected text:


https://gist.github.com/70565


I would be so grateful for some help on this.

Posted on Dec 1, 2012 11:48 AM

Reply
2 replies

Dec 1, 2012 12:25 PM in response to Jeffrey Gill

After looking around a bit more –


first here: https://discussions.apple.com/thread/2043319


then here: https://discussions.apple.com/thread/3197130?start=0&tstart=0


– I was able to cobble together this script:


tell application "Safari"


activate

set theURL to URL of front document

set selectedText to (do JavaScript "(''+getSelection())" in document 1)

set theDate to do shell script "date +'%d-%m-%Y'"


set the clipboard totheURL & return & selectedText & return & theDate as string

end tell


which does the trick very nicely. (I added the date to make it easier to do my references in college assignments.) I used Automator to save it as a Service and assigned a a keyboard shortcut (option + c). I believe this will speed up my workflow tremendously.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript to copy selected text plus page URL in Safari

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.