Este primero AppleScript script se realiza según 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 se haya producido error , el Portapapeles ahora contiene la información del enlace emebbed.
Si por casualidad estás haciendo esto en Safari y estás en la YouTube página y quieres procesarlo, sin tener que hacerlo primero. copie el destino URL en el Portapapeles y sea más rápido que con curl
, como en el primer script para obtenga el título ( theVideoTitle
), luego el siguiente AppleScript script es otra manera de proceder.
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 se haya producido error , el Portapapeles ahora contiene la información del enlace de inserción.
Si está utilizando Google Chrome , cambie las siguientes líneas en el segundo AppleScript script como sigue:
Cambiar:
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;"
También cambie Safari's
en los comandos display dialog
a: Google Chrome's
Nota: en el primer AppleScript script , obtenga el valor de la variable theVideoID
del El valor de la variable theURL
en el Portapapeles se realiza usando un comando do shell script
y sed
, sin embargo, se puede hacer utilizando el controlador getVideoID
utilizado en el segundo AppleScript script , como en 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 se haya producido error , el Portapapeles ahora contiene la información del enlace emebbed.