Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

TuffLuffJimmy

macrumors G3
Original poster
Apr 6, 2007
9,036
180
Portland, OR
I have been trying to write an automator script that will take various files from my downloads folder and my desktop folder and organize each file into various parts of my computer.

I have it set to select those folders, then get their contents, then filter by extension and then move those files to a certain other folder. The problem is I have it filter by extension, png first and if there are no png files the script ends with an error. So I have to have each different kind of file sitting in one of those folders or else the script ends. Is there a way that I can get around this and have the whole script run every time? Or is there some way (maybe variables) that I could get this to work.

I have done a little apple script before, but nothing with variables and nothing too fancy, although it's one of my priorities this summer to learn the language.
 
While your desire to learn Applescript and Automator is something great, this sounds like a job for the little app called Hazel. From my limited understanding of it, Hazel can do little jobs like cleaning up folders and files and stuff.

I know that doesn't help with your desire for Automator help, but it could be a solution to your problems.
 
Thank you very much.
This really does seem to do what I want it to do, however I would rather not pay twenty dollars for it since this can be done with a simple script.
 
Yes, I've been trying to do the same thing. I want to be able to have Automator sort my Downloads folder into appropriate folders.

Has anyone been able to do this?
 
I was able to tweak what happens to the report, and I think this version's better:

Code:
-- initialize two functions we need at end of script

on twodigits(num) -- turns "7/8/9" to '07/08/09"
	if num > 9 then
		return (num as string)
	else
		return "0" & (num as string)
	end if
end twodigits

on datePeriods() -- fetches date string
	set theYear to the year of the (current date) as string
	set theMonth to my twodigits((the month of the (current date)) as integer)
	set theDay to my twodigits(the (the day of the (current date)) as integer)
	return theYear & "." & theMonth & "." & theDay
end datePeriods

-- initialize variables

set allMessages to ""

set dlPath to path to downloads folder

set dlPosix to POSIX path of dlPath

-- this next line could be replaced with the exact 
-- location, rather than prompting the user

set targetFolder to (choose folder with prompt "Where should your files go?")

set tFPosix to POSIX path of targetFolder

-- real business begins

tell application "Finder"
	
	set allTheFiles to items of dlPath
	
	-- depending on how big your downloads folder is, this may take a while
	
	repeat with n in allTheFiles
		
		set nKind to (kind of n) as text
		
		set fileTarget to (targetFolder as string) & nKind & ":"
		
		if not (exists fileTarget) then
			try
				make new folder at (targetFolder as string) with properties {name:nKind}
			on error errMsg number errNum
				display dialog errMsg & return & "Number: " & errNum as string
			end try
			
		end if
		
		set sourceFile to quoted form of POSIX path of (n as string) -- the file to be moved
		
		set tFPosix to quoted form of POSIX path of fileTarget -- where the file will go
		
		set mvMessage to do shell script "mv -vn " & sourceFile & " " & tFPosix -- the real business
		
		set allMessages to allMessages & return & mvMessage -- we save the messages because we're anal
		
	end repeat
	
end tell

set theFileName to "Files Moved on " & my datePeriods() & ".txt"

tell application "Finder"
	set theFileObject to (make new file at dlPath with properties {name:theFileName})
end tell

set theFile to theFileObject as alias

try
	set fileRef to (open for access theFile with write permission)
on error errMsg number errNum
	display dialog ("Open for Access, Error Number: " & errNum as string) & return & errMsg
end try

set filesEOF to get eof fileRef

set eof of fileRef to 0

try
	write allMessages to fileRef
on error errMsg number errNum
	display dialog ("Write, Error Number: " & errNum as string) & return & errMsg
end try


set eof of fileRef to (length of allMessages)

try
	close access fileRef
on error errMsg number errNum
	display dialog ("Close, Error Number: " & errNum as string) & return & errMsg
end try
 
Thanks for the help. Is there a way to do this Automator? I have multiple file type detection scripts running in a main Automator script. I'm having some glitches with this way of doing it. Is there an easy way to do this?
 
There is no real concept of "if" in Automator. It is more like a work pipeline, and a completely linear one at that.
 
There is no real concept of "if" in Automator. It is more like a work pipeline, and a completely linear one at that.

I think larkost is right, inasmuch as there's no "if" action. Possibly you could contain an if/then construct inside an AppleScript that you call from within Automator. At this point, I've been stymied. I keep running up against the things I can't figure out how to do with Automator that I can't figure how to do the things it does.

mt
 
I have the main script running separate workflows. Each one takes the selected input and filters it against what kind of item it needs, such as an image or music. It then moves the item into its appropriate folder.

This almost works. A problem I have is that if one item is selected in the Finder and then I run the workflow, is that it starts to use other items too, even if they are not selected. Is there something special about the service command, or is it not intended for single items?
 
Here are pictures of the Automator workflow. There is the main workflow, and then the individual workflows for each filetype. Is this the right way to do this?

Thanks for all of your help.
 

Attachments

  • Screen shot 2010-01-11 at 3.24.17 AM.png
    Screen shot 2010-01-11 at 3.24.17 AM.png
    317.2 KB · Views: 72
  • Screen shot 2010-01-11 at 3.24.40 AM.png
    Screen shot 2010-01-11 at 3.24.40 AM.png
    238.6 KB · Views: 53
I have the main script running separate workflows. Each one takes the selected input and filters it against what kind of item it needs, such as an image or music. It then moves the item into its appropriate folder.

This almost works. A problem I have is that if one item is selected in the Finder and then I run the workflow, is that it starts to use other items too, even if they are not selected. Is there something special about the service command, or is it not intended for single items?

You'd need a separate workflow to handle selected files. I'm not sure how Automator alone will be able to decide what kind of file it is and then move it to the appropriate folder. You might still need AppleScript.

mt
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.