I am actually trying to create a macro that downloads youtube videos using youtube-dl by just selecting the youtube url and triggering the macro.On some occasions, the url can not be selected but can be copied using the right click menu and "copy link".
I will like the macro to first check if text is selected and if the selected text is a youtube url. If both conditions are true, then the selected text is copied to the clipboard. However, if either of these conditions are false, then the last entry in the clipboard is checked to ensure it is a valid youtube url.
The youtube url is then appended to the command "youtube-dl -f 18" in the terminal and the return key is typed.
Below is a screenshot of the macro using only selected text.
I don’t have KM in front of me, but here’s what you’re basically looking for:
Save the clipboard
Copy selected text (CMD + C)
If/Then: See if the clipboard text contains youtube.com or not. Not a 100% perfect test but probably good enough, Alternately you could use regex for this.
True:
Activate terminal
wait a little bit
Insert text (end of macro, everything else happens in the False section)
Although strangely you cannot create a new Tab via AppleScript.
You can create a new window using:
tell application "Terminal"
do script ""
end tell
So you can keep it all in AppleScript if you want, or you can mix and match it with Keyboard Maestro.
In any case this is an easy one.
-Chris
--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/03/10 11:37
# dMod: 2015/05/15 16:19
# Appl: Terminal
# Task: Download a video in the Terminal using `youtube-dl`.
# Libs: None
# Osax: None
# Tags: @Applescript, @Terminal, @Run, @Script, @Active, @Tab, @Download, @Video
--------------------------------------------------------
set videoURL to "http://www.youtube.com/watch?v=PuPAM0qhl4g"
set downloadDir to POSIX path of (path to downloads folder)
tell application "Terminal"
if not running then run
activate
tell front window
if not busy then
do script "cd " & quoted form of downloadDir in selected tab
set shCMD to "youtube-dl -f 18 " & quoted form of videoURL
do script shCMD in selected tab
end if
end tell
end tell
--------------------------------------------------------
Aha! I missed that when I was perusing the man page.
The following script presents a pick-list of available formats.
You pick one.
It gets downloaded.
* The commented-out section shows how to do the download directly from AppleScript. In general I think it's better to offload this job to the Terminal, because the other method will tie up your AppleScript-Runner-Utility until the download is finished - unless it is capable of running asynchronously. (KM can do this, but it will still be slowed down somewhat.)
-Chris
Edit 2015/05/16 16:39:
Fixed issue where choose-from dialog would not show from KM Execute AppleScript Action.
--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2015/03/10 11:37
# dMod: 2015/05/16 16:41
# Appl: Terminal
# Task: Download a video in the Terminal using `youtube-dl`.
# Libs: None
# Osax: None
# Tags: @Applescript, @Terminal, @Run, @Script, @Active, @Tab, @Download, @Video
--------------------------------------------------------
set videoURL to "http://www.youtube.com/watch?v=PuPAM0qhl4g"
set downloadDir to POSIX path of (path to downloads folder)
set shCMD to "
export PATH=/opt/local/bin/:$PATH;
youtube-dl -F " & videoURL & ";"
set AppleScript's text item delimiters to "format code extension resolution note" & return
set vidFormatList to text item 2 of (do shell script shCMD)
set vidFormatList to paragraphs of vidFormatList
tell application "Keyboard Maestro Engine"
activate
set myVidFormatChoice to choose from list vidFormatList ¬
with title "YouTube Video Format List" with prompt ¬
"Pick Your Poison:" default items {item 1 of vidFormatList}
end tell
if myVidFormatChoice ≠ false then
set myVidFormatChoice to item 1 of myVidFormatChoice
set AppleScript's text item delimiters to " "
set myVidFormatChoice to text item 1 of myVidFormatChoice
# Directly download from AppleScript.
# Will tie up the AppleScript-Runner for the duration of the download unless run asynchronously.
# set shCMD to "
# export PATH=/opt/local/bin/:$PATH;
# cd " & quoted form of downloadDir & ";
# youtube-dl -f " & myVidFormatChoice & " " & videoURL & ";"
# do shell script shCMD
tell application "Terminal"
if not running then run
activate
tell front window
if not busy then
do script "cd " & quoted form of downloadDir in selected tab
set shCMD to "youtube-dl -f " & myVidFormatChoice & " " & quoted form of videoURL
do script shCMD in selected tab
end if
end tell
end tell
end if
--------------------------------------------------------
I was able to accomplish it using KBM without Applescript but I will prefer if it is refined using Applescript.
<img src="/uploads/default/1085/ec7e4af77b31b135.png" width="500 height=“230”>