Applescript – How to convert youtube link text to embed html text

applescriptcopy/pasteyoutube

i want to convert 'youtube link text (from clipboard)'
to
'youtube title and embed html text'
and
copy to clipboard

With Applescript

for example,

  1. I copy

    https://www.youtube.com/watch?v=QBGaO89cBMI
    

into clipboard

  1. run a applescript
  2. then the text (Title and EmbedHTML)

    Radiohead - Lift<p><iframe width="640" height="360" src="https://www.youtube.com/embed/QBGaO89cBMI?rel=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe>
    

copied into clipboard

Best Answer

This first AppleScript script performs based on the steps outlined in your question.

tell current application
    set theURL to the clipboard
    if theURL contains "youtube" then
        try
            set theVideoID to do shell script "sed -e 's#.*=##'<<<" & the quoted form of theURL
            set theVideoTitle to do shell script "curl -s " & theURL & " | grep 'eow-title' | sed -e 's#.*title=\"##' -e 's#\">.*##'"
            set theEmbedLinkSeg1 to "<p><iframe width=\"640\" height=\"360\" src=\"https://www.youtube.com/embed/"
            set theEmbedLinkSeg2 to "el=0&amp;showinfo=0\" frameborder=\"0\" allowfullscreen></iframe>"
            set the clipboard to theVideoTitle & theEmbedLinkSeg1 & theVideoID & theEmbedLinkSeg2
        on error
            display dialog "An error occured during processing. Check the URL and or Script Code." buttons {"OK"} ¬
                default button 1 with title "Processing Error"
            return
        end try
    else
        display dialog "The URL on the Clipboard did not contain a YouTube URL in the expected format." buttons {"OK"} ¬
            default button 1 with title "Processing Error"
        return
    end if
end tell

Assuming no error occurred, the Clipboard now contains the emebbed link information.


If by chance you're doing this in Safari, and are at the YouTube page and want to process, without having to first copy the target URL to the Clipboard and be faster then using curl, as in the first script to get the title (theVideoTitle), then the following AppleScript script is another way to go.

tell application "Safari"
    tell document 1
        set theURL to (get URL)
        set theVideoTitle to do JavaScript "document.getElementById('eow-title').innerText;"
    end tell
end tell

tell current application
    if theURL contains "youtube" then
        try
            set embedLinkSeg1 to "<p><iframe width=\"640\" height=\"360\" src=\"https://www.youtube.com/embed/"
            set embedLinkSeg2 to "el=0&amp;showinfo=0\" frameborder=\"0\" allowfullscreen></iframe>"
            set theVideoID to my getVideoID(theURL)
            set the clipboard to theVideoTitle & embedLinkSeg1 & theVideoID & embedLinkSeg2
        on error
            display dialog "Please verify Safari's current tab is at YouTube with the expected URL format." buttons {"OK"} ¬
                default button 1 with title "Processing Error"
            return
        end try
    else
        display dialog "Safari's current tab is not at YouTube." & linefeed & linefeed & ¬
            "Please select the correct tab and try again." buttons {"OK"} default button 1 with title "Processing Error"
        return
    end if
end tell

on getVideoID(theTextString)
    set TID to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {"="}
    set theTextString to text item 2 of theTextString
    set AppleScript's text item delimiters to TID
    return theTextString
end getVideoID

Assuming no error occurred, the Clipboard now contains the embed link information.


If you're using Google Chrome, then change the following lines in the second AppleScript script as follows:

Change:

tell application "Safari"
    tell document 1
    set theVideoTitle to do JavaScript "document.getElementById('eow-title').innerText;"

To:

tell application "Google Chrome"
    tell active tab of window 1
    set theVideoTitle to execute javascript "document.getElementById('eow-title').innerText;"

Also change Safari's in the display dialog commands to: Google Chrome's


Note: In the first AppleScript script, getting the value of the variable theVideoID from the value of the variable theURL on the Clipboard is done using a do shell script command and sed, however it can be done using the getVideoID handler used in the second AppleScript script, as in the following:

tell current application
    set theURL to the clipboard
    if theURL contains "youtube" then
        try
            set theVideoID to my getVideoID(theURL)
            set theVideoTitle to do shell script "curl -s " & theURL & " | grep 'eow-title' | sed -e 's#.*title=\"##' -e 's#\">.*##'"
            set theEmbedLinkSeg1 to "<p><iframe width=\"640\" height=\"360\" src=\"https://www.youtube.com/embed/"
            set theEmbedLinkSeg2 to "el=0&amp;showinfo=0\" frameborder=\"0\" allowfullscreen></iframe>"
            set the clipboard to theVideoTitle & theEmbedLinkSeg1 & theVideoID & theEmbedLinkSeg2
        on error
            display dialog "An error occured during processing. Check the URL and or Script Code." buttons {"OK"} ¬
                default button 1 with title "Processing Error"
            return
        end try
    else
        display dialog "The URL on the Clipboard did not contain a YouTube URL in the expected format." buttons {"OK"} ¬
            default button 1 with title "Processing Error"
        return
    end if
end tell

on getVideoID(theTextString)
    set TID to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {"="}
    set theTextString to text item 2 of theTextString
    set AppleScript's text item delimiters to TID
    return theTextString
end getVideoID

Assuming no error occurred, the Clipboard now contains the emebbed link information.