Este primero AppleScript script realiza basándose en los pasos descritos en su pregunta.
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&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
Suponiendo que no error ocurrió, el Portapapeles contiene ahora la información del enlace emebbed.
Si por casualidad estás haciendo esto en Safari y están en el YouTube página y desea procesar, sin tener que copiar primero el objetivo URL a la Portapapeles y será más rápido que utilizar curl
como en el primer script para obtener el título ( theVideoTitle
), entonces AppleScript script es otra forma de hacerlo.
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&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
Suponiendo que no error ocurrió, el Portapapeles contiene ahora la información del enlace incrustado.
Si utiliza Google Chrome cambie las siguientes líneas en el archivo segundo AppleScript script como sigue:
Cambia:
tell application "Safari"
tell document 1
set theVideoTitle to do JavaScript "document.getElementById('eow-title').innerText;"
Para:
tell application "Google Chrome"
tell active tab of window 1
set theVideoTitle to execute javascript "document.getElementById('eow-title').innerText;"
Cambia también Safari's
en el display dialog
comandos a: Google Chrome's
Nota: En la primera AppleScript script , obteniendo el valor de la variable theVideoID
del valor de la variable theURL
en el Portapapeles se realiza mediante un do shell script
comando y sed
sin embargo, puede hacerse utilizando la función getVideoID
manipulador utilizado en el segundo AppleScript script como en el siguiente:
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&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
Suponiendo que no error ocurrió, el Portapapeles contiene ahora la información del enlace emebbed.
0 votos
¿Le ha servido de algo mi respuesta?