0

Complete newbie here so please let me know if I need to clarify or otherwise improve my question. I have searched multiple times using different keywords and have not been able to find a solution to my issue, or make the ones I hoped were a solution work for me.

I want to create a AppleScript script that when triggered will allow me to paste a web text clip appended with source attribution and a timestamp, without loosing any embedded links within the selected text.

Here is a screen shot of what I want to achieve:

Screenshot

Not knowing much of anything programming wise, I was able to cobble together the following AppleScript script after a few days of web searching.

-- clear the clipboard
tell application "Finder"
    set the clipboard to " "
    delay 0.1
end tell

-- copy selected text
tell application "Safari"
    activate
    tell application "System Events"
        tell process "Safari"
            keystroke "c" using {command down}
            delay 0.1
        end tell
    end tell
end tell

-- open and paste web clip into specified TextEdit file 
tell application "TextEdit"
    activate
    open "Macintosh HD:Users:Web:Documents:Web Text Clips:Web_Text_Clips.rtf"
    delay 0.2
    tell application "System Events"
        tell process "TextEdit"
            keystroke "v" using {command down}
            delay 0.1
        end tell
    end tell
end tell

-- get, format and copy source info and timestamp 
tell application "Safari"
    activate
    set theLongDate to current date
    set theWindowName to the name of the front window
    set theURL to the URL of the front document
    set writeString to "- - - - - " & return & "From: " & theURL & return & "Page Title: " & theWindowName & return & "Date: " & theLongDate
    set the clipboard to writeString
end tell

-- paste source info and timestamp into predefined position of the specified TextEdit file 
tell application "TextEdit"
    activate
    tell application "System Events"
        tell process "TextEdit"
            keystroke (ASCII character 31) using command down
            keystroke return
            keystroke return
            keystroke "v" using {command down}
            delay 0.1
        end tell
    end tell
end tell

-- copy content of specified TextEdit file 
tell application "TextEdit"
    activate
    tell application "System Events"
        tell process "TextEdit"
            keystroke "a" using {command down}
            keystroke "c" using {command down}
            delay 0.1
        end tell
    end tell
end tell

-- delete content of specified TextEdit file 
tell application "TextEdit"
    activate
    tell application "System Events"
        tell process "TextEdit"
            keystroke "a" using {command down}
            keystroke "x" using {command down}
            delay 0.1
        end tell
    end tell
end tell

-- save specified TextEdit file and quit TextEdit
tell application "TextEdit"
    save "Macintosh HD:Users:Web:Documents:Web Text Clips:Web_Text_Clips.rtf"
    quit
end tell

I was forced into this workaround because when I used the set command the embedded links got expunged from the selected web text.

While this script does work, it is quite cumbersome and slow. I have tried all sorts of different things, including some shell script commands, but so far nothing else has worked.

Can anyone help me in creating a more elegant and faster script that still maintains the embedded links in the selected web text?

I am running macOS Sierra 10.12.6.

  • @user3439894 I appreciate the masterful edit of my spelling and grammar mistakes. Could you also extended your kindness into the more technical aspects of my questions? i.e. how to paste simultaneously a web text clip, its source information and a timestamp? – Web Smith Sep 1 '17 at 15:48
-1

I'm a bit confused by your script, it seems to copy and paste and delete and save, so if I misunderstood your intended use, I apologize. It seems to me like you want a file saved in that Web Clips folder that just contains what you had selected in Safari with the link, title, and date. I would write this as an HTM file rather than a text file, and here's the code I used with inline comments.

set myFolder to ((path to users folder) as text) & "Web:Documents:Web Text Clips:"
set myFile to myFolder & "Web_Text_Clips.htm"

tell application "Safari"
set theLongDate to current date
set theWindowName to the name of the front window
set selectedText to (do JavaScript "(''+getSelection())" in document 1) --use javascript to get what part of the page is selected
set theURL to the URL of the front document
end tell

set myFolder to POSIX path of (myFolder as alias) --convert to posix path for use in the command line
set myFile to quoted form of (myFolder & myFile) --append the file name to the end of the file path
do shell script "touch " & myFile --create the htm file

--Add content to the HTM document
newHTM(myFile) --Add the HTM doctype and opening tags.
addElement("p", "--------", myFile) --"p" is the htm tag for a paragraph, and we want this to be it's own paragraph. 
addElement("p", "From: <a href=" & theURL & ">" & theURL & "</a>", myFile) --a bit more complex, adding a paragraph with a link (<a> is the link tag)
addElement("p", "Page Title: " & theWindowName, myFile) --Adding a paragraph for the Page Title
addElement("p", "Date: " & theLongDate, myFile) --Adding a paragraph for the Date line.
closeHTM(myFile) --Add the closing HTML tags.

------------HANDLERS------------
on newHTM(filePath)
    do shell script "printf '<!doctype html>\n<html lang=\"en\">\n<head>\n\t<title>Web Clips</title>\n\n</head>\n\n<body>\n\t' > " & filePath
end newHTM

on closeHTM(filePath)
    do shell script "printf '\n\t</body>\n</html>' >> " & filePath
end closeHTM

on addElement(elem, htm, filePath)
    set htm to "\t\t<" & elem & ">" & htm & "</" & elem & ">\n"
    do shell script "printf '" & htm & "' >> " & filePath
end addElement

This script preserves the link by using HTML to "embed" the link. It will open in your default browser. It appeared to me, from your script, that this file only contains the latest web clipping, so that's how I wrote it. If, however, you want this file to contain a list of web clippings, the script would need to be edited. Either way, I hope this gives you a good start. For readability, I put the adding of html code in handlers below the body of the code.

  • Would whoever downvoted my answer please explain why? The script does exactly what the OP requested. – Hurston Sep 5 '17 at 14:33
  • I thank you for your assistance and apologise for my confusing question and unclear goal. My desired end result is to have my selected web text clip (with any possible embedded links) , its source information and a timestamp siting on the clipboard ready to paste as a single entry into my preferred note app. I originally asked my question on stack overflow before thinking this might be a better place for such an Apple centric question. User jackjr300 was kind enough to provide a great script that resolves my issue. You can check it out here: tinyurl.com/y7mp43zw – Web Smith Sep 7 '17 at 10:43
  • Ah, thanks for the clarification. I didn't realize you wanted it on your clipboard. I understand now why what I wrote isn't what you were looking for. – Hurston Sep 7 '17 at 22:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .