Go Back   The macosxhints Forums > OS X Help Requests > AppleScript



Reply
 
Thread Tools Rate Thread Display Modes
Old 06-02-2012, 02:31 AM   #1
TechSysPete
Prospect
 
Join Date: May 2012
Location: Savannah, Ga
Posts: 5
Copy files and sort into folders by file extension

Basically like most geeks I'm constantly downloading files from the internet. Whether it be the days funny demotivational pic I find on Facebook and other sites to script files, pdf's (I print webpages to pdf like a mad man!) and whatever else I can find that's worth keeping and reading/viewing again later. So the script was written to choose all the files in my "Downloads" folder and then sort them to the correct directories.

My directory hierarchy:

/Scripts
--sh
--scpt
/Users/me/Documents/AllDocs
--pdf
--docx
--xlsx
--pptx
--txt
/Users/me/Pictures/AllPics
--png
--jpg
--gif (you know there are some REALLY funny ones out there!)

So what I would like help with is to hard code the folder locations so that I just pick the source location (my Download folder) and all files are sorted the correct directory. And first and foremost I REALLY hope others can find this script and use it themselves!!!! Thanks to all that view and offer help.

Code:
set homeFolder to "Macintosh HD:Users:macncheese:Downloads:"
set homeFolder to homeFolder as text
set travelFolder to "Macintosh HD:Users:macncheese:Test1:"
set travelFolder to travelFolder as text
set rtfFolderName to "rtf"
set pdfFolderName to "pdf"
set docxFolderName to "docx"
set xlsxFolderName to "xlsx"
set pptxFolderName to "pptx"
set jpgFolderName to "jpg"
set pngFolderName to "png"
set gifFolderName to "gif"
set shFolderName to "sh"
set scptFolderName to "scpt"

-- choose the files
set theFiles to choose file with prompt "Choose the files you want to copy!" with multiple selections allowed

-- make sure the folders exist
tell application "Finder"
	if not (exists folder (travelFolder & rtfFolderName)) then
		make new folder at folder travelFolder with properties {name:rtfFolderName}
	end if
	if not (exists folder (travelFolder & pdfFolderName)) then
		make new folder at folder travelFolder with properties {name:pdfFolderName}
	end if
	if not (exists folder (travelFolder & docxFolderName)) then
		make new folder at folder travelFolder with properties {name:docxFolderName}
	end if
	if not (exists folder (travelFolder & xlsxFolderName)) then
		make new folder at folder travelFolder with properties {name:xlsxFolderName}
	end if
	if not (exists folder (travelFolder & pptxFolderName)) then
		make new folder at folder travelFolder with properties {name:pptxFolderName}
	end if
	if not (exists folder (travelFolder & jpgFolderName)) then
		make new folder at folder travelFolder with properties {name:jpgFolderName}
	end if
	if not (exists folder (travelFolder & pngFolderName)) then
		make new folder at folder travelFolder with properties {name:pngFolderName}
	end if
	if not (exists folder (travelFolder & gifFolderName)) then
		make new folder at folder travelFolder with properties {name:gifFolderName}
	end if
	if not (exists folder (travelFolder & shFolderName)) then
		make new folder at folder travelFolder with properties {name:shFolderName}
	end if
	if not (exists folder (travelFolder & scptFolderName)) then
		make new folder at folder travelFolder with properties {name:scptFolderName}
	end if
end tell

-- copy the files
repeat with aFile in theFiles
	set fileExtension to item 2 of getNameAndExtension(aFile)
	if fileExtension is "rtf" then
		tell application "Finder"
			copy aFile to folder (travelFolder & rtfFolderName)
		end tell
	else if fileExtension is "pdf" then
		tell application "Finder"
			copy aFile to folder (travelFolder & pdfFolderName)
		end tell
	else if fileExtension is "docx" then
		tell application "Finder"
			copy aFile to folder (travelFolder & docxFolderName)
		end tell
	else if fileExtension is "xlsx" then
		tell application "Finder"
			copy aFile to folder (travelFolder & xlsxFolderName)
		end tell
	else if fileExtension is "pptx" then
		tell application "Finder"
			copy aFile to folder (travelFolder & pptxFolderName)
		end tell
	else if fileExtension is "jpg" then
		tell application "Finder"
			copy aFile to folder (travelFolder & jpgFolderName)
		end tell
	else if fileExtension is "jpeg" then
		tell application "Finder"
			copy aFile to folder (travelFolder & jpgFolderName)
		end tell
	else if fileExtension is "png" then
		tell application "Finder"
			copy aFile to folder (travelFolder & pngFolderName)
		end tell
	else if fileExtension is "gif" then
		tell application "Finder"
			copy aFile to folder (travelFolder & gifFolderName)
		end tell
	else if fileExtension is "sh" then
		tell application "Finder"
			copy aFile to folder (travelFolder & shFolderName)
		end tell
	else if fileExtension is "scpt" then
		tell application "Finder"
			copy aFile to folder (travelFolder & scptFolderName)
		end tell
	end if
end repeat



(*=============== SUBROUTINES ===============*)
on getNameAndExtension(F)
	set F to F as Unicode text
	set {name:Nm, name extension:Ex} to info for file F without size
	if Ex is missing value then set Ex to ""
	if Ex is not "" then
		set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
	end if
	return {Nm, Ex}
end getNameAndExtension
TechSysPete is offline   Reply With Quote
Old 06-02-2012, 05:18 PM   #2
Red_Menace
All Star
 
Join Date: Mar 2006
Location: Littleton, Colorado, USA
Posts: 506
Standard Additions has a path to comand that will return a file alias for various default folder locations. This alias can be converted to text so additional path components can be added, and the resulting path can then be used as the destination folder. Note that since the name extensions you are looking for and the corresponding folder name are the same, you can shorten your script quite a bit by using these in some lists, for example:

Code:
set scriptsFolder to ((path to startup disk) as text) & "Scripts:"
set scriptExtensions to {"sh", "scpt"}
makeFolders_atFolder_(scriptExtensions, scriptsFolder) -- create subfolders as needed

set documentsFolder to ((path to documents folder) as text) & "AllDocs:"
set documentExtensions to {"pdf", "docx", "xlsx", "pptx", "txt"}
makeFolders_atFolder_(documentExtensions, documentsFolder) -- create subfolders as needed

set picturesFolder to ((path to pictures folder) as text) & "AllPics:"
set pictureExtensions to {"png", "jpg", "gif"}
makeFolders_atFolder_(pictureExtensions, picturesFolder) -- create subfolders as needed

repeat with aFile in (choose file with prompt "Choose the files you want to copy!" default location (path to downloads folder) with multiple selections allowed)
	set {theName, fileExtension} to getNameAndExtension_(aFile)
	if fileExtension is in scriptExtensions then
		tell application "Finder" to duplicate aFile to folder (scriptsFolder & fileExtension)
	else if fileExtension is in documentExtensions then
		tell application "Finder" to duplicate aFile to folder (documentsFolder & fileExtension)
	else if fileExtension is in pictureExtensions then
		tell application "Finder" to duplicate aFile to folder (picturesFolder & fileExtension)
	else -- no extension match
		log "file " & quoted form of (theName & "." & fileExtension) & " was skipped"
	end if
end repeat


on getNameAndExtension_(someFile)
	set someFile to someFile as text
	tell application "System Events" to tell disk item someFile to set {theName, theExtension} to {name, name extension} -- info for is deprecated
	if theExtension is not "" then set theName to text 1 thru -((count theExtension) + 2) of theName -- just the name part
	return {theName, theExtension}
end getNameAndExtension_


on makeFolders_atFolder_(someList, someFolder) -- make a folder structure with the mkdir shell script
	# items in someList with special characters (such as spaces) need to be quoted or the characters escaped
	set someFolder to someFolder as text
	if someFolder does not start with "/" then set someFolder to POSIX path of someFolder
	log someFolder
	# build a parameter string from items in the folder list
	set {tempTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
	set {parameters, AppleScript's text item delimiters} to {someList as text, tempTID}
	
	do shell script "cd " & quoted form of someFolder & "&& mkdir -p " & parameters
end makeFolders_atFolder_
__________________
MacBook Pro / OS X Mountain Lion (10.8.3) / Xcode 4.6 / [various (much) older stuff keeping dust off the shelves]
Red_Menace is offline   Reply With Quote
Old 06-03-2012, 12:45 AM   #3
TechSysPete
Prospect
 
Join Date: May 2012
Location: Savannah, Ga
Posts: 5
Red_Menace....you sir, need a "Donate" button!

Thank you so much. I'm going to study this for the next hour and more in the morning when I get up. Again thank you!!
TechSysPete is offline   Reply With Quote
Old 06-03-2012, 08:27 PM   #4
TechSysPete
Prospect
 
Join Date: May 2012
Location: Savannah, Ga
Posts: 5
One more thing I forgot to ask.

Could I have the selected files deleted after the copy action? I see you used the "duplicate" command and I find nothing about it being able to delete. Guessing it's just a very simple command. So is there a way to delete them the duplicate command finishes?

Looking through what you've written I would like to try adding in something like

tell application "Finder" to delete aFile
end tell

If this is correct, or near correct, where would I insert it? I assume at the very end but you're script is far beyond anything I can comprehend at this moment but I look forward to tearing it apart more when I have time so that I can learn why/how things do what they do. Of all the googling I've done for Applescript scripts this is by far one of the most interesting ones.
TechSysPete is offline   Reply With Quote
Old 06-03-2012, 10:29 PM   #5
Red_Menace
All Star
 
Join Date: Mar 2006
Location: Littleton, Colorado, USA
Posts: 506
The scripting dictionary of the Finder (or any scriptable application) can be opened in the AppleScript Editor, where you can view the commands and terms that have been provided.

Your original script used copy (which isn't used for files), so I just guessed that you were wanting to copy (duplicate) the file. There is also a move command, which would obviate the need to delete an original (although there is a delete command), so that can be used instead of duplicate.
__________________
MacBook Pro / OS X Mountain Lion (10.8.3) / Xcode 4.6 / [various (much) older stuff keeping dust off the shelves]
Red_Menace is offline   Reply With Quote
Old 06-03-2012, 11:23 PM   #6
TechSysPete
Prospect
 
Join Date: May 2012
Location: Savannah, Ga
Posts: 5
Thanks for the heads up about the scripting dictionary. In case of power failure or other anomaly would you please help me with adding a delete line?
TechSysPete is offline   Reply With Quote
Old 06-03-2012, 11:43 PM   #7
Red_Menace
All Star
 
Join Date: Mar 2006
Location: Littleton, Colorado, USA
Posts: 506
The variable aFile still refers to the current file, so to delete the original item after copying it, you can just use it with a delete command, for example (I surrounded the file duplication parts with an error statement so that if there is an error in duplicating, the original won't get deleted):

Code:
 repeat with aFile in (choose file with prompt "Choose the files you want to copy!" default location (path to downloads folder) with multiple selections allowed)
	set {theName, fileExtension} to getNameAndExtension_(aFile)
	try
		if fileExtension is in scriptExtensions then
			tell application "Finder"
				duplicate aFile to folder (scriptsFolder & fileExtension)
				delete aFile
			end tell
		else if fileExtension is in documentExtensions then
			tell application "Finder"
				duplicate aFile to folder (documentsFolder & fileExtension)
				delete aFile
			end tell
		else if fileExtension is in pictureExtensions then
			tell application "Finder"
				duplicate aFile to folder (picturesFolder & fileExtension)
				delete aFile
			end tell
		else -- no extension match
			log "file " & quoted form of (theName & "." & fileExtension) & " was skipped"
		end if
	on error errorMessage
		log errorMessage -- just log the error
	end try
end repeat
__________________
MacBook Pro / OS X Mountain Lion (10.8.3) / Xcode 4.6 / [various (much) older stuff keeping dust off the shelves]
Red_Menace is offline   Reply With Quote
Old 06-03-2012, 11:47 PM   #8
TechSysPete
Prospect
 
Join Date: May 2012
Location: Savannah, Ga
Posts: 5
Again Red_Menace thank you so much for your time! I'm going to read through it tomorrow and learn myself something.
TechSysPete is offline   Reply With Quote
Reply

Tags
applescript, copy, extension, file, sort

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 11:18 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.