My guess is that not many will wanna help with this, but I figured: why not? Anyway, I’m really new to applescript, but I’ve created a script that takes a list of songs from TextEdit like:
10th+Avenue+Freezeout+Bruce+Springsteen
21st+Century+Schizoid+Man+King+Crimson
(just find/replace all the spaces with +'s)
then it takes those songs and through my script it finds all the songs and downloads them using youtube-mp3.com with Google Chrome. But it has bugs, some of which I will be able to figure out and others that I might not. I was wondering if anyone would be willing to search through this and maybe test it to see if they can quickly find a solution. Any help is appreciated. If not, maybe this could give someone new ideas or something, idk. Anyway, thanks for anything you have to offer. Here is my script.
tell application "Google Chrome"
activate
tell application "TextEdit" to set AllSongs to text of document "AllSongs"
set song_num to 1
make new window
repeat with main_counter from 1 to (round (number of paragraphs in AllSongs) / 15 rounding up)
set songs to ""
if number of paragraphs of AllSongs ≥ song_num + 14 then
repeat with repeater from 1 to 15
set songs to songs & paragraph (song_num - 1 + repeater) of AllSongs & "
"
end repeat
else
if number of paragraphs of AllSongs < song_num + 14 then
repeat with repeater from 1 to ((number of paragraphs of AllSongs) - song_num)
set songs to songs & paragraph (song_num - 1 + repeater) of AllSongs & "
"
end repeat
set songs to paragraphs song_num thru (count paragraphs of AllSongs) of AllSongs
end if
end if
tell application "TextEdit" to set text of document 1 to ""
make new window
tell front window
set theURL to songs
set theURL to every paragraph of theURL
tell application "TextEdit" to set text of document 1 to songs as text
--add links around names
repeat with x from 1 to number of items in theURL
set item x of theURL to "http://www.youtube.com/results?search_query=" & item x of theURL & "&aq=f"
end repeat
--create tabs with each going to a different youtube search page
repeat with k from 1 to number of items in theURL
set URL of active tab to item k of theURL
make new tab with properties {URL:k}
end repeat
--remove +'s from songs
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to "+"
set songs to text items of songs
set text item delimiters of AppleScript to " "
set songs to "" & songs
set text item delimiters of AppleScript to prevTIDs
--remove tabs (big spaces) from songs
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to " "
set songs to text items of songs
set text item delimiters of AppleScript to " "
set songs to "" & songs
set text item delimiters of AppleScript to prevTIDs
set songs to every paragraph of songs
set active tab index to 1
--wait until finished loading
repeat with z from 1 to number of items in songs
delay 1
repeat
if title of active tab contains "youtube" then
--copy page
tell active tab
select all
copy selection
end tell
--search for text that lets me know it's done
set the clipboard to «class ktxt» of ((the clipboard as text) as record)
set doc to the clipboard
if doc contains item z of songs then
exit repeat
end if
end if
end repeat
if doc contains "Translate results into my language" then
if doc contains "did you mean" then
tell application "System Events"
repeat with numtimes from 1 to 15
keystroke tab
end repeat
keystroke return
end tell
else
tell application "System Events"
repeat with numtimes from 1 to 14
keystroke tab
end repeat
keystroke return
end tell
end if
else
if doc contains "did you mean" then
tell application "System Events"
repeat with numtimes from 1 to 14
keystroke tab
end repeat
keystroke return
end tell
else
tell application "System Events"
repeat with numtimes from 1 to 13
keystroke tab
end repeat
keystroke return
end tell
end if
end if
set active tab index to (active tab index) + 1
end repeat
tell application "TextEdit" to set text of document 1 to item z of songs
--1st half of youtube to mp3
--wait until finished loading
set active tab index to 1
repeat
delay 1
if title of active tab contains "youtube" then
--copy page
tell active tab
select all
copy selection
end tell
--search for text that lets me know it's done
set the clipboard to «class ktxt» of ((the clipboard as text) as record)
set doc to the clipboard
if doc contains "comments" then
delay 2
exit repeat
end if
if doc contains "Search results for" then
if doc contains "Translate results into my language" then
if doc contains "did you mean" then
tell application "System Events"
repeat with numtimes from 1 to 15
keystroke tab
end repeat
keystroke return
end tell
else
tell application "System Events"
repeat with numtimes from 1 to 14
keystroke tab
end repeat
keystroke return
end tell
end if
else
if doc contains "did you mean" then
tell application "System Events"
repeat with numtimes from 1 to 14
keystroke tab
end repeat
keystroke return
end tell
else
tell application "System Events"
repeat with numtimes from 1 to 13
keystroke tab
end repeat
keystroke return
end tell
end if
end if
end if
end if
end repeat
repeat with counter from 1 to number of items in songs
set x to URL of active tab
tell application "System Events" to set the clipboard to ""
set URL of active tab to "http://www.youtube-mp3.org/"
--wait until finished loading
repeat
delay 1
if title of active tab contains "youtube" then
--copy page
tell active tab
select all
copy selection
end tell
--search for text that lets me know it's done
set the clipboard to «class ktxt» of ((the clipboard as text) as record)
set doc to the clipboard
if doc contains "What is YouTube mp3?" then
delay 2
exit repeat
end if
end if
end repeat
--enter url in text field and hit enter
tell application "System Events"
keystroke tab & tab & tab
keystroke x as text
keystroke return
keystroke tab
delay 1
end tell
set active tab index to (active tab index) + 1
tell application "System Events" to set the clipboard to ""
end repeat
--Second half of Youtube mp3
set active tab index to 1
--wait until finished loading
repeat with counter from 1 to number of items in songs
repeat
delay 0.5
--copy page
tell active tab
select all
copy selection
end tell
--search for text that lets me know it's done
set the clipboard to «class ktxt» of ((the clipboard as text) as record)
set doc to the clipboard
if doc contains "link this mp3" then
exit repeat
end if
if doc contains "Invalid URL" then
tell application "TextEdit" to set text of document "Errors" to text of document "Errors" & item counter of songs & "
"
exit repeat
end if
if doc contains "What is YouTube mp3?" then
if doc does not contain "Invalid URL" then
if doc does not contain "link this mp3" then
tell application "TextEdit" to set text of document "Errors" to text of document "Errors" & item counter of songs
exit repeat
end if
end if
end if
end repeat
tell application "System Events"
repeat with numtimes from 1 to 17
keystroke tab
delay 0.3
end repeat
keystroke return
end tell
set active tab index to (active tab index) + 1
end repeat
set song_num to song_num + 15
delay 5
end tell
tell window 2 to close
end repeat
end tell
Add a little bit more labels, Im having trouble finding out what this “AllSongs” document is…
EDIT: Ok, I get that “AllSongs” is the textedit doc, so i ran it and I got two google chrome windows (for the two songs I entered) but after that nothing. The code’s still running, but with tons of lag, try compaction maybe? Is this a bug you encountered?
Ok, heres some code to convert something with spaces to +'s (allows a more friendly appearance):
display dialog "Enter the song name:" default answer "song name" buttons {"OK"} default button 1
set theSong to text returned of result
set the_Song to ""
if number of words in theSong is 1 then
set the_Song to theSong
else
if number of words in theSong is 2 then
set the_Song to (word 1 of theSong) & "+" & (word 2 of theSong)
else
repeat until number of words in the_Song = 2
set the_Song to the_Song & (first word of theSong) & "+"
set theSong to text ((length of first word of theSong) + 2) thru end of theSong
end repeat
set the_Song to the_Song & (word 1 of theSong) & "+" & (word 2 of theSong)
end if
end if
set theYoutubeHTML to "http://www.youtube.com/results?search_query=" & the_Song & "&aq=f"
Model: Macbook
Browser: Safari 533.21.1
Operating System: Mac OS X (10.6)
Thanks for the input!
I have changed it a bunch since posting this, and most of the bugs are worked out. My next comment will be the new script.
No, what you encountered was not one of the bugs. Did it solve the problem when you added +'s? Sorry I wasn’t specific about that. Ya AllSongs is the document I leave open for Script Editor to read, and it contains about 1000 song names like this:
The+Dolphin’s+Cry+Live
The+Final+Countdown+Europe
The+Four+Horsemen+Metallica
The+Great+Gig+In+The+Sky+Pink+Floyd
The+Impression+That+I+Get+The+Mighty+Mighty+Bosstones
The+King+Will+Come+Wishbone+Ash
(Song+Name+Artist+Name). No punctuation.
The 2 windows were for 2 sets of songs to alternate in case the one set wasn’t done before the second started (If there’s more than 15 songs it does the process with fifteen (1 per tab), then switches windows and does it with the next fifteen.)
Thanks for the thing that adds the +'s, but I already have one, I just haven’t really used it because i didn’t need to. Mine simply uses a find/replace method for all the spaces.
My newest edition of the script has a dialog box like your suggestion, where you paste your list of songs. Currently, though, it’s not too user friendly unless you made it because you have to post the songs exactly like my list up there. After i add the + replacer it’ll be a little better but not much. Thanks so much. I appreciate anything you have to offer. Oh and one last thing: i’m not sure if i know how to make it much more compact than the version i’m about to post. If you’d like, would you offer suggestions on that? Thanks
Thanks for the
There. It’s pretty near finished. I made tons of changes. Just a few minor bugs left. Here it is:
tell application "Google Chrome"
activate
--dialog box to get song list
set AllSongs to ""
set EmptySpace to 0
repeat
set question to display dialog ("Paste list of songs below, or type them in") default answer "song name artist name" buttons {"Quit Script", "Add Another Song", "Start Downloading Songs"} default button 2
set answer to button returned of question
if EmptySpace is 1 then
set AllSongs to AllSongs & "
" & text returned of question
else
set AllSongs to AllSongs & text returned of question
set EmptySpace to 1
end if
if answer is equal to "Quit Script" then
return
end if
if answer is equal to "Start Downloading Songs" then
exit repeat
end if
end repeat
--other variables to set
set the clipboard to ""
set song_num to 1
set refresher to 0
set retry to 0
--new window to work in
make new window
tell front window
if (count paragraphs in AllSongs) ≥ 15 then
repeat with k from 1 to 14
make new tab
end repeat
else
repeat with k from 1 to number of paragraphs in AllSongs
make new tab
end repeat
end if
--repeat entire process by creating 1 window with 15 tabs each time.
repeat with main_counter from 1 to (round (number of paragraphs in AllSongs) / 15 rounding up)
set songs to ""
if number of paragraphs of AllSongs ≥ song_num + 14 then
repeat with repeater from 1 to 15
set songs to songs & paragraph (song_num - 1 + repeater) of AllSongs & "
"
end repeat
else
if number of paragraphs of AllSongs < song_num + 14 then
repeat with repeater from 1 to ((number of paragraphs of AllSongs) - song_num)
set songs to songs & paragraph (song_num - 1 + repeater) of AllSongs & "
"
end repeat
set songs to paragraphs song_num thru (count paragraphs of AllSongs) of AllSongs
end if
end if
set theURL to songs
--replace spaces with +'s in theURL
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to " "
set theURL to text items of theURL
set text item delimiters of AppleScript to "+"
set theURL to "" & theURL
set text item delimiters of AppleScript to prevTIDs
set theURL to paragraphs 1 thru 15 of theURL
--add links around names (and make sure no covers are downloaded)
repeat with x from 1 to number of items in theURL
set item x of theURL to "http://www.youtube.com/results?search_query=" & item x of theURL & "+-cover&aq=f"
end repeat
--Set each open tab of the window to a different youtube search page
repeat with k from 1 to number of items in theURL
set URL of tab index k to item k of theURL
end repeat
--remove +'s from songs
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to "+"
set songs to text items of songs
set text item delimiters of AppleScript to " "
set songs to "" & songs
set text item delimiters of AppleScript to prevTIDs
--remove tabs (big spaces) from songs
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to " "
set songs to text items of songs
set text item delimiters of AppleScript to " "
set songs to "" & songs
set text item delimiters of AppleScript to prevTIDs
set songs to paragraphs 1 thru 15 of songs
set active tab index to 1
set doc to ""
--On each search page, select the first link and go to it
repeat with z from 1 to number of items in songs
--wait until finished loading
repeat
if refresher is 60 then
reload active tab
set refresher to 0
end if
if title of active tab contains "chrome" then
set URL of active tab to item 1 of theURL
end if
if title of active tab contains "youtube" then
--copy page
tell active tab
select all
delay 0.2
copy selection
set refresher to refresher + 0.2
end tell
--search for text that lets me know it's done
set the clipboard to (the clipboard as text)
set doc to the clipboard
if doc contains "No video results for" then
reload active tab
end if
if doc contains item z of songs then
set the clipboard to ""
exit repeat
end if
end if
delay 1
set refresher to refresher + 1
end repeat
--Select the link and push enter based on how many times you have to hit tab for it to be selected
set TabStrokes to 13
if doc contains "Translate results into my language" then
set TabStrokes to TabStrokes + 1
end if
if doc contains "did you mean" then
set TabStrokes to TabStrokes + 1
end if
if paragraphs 10 thru 13 of doc contains "Translate" then
set TabStrokes to TabStrokes + 1
end if
tell application "System Events"
repeat with numtimes from 1 to TabStrokes
keystroke tab
end repeat
keystroke return
end tell
if z < number of items in songs then
set active tab index to (active tab index) + 1
end if
end repeat
--1st half of youtube to mp3 (Set x to URL and go to youtube-mp3.org)
set active tab index to 1
--wait until finished loading
repeat
if refresher is 60 then
reload active tab
set refresher to 0
end if
if title of active tab contains "youtube" then
--copy page
tell active tab
select all
delay 0.2
set refresher to refresher + 0.2
copy selection
end tell
--search for text that lets me know it's done
set the clipboard to (the clipboard as text)
set doc to the clipboard
if doc contains "Search results for" then
set TabStrokes to 13
if active tab index is 1 then
set TabStrokes to TabStrokes + 2
end if
if doc contains "Translate results into my language" then
set TabStrokes to TabStrokes + 1
end if
if doc contains "did you mean" then
set TabStrokes to TabStrokes + 1
end if
if paragraphs 10 thru 13 of doc contains "Translate" then
set TabStrokes to TabStrokes + 1
end if
tell application "System Events"
repeat with numtimes from 1 to TabStrokes
keystroke tab
end repeat
keystroke return
end tell
set doc to ""
end if
if doc contains "suggestions" then
delay 2
set doc to ""
set the clipboard to ""
exit repeat
end if
end if
delay 1
set refresher to refresher + 1
end repeat
--Set x to URL and go to youtube-mp3.org. Then select the conversion text field and type URL into it
repeat with counter from 1 to number of items in songs
set x to URL of active tab
set URL of active tab to "http://www.youtube-mp3.org/"
--wait until finished loading
repeat
if refresher is 60 then
reload active tab
set refresher to 0
end if
if title of active tab contains "youtube" then
--copy page
tell active tab
select all
delay 0.2
copy selection
set refresher to refresher + 0.2
end tell
--search for text that lets me know it's done
set the clipboard to (the clipboard as text)
set doc to the clipboard
if doc contains "What is YouTube mp3?" then
delay 2
set doc to ""
set the clipboard to ""
exit repeat
end if
end if
delay 1
set refresher to refresher + 1
end repeat
--enter url in text field and hit enter
tell application "System Events"
keystroke tab & tab & tab
keystroke x as text
keystroke return
keystroke tab
delay 1
end tell
if counter < number of items in songs then
set active tab index to (active tab index) + 1
end if
set doc to ""
set the clipboard to ""
end repeat
--Second half of Youtube mp3
set active tab index to 1
repeat with counter from 1 to number of items in songs
set attempts to 0
--wait until finished loading
repeat
if refresher is 60 then
reload active tab
set refresher to 0
end if
if title of active tab contains "YouTube" then
if title of active tab does not contain "YouTube to mp3 Converter" then
if title of active tab does not contain "YouTube - " then
tell active tab
go back
delay 3
set refresher to 0
end tell
end if
end if
else if title of active tab is not "" then
delete tab active tab index
end if
delay 0.5
set refresher to refresher + 0.5
--copy page
tell active tab
select all
delay 0.2
set refresher to refresher + 0.5
copy selection
end tell
--search for text that lets me know it's done
set the clipboard to (the clipboard as text)
set doc to the clipboard
if doc contains "link this mp3" then
set doc to ""
set the clipboard to ""
exit repeat
end if
if doc contains "Invalid URL" then
tell application "TextEdit"
activate
set text of document 1 to text of document 1 & item counter of songs & "
"
end tell
activate
set doc to ""
set the clipboard to ""
exit repeat
end if
if doc contains "suggestions" then
delay 2
set x to URL of active tab
set the clipboard to ""
set URL of active tab to "http://www.youtube-mp3.org/"
delay 3
set counter to counter - 1
set doc to ""
set refresher to 0
end if
if doc contains "Search results for" then
set TabStrokes to 18
if doc contains "Translate results into my language" then
set TabStrokes to TabStrokes + 1
end if
if doc contains "did you mean" then
set TabStrokes to TabStrokes + 1
end if
if paragraphs 10 thru 13 of doc contains "Translate" then
set TabStrokes to TabStrokes + 1
end if
tell application "System Events"
repeat with numtimes from 1 to TabStrokes
keystroke tab
end repeat
keystroke return
delay 3
end tell
set doc to ""
set counter to counter - 1
set refresher to 0
end if
if doc contains "as an Error caused by YouTube, we cannot deliver" then
if attempts is 0 then
tell active tab
go back
delay 3
go back
delay 3
end tell
set retry to 1
set attempts to attempts + 1
set doc to ""
set counter to counter - 1
set refresher to 0
else
set doc to ""
set the clipboard to ""
tell application "TextEdit" to set text of document 1 to text of document 1 & item counter of songs & "
"
exit repeat
end if
end if
if doc contains "What is YouTube mp3?" then
if doc does not contain "processing" then
if doc does not contain "as an Error caused by YouTube, we cannot deliver" then
if doc does not contain "Invalid URL" then
if doc does not contain "link this mp3" then
if retry is 1 then
delay 2
tell application "System Events"
keystroke tab & tab & tab
keystroke x as text
keystroke return
keystroke tab
delay 3
end tell
set retry to 0
set doc to ""
set counter to counter - 1
set refresher to 0
else
go back
delay 3
end if
end if
end if
end if
end if
end if
delay 1
set the clipboard to ""
set refresher to refresher + 1
end repeat
tell application "System Events"
repeat with numtimes from 1 to 17
keystroke tab
delay 0.1
end repeat
keystroke return
end tell
if counter < number of items in songs then
set active tab index to (active tab index) + 1
end if
end repeat
set song_num to song_num + 15
repeat with k from 1 to 10
display dialog "Push cancel to stop, or do nothing to continue
" & 11 - k buttons {"Cancel"} giving up after 1
end repeat
set the clipboard to ""
set active tab index to 1
end repeat
end tell
end tell
Ok, I’ll post my code i made to download one song you enter (in a dialog box)
Ok, I didnt use any mouse/keyboard scripting in mine, so I couldnt download it, it just opens up the site with the download button. Also, mines just a video downloader, not songs (it does songs, but its theme is videos :)). Ok, here it is so far (feel free to post some of your ideas):
-- youLaunch - A youtube video searcher
try
getSongTitle()
replacePlusAndQuotes(theSong)
findURL(newSong)
on error
display dialog " Thanks for using youLaunch!" buttons {"OK"} default button 1
end try
on getSongTitle()
display dialog "Enter the video name:" default answer "video name" buttons {"Quit", "OK"} default button 2
set theResult to result
if button returned of theResult is "Quit" then
ImExitingRightNowCuzThisIsntDefined
end if
global theSong
set theSong to text returned of theResult
end getSongTitle
on replacePlusAndQuotes(theSong)
global newSong
set newSong to ""
repeat until number of characters in theSong = 1
set thisLetter to character 1 of theSong
if thisLetter is " " then
set thisLetter to "+"
end if
if thisLetter is "'" then
set thisLetter to ""
end if
set newSong to newSong & thisLetter
set theSong to text 2 thru end of theSong
end repeat
if theSong is " " then
set theSong to "+"
end if
if thisLetter is "'" then
set thisLetter to ""
end if
set newSong to newSong & theSong
end replacePlusAndQuotes
on findURL(newSong)
global idCode
set theYoutubeHTML to "http://www.youtube.com/results?search_query=" & newSong
set theCode to do shell script ("curl " & theYoutubeHTML) without altering line endings
set idCode to text ((offset of "<h3><a href=\"" in theCode) + 13) thru end of theCode
set idCode to text 1 thru ((offset of "\"" in idCode) - 1) of idCode
end findURL
set justCode to text ((offset of "=" in idCode) + 1) thru end of idCode
set newUrl to "http" & ":" & "//www.youtube-mp3.org/get?video_id=" & justCode & "&h=1d01ee94224cb8d36e1c3d2bad113e16"
tell application "Safari"
open location newUrl
display dialog "Click 'Download' to download the file to your CPU" buttons {"Ok"} default button 1
end tell
As you can see, one way of organization I used was handler. Read up on them in the forums. Also, try not using as much keyboard scripting, I use (do shell script “curl URLHERE”) to find the links i need without opening up a page. The problem i had was the download button goes to a link, normally i could find this link in the code but its inside java script
i might just use one keyboard scripter there.
EDIT:
This is for Safari, just change “Safari” to “Google Chrome” for you, that’ll still work fine, just tested it
Wow those are some great tips lemuralex13! Thanks!!! But I’ve never used curl before, and yours keeps saying idcode is not defined. If you could explain what’s going on with the curl process that’d be amazing! Thanks.
Hmm, i dont know why idcode isnt working… I’ll take a look, worked on mine, idk. Try getting rid of the handlers, looks messier but that should fix it. Ok, well the curl command is like “see url”, little pun
Haha, well it gets the source code for the url following it. On Safari you can click “View” then “View Source”. (Google Chrome is “View” → “Developer” → “View Source”) What i did was I youtube searched something, then opened the site’s source, and found the first search result’s url in the code. I then copied some of the stuff before it, and used “offset” to find where it was in applescript. Do the same with stuff after it so you know where it ends. This is what i call a “Reference Marker”. I can now use this in my code so it can always find the first search result. That part of the code is:
set theYoutubeHTML to "http://www.youtube.com/results?search_query=" & newSong
set theCode to do shell script ("curl " & theYoutubeHTML) without altering line endings -- Gets source code
set idCode to text ((offset of "<h3><a href=\"" in theCode) + 13) thru end of theCode -- Goes to ref marker
set idCode to text 1 thru ((offset of "\"" in idCode) - 1) of idCode -- chops off code after 2nd ref marker
So it goes to the search results of whatever you entered, and gets the URL for the first result. Then it gets just the youtube idcode part of that url. (/watch?v=4D-4Z879hsA —> 4D-4Z879hsA) It then goes to the download page by taking http://www.youtube-mp3.org/get?video_id=" and appending the idcode then appending “&h=1d01ee94224cb8d36e1c3d2bad113e16”. The code is:
set justCode to text ((offset of "=" in idCode) + 1) thru end of idCode -- chops off all except the id
set newUrl to "http://www.youtube-mp3.org/get?video_id=" & justCode & "&h=1d01ee94224cb8d36e1c3d2bad113e16" -- Gets the youtube-mp3.org site's URL
This is what takes you to a videos download page on youtube-mp3.org. I always try and look for stuff like that in the urls (did the same with searching in youtube). Then it just opens the URL at the end of the script. I know this must all sound confusing, if you want me to explain any one part better, just say so, i’d be happy to do so 
Also, with this line here:
Set newUrl to “http://www.youtube-mp3.org/get?video_id=” & justCode & “&h=1d01ee94224cb8d36e1c3d2bad113e16”
The id at the end(“1d01…”) isnt the right id for any videos, so it just takes you to the page with the download button, doesnt auto download, if only I could get inside the java…
Frustrating. Lol
Thanks this really helps! I figured out why idcode wasn’t working. It’s cuz my findreplace method decided to quit working and i can’t figure out why. Do you see why? Here’s the top part of my script. Just to test why findreplace isn’t working
--set handlers to be repeated later
--handler 1
on refresh(refresher, mins)
my docCheck(refresher)
if refresher ≥ 20 and refresher < 30 then
if (the clipboard) is "" then
try
tell application "System Events" to keystroke "c" using {command down}
end try
end if
else if refresher ≥ 30 and refresher < 600 then
tell application "Google Chrome" to reload active tab of window 1
set refresher to 0
set mins to mins + 0.5
else if mins ≥ 10 then
tell application "TextEdit"
activate
make new document
set text of document 1 to "Error. We're sorry, something has prevented this script from moving on. Please close all unnecessary windows and continue on with all undownloaded songs."
end tell
return
end if
end refresh
--handler 2
on replaceText(find, replace, someText)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set someText to text items of someText
set text item delimiters of AppleScript to replace
set someText to "" & someText
set text item delimiters of AppleScript to prevTIDs
return someText
end replaceText
--handler 3
on copier(refresher, doc)
tell application "Google Chrome" to tell front window to tell active tab
select all
delay 0.2
copy selection
set refresher to refresher + 0.2
end tell
try
set my doc to the clipboard as text
on error
set my doc to ""
end try
end copier
--handler 4
on searchResults(doc, numtimes)
tell application "System Events"
if paragraphs 10 thru 13 of doc contains "Translate" then keystroke tab
if doc contains "Translate results into my language" then keystroke tab
if doc contains "did you mean" then keystroke tab
if doc contains "Watch Now: " then keystroke tab & tab
try
repeat numtimes times
keystroke tab
end repeat
keystroke return
end try
end tell
set doc to ""
set the clipboard to ""
end searchResults
--handler 5
on errorReport(repeater, input, doc, message)
tell application "TextEdit"
activate
set text of document 1 to text of document 1 & message & item repeater of input & return
delay 1
end tell
tell application "Google Chrome" to activate
set doc to ""
set the clipboard to ""
end errorReport
--handler 6
on docCheck(doc)
tell application "TextEdit"
activate
set text of document 1 to doc as text
delay 0.5
end tell
tell application "Google Chrome" to activate
end docCheck
--handler 7
--handler 8
--dialog box to get song list
set AllSongs to ""
set EmptySpace to 0
repeat
set question to display dialog ("Paste list of songs below, or type them in") default answer "song name artist name" buttons {"Quit Script", "Add Another Song", "Start Downloading Songs"} default button 2
set answer to button returned of question
if EmptySpace is 1 then
set AllSongs to AllSongs & return & text returned of question
else
set AllSongs to AllSongs & text returned of question
set EmptySpace to 1
end if
if answer is equal to "Start Downloading Songs" then
exit repeat
else if answer is equal to "Quit Script" then
return
end if
end repeat
--other variables to set
set the clipboard to ""
set song_num to 1
set refresher to 0
set songs to ""
if number of paragraphs of AllSongs ≥ song_num + 14 then
set text item delimiters of AppleScript to return
set songs to paragraphs song_num thru (song_num + 14) of AllSongs as text
set text item delimiters of AppleScript to ""
else
set text item delimiters of AppleScript to return
set songs to paragraphs song_num thru (count paragraphs of AllSongs) of AllSongs as text
set text item delimiters of AppleScript to ""
end if
set theURL to songs
--replace spaces with +'s in theURL
my replaceText(" ", "+", theURL)
return theURL
Haha, very simple thing actually, you have:
my replaceText(" ", "+", theURL)
return theURL
when you should have:
set theURL to my replaceText(" ", "+", theURL)
return theURL
That fixed it on mine, try it
Seems like a stupid mistake I’d make… Lol
Yup, feel free to post your full code as soon as you finish!
Ok, i got it working pretty well, but after like 50 songs or so, it slows down and eventually stops responding. It might just stop on a certain song, but I don’t like to check, because it freezes up google chrome and doesn’t work again until i restart my computer. Does anyone have any idea how to fix this? Or is it simply unfixable? It’s been doing the slow-down thing since the beginning.
This is getting to be quite long of a page to scroll through
Ok, I would recommend adding a “delay .5” at the end of your loop so it waits half of a second in between each download, should bog it down less. But a better solution would be doing each page individually rather than opening 50 pages at once. You can open 1, do stuff with it, close it, and repeat x50. Try coding it and posting the new code here. The second’s a LOT more work, but would definitely work a lot better (1st removed lag in AS, 2nd removes lag in google chrome, which affects the CPU more).
Ok, so It’s been quite awhile, but I think I finally got this thing working. I changed it immensely, and now it doesn’t even use Chrome.
However, it does use MediaHuman Youtube to MP3, so if you wanna test this out, you’ll need that. It’s free.
Here’s an example of a list I used to paste into the dialog box that asks you for the song list:
Wooden+Ships+Crosby,+Stills+&+Nash
Woodstock+Crosby+Stills+Nash+&+Young
Working+Class+Hero+John+Lennon
Worn+Down+Piano+Mark+&+Clark+Band
Yellow+Coldplay
Yesterday+The+Beatles
You’ve+Got+A+Friend+James+Taylor
You+Ain’t+Seen+Nothing+Yet+Bachman+Turner+Overdrive
You+Are+the+Sunshine+of+My+Life+Stevie+Wonder
You+Can’t+Always+Get+What+You+Want+The+Rolling+Stones
You+Keep+Me+Hangin’+On+Vanilla+Fudge
You+Oughta+Know+Alanis+Morissette
It should work without the +'s, but I haven’t tried that yet.
Here’s the whole script:
--handler 1
on replaceText(find, replace, someText)
set prevTIDs to text item delimiters of AppleScript
set text item delimiters of AppleScript to find
set someText to text items of someText
set text item delimiters of AppleScript to replace
set someText to "" & someText
set text item delimiters of AppleScript to prevTIDs
return someText
end replaceText
--dialog box to get song list
set AllSongs to ""
set EmptySpace to 0
repeat
set question to display dialog ("Paste list of songs below, or type them in") default answer "song name artist name" buttons {"Quit Script", "Add Another Song", "Start Downloading Songs"} default button 2
set answer to button returned of question
if EmptySpace is 1 then
set AllSongs to AllSongs & return & text returned of question
else
set AllSongs to AllSongs & text returned of question
set EmptySpace to 1
end if
if answer is equal to "Start Downloading Songs" then
exit repeat
else if answer is equal to "Quit Script" then
return
end if
end repeat
--other variables to set
set the clipboard to ""
--repeat entire process by creating 1 window with 15 tabs each time.
repeat with main_counter from 1 to (number of paragraphs in AllSongs)
activate
set song to paragraph main_counter of AllSongs
set paras to ((number of paragraphs in AllSongs) - main_counter + 1)
tell application "TextEdit"
activate
set text of document 1 to ((paras as text) & " songs left...")
end tell
--replace spaces with +'s in song
set song to my replaceText(" ", "+", song)
--remove apostrophes in song
set song to my replaceText("'", "", song)
--remove parentheses in song
set song to my replaceText("(", "", song)
set song to my replaceText(")", "", song)
--remove hyphons in song
set song to my replaceText("-", "", song)
--remove double-pluses in song
set song to my replaceText("++", "+", song)
--remove tab's in song
set song to my replaceText(" ", "+", song)
--replace "&" with "and"
set song to my replaceText("&", "and", song)
set song to every paragraph of song
--add links around names (and make sure no covers are downloaded)
set song to "http://www.youtube.com/results?search_query=" & song
set doc to ""
set newUrl to ""
try
set theCode to do shell script ("curl " & (song)) without altering line endings -- Gets source code
set idCode to text (offset of "watch?v=" in theCode) thru end of theCode -- Goes to ref marker
set idCode to text 1 thru ((offset of "\"" in idCode) - 1) of idCode -- chops off code after 2nd ref marker
set idCode to "http://www.youtube.com/" & idCode
set the clipboard to idCode
end try
end repeat
tell application "YouTube to MP3" to activate
tell application "TextEdit"
activate
set text of document 1 to ("All done! Press the download all button (the down arrow in the top right corner) in the app YouTube to MP3")
end tell
Oh and one more thing: Make sure the YouTube to MP3 app is open before you start the script and set it to “Auto add from clipboard” (bottom left options button)
Feel completely free to ask any more questions. I want to make this as functional as possible.