applescript - Come convertire il testo del link di YouTube per incorporare il testo html?

0

Voglio convertire 'testo del link di youtube (dagli appunti)' a "titolo youtube e testo html incorporato" e copia negli appunti

Con Applescript

per esempio,

  1. Copia

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

negli appunti

  1. esegui un applescript
  2. quindi il testo (Title e 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>
    

copiato negli appunti

    
posta rhrudxo 13.09.2017 - 08:05
fonte

2 risposte

1

Questo primo script AppleScript viene eseguito in base ai passaggi descritti nella tua domanda.

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

Supponendo che non ci sia errore , Appunti ora contiene le informazioni del link emesso.

Se per caso lo fai in Safari e sei nella pagina YouTube e desideri elaborare, senza dover prima copia target URL negli appunti e diventa più veloce usando curl , come nel primo script a ottieni titolo ( theVideoTitle ), quindi il seguente AppleScript script è un altro modo per andare.

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

Supponendo che non ci sia errore , gli Appunti ora contengono le informazioni sul link incorporato.

Se utilizzi Google Chrome , modifica le seguenti righe nello secondo AppleScript script come segue:

Modifica:

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

A:

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

Modifica anche Safari's nei comandi display dialog a: Google Chrome's

Nota: nel primo AppleScript script , ottenendo il valore della variabile theVideoID dal valore della variabile theURL degli appunti viene eseguito utilizzando un do shell script comando e sed , tuttavia può essere fatto usando il getVideoID handler usato nello secondo AppleScript script , come nel seguenti:

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

Supponendo che non ci sia errore , Appunti ora contiene le informazioni del link emesso.

    
risposta data 13.09.2017 - 16:57
fonte
0

Prova questo. Non include il bit di partenza con il titolo (non so come afferrarlo meglio), ma oltre a questo, dovrebbe funzionare alla grande.

set video_id to trimText(the clipboard, "https://www.youtube.com/watch?v=", "beginning")
set the clipboard to ("<p><iframe width=\"640\" height=\"360\" src=\"https://www.youtube.com/embed/" & (video_id) & "?rel=0&amp;showinfo=0\" frameborder=\"0\" allowfullscreen></iframe>")
display dialog (the clipboard)

on trimText(theText, theCharactersToTrim, theTrimDirection)
    set theTrimLength to length of theCharactersToTrim
    if theTrimDirection is in {"beginning", "both"} then
        repeat while theText begins with theCharactersToTrim
            try
                set theText to characters (theTrimLength + 1) thru -1 of theText as string
            on error
                -- text contains nothing but trim characters
                return ""
            end try
        end repeat
    end if
    if theTrimDirection is in {"end", "both"} then
        repeat while theText ends with theCharactersToTrim
            try
                set theText to characters 1 thru -(theTrimLength + 1) of theText as string
            on error
                -- text contains nothing but trim characters
                return ""
            end try
        end repeat
    end if
    return theText
end trimText

Se non vuoi che compaia ogni volta con una finestra di dialogo, elimina la terza riga.

    
risposta data 13.09.2017 - 08:57
fonte

Leggi altre domande sui tag