I want to start by saying that I am a complete NEWBIE with apple script. I am average with a couple of other scripting languages but not really an expert at any of them. I have started reading into apple script but seem to be missing the big picture. I am trying to complete the task below. I looked at this for a couple of hours and went through the forums trying to find samples that were similar, but I honestly didn’t get very far. It doesn’t seem like it should be that difficult but I am lost. Any help would be greatly appreciated.
I have a folder where my images are placed when being imported from different devices. The images will have the naming structure of MMDDYYY_IMG###.whatever.
I want to be able to read the filename and pull out the MM and the YYYY. I then have a group of folders that are created which are named 01JAN2014, 02FEB2014, 03MAR2014, etc… Based off the MM and YYYY that are in the file name, I want to have the script move the files to the appropriate directory.
set filnam to "03MAR2014_IMG2209"
set theday to characters 1 thru 2 of filnam as text
set themonth to characters 3 thru 5 of filnam as text
set theyear to characters 6 thru 9 of filnam as text
display dialog "Month: " & themonth & "\rDay: " & theday & "\rYear: " & theyear
Getting the names of all the files in a folder:
set whichfolder to choose folder
tell application "Finder"
set lst to name of every file in folder whichfolder
end tell
return lst
Sorry to keep jumping on this one, but ‘characters . as text’ extracts a list of characters and then coerces it to text. It shouldn’t really be used, therefore, without explicitly setting AppleScript’s text item delimiters to “” or to {“”} first. Extracting the ‘text’ directly is more efficient, less to type, and foolproof:
set filnam to "03MAR2014_IMG2209"
set theday to text 1 thru 2 of filnam
set themonth to text 3 thru 5 of filnam
set theyear to text 6 thru 9 of filnam
display dialog "Month: " & themonth & "
Day: " & theday & "
Year: " & theyear
try this, the script assumes that the input date format MMDDYYY_IMG###.whatever has always the same length (2 digits month and day, 4 digits year.
you will asked for the input folder containing the images.
The subfolders are created automatically and the images are moved.
If the property baseArchiveFolder remains missing value the folders are created in the input folder.
You can specify a POSIX path (with a trailing slash) for a different folder
property monthAbbreviations : {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}
property baseArchiveFolder : missing value -- "/Users/myUser/path/to/folder/" <-- the trailing slash is crucial
set baseImportFolder to choose folder
tell application "Finder" to set imageFiles to files of baseImportFolder
if baseArchiveFolder is missing value then
set destinationFolder to POSIX path of baseImportFolder
else
set destinationFolder to baseArchiveFolder
end if
repeat with aFile in imageFiles
set fileName to name of aFile
try
tell fileName to set {theMonth, theDay, theyear} to {text 1 thru 2, text 3 thru 4, text 5 thru 8}
set folderName to theMonth & item (theMonth as integer) of monthAbbreviations & theyear
set sourceFile to quoted form of POSIX path of (aFile as text)
set destinationPath to quoted form of (destinationFolder & folderName & "/" & fileName)
do shell script "/usr/bin/ditto " & sourceFile & space & destinationPath
do shell script "/bin/rm " & sourceFile
on error
display dialog "The file name " & fileName & " seems to be in wrong format"
end try
end repeat
Combining the two together I end up with something like this:
set whichfolder to "Macintosh HD:Users:testuser:Downloads:Testing:"
tell application "Finder"
set lst to name of every file in folder whichfolder
end tell
--set filnam to "03MAR2014_IMG2209"
set theday to text 1 thru 2 of filnam
set themonth to text 3 thru 5 of filnam
set theyear to text 6 thru 9 of filnam
display dialog "Month: " & themonth & "
Day: " & theday & "
Year: " & theyear
So, now there seems like there are two things that I need to do. I would need to define the filnam off of the the lst that is defined from the folder search.
I would think instead of reading ‘lst to name of every file in folder whichfolder’ that I should be reading it into an array of some sort. Is this different with applescript? With applescript do i get the lst and then split that up to create move statements?
I would also need to loop through the group of set commands that read the filenames. I would assume within this loop I would do the sets and do the move all at once.
set whichfolder to "Macintosh HD:Users:testuser:Downloads:Testing:"
So whichfolder is a string that represents a path, and hence needs the word folder to make a folder object.
In the original script, whichfolder was the result of the choose folder command, which returns an alias, not a path. If you put the word folder in front of an alias, you will get an error. You either use the alias alone, or convert the alias to a path and add the word folder.
Paths and aliases are two different things, and need to be treated differently.
Shane Stanley - that makes sense. Thanks for the clarification. I will be passing the path to the script rather than selecting the script each time. So I will keep ‘folder’ in there per your explanation. Thanks.
I am really struggling to transferring from lst being the list of filenames to filnam being the name of the file. I have written this:
set whichfolder to "Macintosh HD:Users:UserName:Downloads:Testing:"
tell application "Finder"
set lst to name of every file in folder whichfolder
repeat the count of lst times
set filnam to every item of lst
set theday to text 1 thru 2 of filnam
set themonth to text 3 thru 5 of filnam
set theyear to text 6 thru 9 of filnam
end repeat
end tell
I thought the line
set filnam to every item of lst
would make filnam each of the individual filenames so that when i got the text of the characters i would get the characters of the filename as opposed to the the filenames. So, when i get the above the result is files 6, 7, 8, 9 of the list of files.
set whichfolder to "Macintosh HD:Users:UserName:Downloads:Testing:"
tell application "Finder"
set lst to name of every item in folder whichfolder
repeat with i from 1 to count lst
set lst's item 1 to text 1 thru 2 of lst's item i
-- set themonth to text 3 thru 5 of filnam
-- set theyear to text 6 thru 9 of filnam
end repeat
end tell
it returns “01” as a result which seems appropriate. But, I would expect to have seen the first two characters for every file in the folder. Why am I only seeing one result? Does it only show the last? If I put a move command in the repeat statement would it move each file to the appropriate folder?
OK. I think I have one more question then I can resolve this. I am hoping.
With the above code snippet lst’s item 1 is equal to the first 2 characters of the file name. But, I can’t figure out what is equal to the file name anymore. I know lst is the list of all filenames. Prior to this statement
set lst's item 1 to text 1 thru 2 of lst's item i
lst’s item 1 is equal to the file name (I believe). How can I get a variable equal to the filename and the first two characters of the filename at the same time?
set whichfolder to "Macintosh HD:Users:msposato:Downloads:Testing:"
tell application "Finder"
set lst to name of every item in folder whichfolder
repeat with i from 1 to count lst
set theMonth to text 1 thru 2 of lst's item i
set theDay to text 3 thru 4 of lst's item i
set theYear to text 8 thru 5 of lst's item i
move file "Macintosh HD:Users:UserName:Downloads:Testing:" & lst's item 1 to "Macintosh HD:Users:UserName:Desktop:Test:" & lst's item 1 with replacing
end repeat
end tell
the move statement is erroring out. I am making progress but not quite there yet.
set whichfolder to "Macintosh HD:Users:UserName:Downloads:Testing:"
tell application "Finder"
set lst to name of every item in folder whichfolder
repeat with i from 1 to count lst
set theMonth to text 1 thru 2 of lst's item i
set theDay to text 3 thru 4 of lst's item i
set theYear to text 8 thru 5 of lst's item i
set the_FullFile to "Macintosh HD:Users:UserName:Downloads:Testing:" & lst's item i
move file the_FullFile to "Macintosh HD:Users:UserName:Desktop:Test:" with replacing
end repeat
end tell
It moves all the files for me from one folder to another. Are there case statements’s in AppleScript? I believe the only thing I need to do now is instead of just one move either use a case statement of a series of if statements to move it the proper directory using theMonth and theYear as my criteria and the folder names to move it to.
I will have to work on this tomorrow.
If anyone sees any problems with what I have written so far please let me know.