I will start by saying I am fairly new to AppleScript. I have really only written one script and that was with some help from this forum. I am going to take a shot at my second script now to automate another task I have to perform. I am hoping that I can get some insight on the best direction to go with this.
Inside of FolderA there are a group of folders. Inside each of these folders are other files and folders. And inside those folders are Files. So it goes three levels deep at the most. I need to scan through these folders and grab all the files with ‘today’s date’. The each of these files should be FTPed to a location with the same directory structure and placed into the proper folder.
I have been reading a lot online about different FTP options. It seems that one was discontinued as of Lion and I am running Mavericks. So, it seems that my best bet is going with do script and using either FTP or CURL. Neither one will upload a directory so I am forced to upload each file individually. I know FTP well but really haven’t used CURL before. But, curl appears to be the more popular method with applescript. Is there a reason for using curl over FTP with my scenario.
This is the workflow I have in mind right now:
set LocalDirectory
set RemoteDirectory
Get list of Folders
REPEAT for each FOLDER in LIST
CD to Directory
Get list of Files with today's date
if list NOT NULL
Set Directory to LocalDirectory+
Set RemoteDirectory+
REPEAT for each FILE in LIST
FTP file
END REPEAT
END REPEAT
Is there a better method to go with solving this problem?
In the end I will run this script on a trigger that runs it a couple of times per week at a specific time.
I started working on this script off of my workflow. I haven’t got to the actual FTP portion so I am still interested in thoughts on that method.
I am getting an error with the small amount of script I have written. It is complaining that "Finder got an error: Can’t make [filename] to integer.
I know it has something to do with the line where I am trying to get the modification date but I can’t figure out why. I want that modification date so that I can check if it is today’s date.
tell application "Finder"
set all_folder to name of every item in folder LocalDirectory
repeat with i from 1 to count all_folder
set LocalPath to LocalDirectory & all_folder's item i
set all_files to name of every item in folder LocalPath
repeat with x from 1 to count all_files
set modDate to the modification date of file all_files's item i
end repeat
end repeat
end tell
I’m not really making much progress here. I have been reading a bunch of other forum posts and the error I am receiving seems to be related to the me using a System Alias.
I changed my script slightly trying to get better results and I get a different error now. I’m not sure if it is better or worse. I know have
tell application "Finder"
set all_folder to name of every item in folder LocalDirectory
repeat with i from 1 to count all_folder
set LocalPath to LocalDirectory & all_folder's item i
set all_files to name of every item in folder LocalPath
repeat with x from 1 to count all_files
set theFile to LocalPath & ":" & all_files's item i
display dialog theFile
set modDate to the modification date of (info for theFile)
end repeat
end repeat
end tell
The error I get now is that “Finder got an error: path to file” number -43 from path to file"
I put the display dialog in so I could see the value of theFile before it goes into the modification date and the file does exist. The -43 error seems to imply that the File wasn’t found.
I have changed the script and I think I have accomplished everything except for the FTP. I am going to work on the FTP portion now. I think I am going to open an FTP at the start of the script and then do a put statement with each file on the inner if statement. Then I will close the FTP at the end. Does this sound like a good approach?
tell application "Finder"
set LocalDirectory to "Macintosh HD:Users:msposato:Library:Application Support:Out of the Park Developments:OOTP Baseball 14:saved_games:NABL.lg:news:html:"
set modDays to 1
set all_folder to name of every item in folder LocalDirectory
repeat with folderItem in all_folder
set thePath to LocalDirectory & folderItem
if kind of (info for alias thePath) is "Folder" then
display dialog thePath
set LocalFolder to LocalDirectory & folderItem & ":"
set fileList to list folder LocalFolder without invisibles
set newFiles to {}
set modDate to (current date) - modDays * days
repeat with fileItem in fileList
set theFile to contents of fileItem
set infoFile to info for file (LocalFolder & theFile)
if modification date of infoFile > modDate then
set end of newFiles to theFile
end if
end repeat
end if
end repeat
end tell
this is a solution using Spotlight which is much faster than iterating through the entire folder hierarchy.
cURL is the best way to upload FTP data, because it’s also able to create missing directories on-the-fly.
Change the values of the property lines to your FTP account data
property serverPath : "[url=ftp://ftp.mydomain.com/path/to/directory/]ftp.mydomain.com/path/to/directory/[/url]" -- the trailing slash is important
property user_name : "myUserName"
property pass_word : "myPassWord"
set userApplicationSupportDirectory to POSIX path of (path to application support folder from user domain)
set localDirectory to userApplicationSupportDirectory & "Out of the Park Developments/OOTP Baseball 14/saved_games/NABL.lg/news/html/"
set pathOffset to (length of localDirectory) + 1
set filesToBeUploaded to paragraphs of (do shell script "mdfind -onlyin " & quoted form of localDirectory & " 'kMDItemContentModificationDate >= $time.today && kMDItemKind != \"Folder\"'")
repeat with aFile in filesToBeUploaded
set partialFilePath to text pathOffset thru -1 of aFile
set ftpFilePath to serverPath & partialFilePath
do shell script "curl --ftp-create-dirs " & quoted form of ftpFilePath & " --anyauth -u " & user_name & ":" & pass_word & " -T " & quoted form of aFile
end repeat
StefanK - thanks for the response. I was debating between curl and NcFTP for the file uploads. I don’t really need to create directories on the fly since the directory structure of my needs here will always be the same. NcFTP does seem to be faster. I was seeing about 3 seconds to upload a single html file through curl. When running this script there will be about 25,000 files that will have changed and will need to be uploaded. If I run this through filezilla manually through the GUI it gets the job done in 35-40 minutes. I was hoping to get similar performance out of my script.
The mdfind that you use in your script is nice. I wasn’t familiar with it but did some research after seeing your reply. It seems very powerful. But, does this line of code ever go to sub directories and upload files in those directories that have changed. I notice that a lot of times the files within a folder have changed and have todays date but the folder itself does not. This is why I was going into the folders and searching through each file. Is this a realistic concern?