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&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&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&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.