Download multiple files and rename

I have a folder with 20+ .webloc files, all pointing to Google calendar .ics downloads. (The URLs all look like "https://www.google.com/calendar/.../basic.ics".) I want to be able to automate downloading each of these, but the problem is that, no matter what the calendar, the file is always named "basic.ics". What I'd like to do is 1) iterate through each .webloc in a specific folder, 2) download each one to another folder and 3) name the downloaded file according to the associated .webloc (ie, reed.webloc would download to reed.ics in a different folder).


My AppleScript skills are pretty lousy, and Automator does not seem to be able to manage this sort of renaming of the download. Anybody got any advice? Is there an existing script out there somewhere that I can modify for my needs?

17" MacBook Pro, Mac OS X (10.6.7), iPhone 4

Posted on May 19, 2011 7:15 AM

Reply
2 replies

May 19, 2011 9:17 AM in response to thomas_r.

I think this will do it:


set basePath to "path:to:folder:where:ics folders live"

set weblocFolderPath to "path:to:folder:with:webloc files"


tell application "Finder"


-- extract pertinent info from webloc files

set {locs, filenames} to {location, name} of every internet location file of folder weblocFolderPath

end tell


repeat with i from 1 to count of locs


--remove .webloc extension from name

set newName to text 1 thru -8 of item i of filenames



-- make new folder with webloc file name

tell application "Finder"

if not (exists folder newName of folder basePath) then

makenewfolderatfolderbasePathwith properties {name:newName}

end if

set theFolderPath to foldernewName of folderbasePath as text

end tell



-- download ics file into the folder (replacing if needed) with webloc file name

tell application "URL Access Scripting"

set newFile to download (item i of locs) to (theFolderPath & newName & ".ics") replacing yes

end tell

end repeat

  • basePath is the folder where you want to make the folders to store the downloaded ics files
  • weblocFolderPath is the folder where your 20 weblocs are stored
  • both paths should be listed as text strings, colon delimited, including the disk name (e.g. "hard disk name:folder name:folder name:final folder name")

May 19, 2011 9:50 AM in response to twtwtw

I actually ended up managing to figure it out for myself, by piecing together bits of scripts from here and there on the web. Part of the problem was that URL Access Scripting is apparently unable to handle https links... the file was only partially downloaded and then AppleScript threw an error. That really threw me for a loop for a while


In the end, this is the script that seems to work for me:


tell application "Finder"

set urlsFolder to "HD:Users:me:calendars:urls" as alias

set backupsFolder to "HD:Users:me:calendars:backups" as alias


set {year:y, month:m, day:d} to (current date)

set newBackupsFolderName to y & m & d as string

set newFolder to makenewfolderatbackupsFolderwith properties {name:newBackupsFolderName}

set newBackupFolder to (POSIX path of backupsFolder) & newBackupsFolderName & "/"


set allURLFiles to every file in urlsFolder

repeat with aURLFile in allURLFiles

set theName to displayed name of aURLFile

set thePath to newBackupFolder & theName & ".ics"

set theURL to location of aURLFile

set theCommand to "curl " & theURL & " > " & thePath

do shell scripttheCommand

end repeat

end tell


This is, I'm sure, far from perfect, and since I ended up using curl to do the heavy lifting there was probably a way to do it entirely in a shell script... but it works, and that's what matters. The "displayed name" bit probably works, I assume, for stripping the extension since I have those files set to hide their extensions, and it'll give an error if the new folder with the dated name already exists.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Download multiple files and rename

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.