Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.4: Print a folder's contents via Automator Desktop
On numerous occasions, I've been asked by my PC-using colleagues if there was a way to print out the contents of a folder. My quick hack usually involved hitting the Print Screen button with Windows Explorer open on the specified folder, and then pasting the image into word processor for printing.

Automator makes it easy to get the contents of any folder on a Mac as a text file ready for printing, batch processing or any other purpose.

First, open Automator and from the list of Finder Actions, double click on Get Selected Finder Items (or drag it from the Action list to the right side of the window). Then, double click on Get Folder Contents, and finally, double click on New Text File from the list of TextEdit Actions.

For more flexibility, under the New Text File action's options, check Show Action When Run and Show Selected Items with both Save As and Where options checked. The Finder will in return, ask you where to save the file and how to name it.

Save the action as a Finder plug-in, and control-click on any folder in the Finder to run your newly-created action from the contextual menu. The contents of specified folder will be saved in a text file (one item per line), incredibly useful in certain situations. (Now, if scripting is your niche, I also find it useful to trim the path of the file and keep the names only...)

[robg adds: As an alternative, we've covered printing folder contents via drag-and-drop to a desktop printer icon before. For more advanced Finder window printing needs, I'm partial to Print Window.]
    •    
  • Currently 4.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (9 votes cast)
 
[66,402 views]  

10.4: Print a folder's contents via Automator | 19 comments | Create New Account
Click here to return to the '10.4: Print a folder's contents via Automator' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: Print a folder's contents via Automator
Authored by: tomaira on Jul 26, '06 08:57:01AM

Sweet! I've always done it manually from a Terminal window, by redirecting a list output to a file like this:

ls [path to folder] > [path to output file]

Never thought of the possibility to actually do this with Automator instead. Thanks!



[ Reply to This | # ]
10.4: Print a folder's contents via Automator
Authored by: nickdynamite on Jul 26, '06 09:17:21AM

huge fan of Print Window here as well. Great app.



[ Reply to This | # ]
10.4: Print a folder's contents via Automator
Authored by: OceanCubed on Jul 26, '06 09:50:25AM

Another option is PrintWindow from

[link:]http://www.searchwaresolutions.com/

The free version works perfectly.



[ Reply to This | # ]
10.4: Print a folder's contents via Automator
Authored by: OceanCubed on Jul 26, '06 09:52:01AM

Apologies for the above, perhaps if I read the entire post before I post, I might make a better contribution!!!



[ Reply to This | # ]
10.4: Print a folder's contents via Automator
Authored by: mm2270 on Jul 26, '06 09:57:39AM

Nice hint. But the only problem I see with this is that Automator dumps the entire path to the files as well as the filenames into the resulting text file.

If all you want is a list of the folder contents, this doesn't exactly get you there.

You could of course use a text editor app like BBEdit or some other program to batch delete the path portion, since in most cases it will be the same for all items, being that they're in the same folder.
But it would be nice if Automator allowed you to customize what it actually grabbed to dump into the text file.



[ Reply to This | # ]
10.4: Print a folder's contents via Automator
Authored by: puggheim on Jul 26, '06 11:35:53AM

I allways just drag the folder to Printer Setup Utility (since I like a clean desktop with no desktop printers - these can be used for this too) - then choose printer or save as PDF or whatever...



[ Reply to This | # ]
10.4: Print a folder's contents via Automator
Authored by: guitarrgirl on Mar 22, '07 11:03:45AM

This one works, but doesn't list subfolders

:)
m



[ Reply to This | # ]
10.4: Print a folder's contents via Automator
Authored by: mm2270 on Jul 26, '06 11:42:18AM

Actually, trimming down the listing can be easily own right in the standard TextEdit.
Just hilite copy the full path minus the filename from the first line, open the Find/Replace dialog and paste it in the Find filed, leave the Replace filed blank and hit Replace All. Voila! All cleaned up.

This works much better than copy/paste from the Finder, since that has been broken since th eday OS X was born (pastes items in completely random order)



[ Reply to This | # ]
10.4: Print a folder's contents via Automator
Authored by: magnamous on Jul 29, '06 01:34:56AM
I missed OS 9's ability to print out a folder hierarchy just by highlighting a bunch of disclosed folders in list view, hitting copy, and hitting paste in SimpleText. It would put out a tabbed hierarchy of all of the folders. Very nice, very neat.

I tried to recreate that effect with an applescript, and it works pretty well. I have two problems: I can't get it to work as part of the Automator workflow (it doesn't seem to like that I defined a few functions of my own), and I don't know how to get it to avoid duplicate folder hierarchies for multiple folders in the same path. In other words:

/Users/me/foo/ & /Users/me/bar/
comes out as:


Users
  me
    foo
Users
  me
    bar

instead of:

Users
  me
    foo
    bar

and I don't know how to get it to do what I want. Any thoughts?

My script is extremely ugly, and I'm sure it could be much more optimized, but I'm pushing my skills to have gotten this far. Anyway, here it is, for those who are interested. By the way, I'm sorry that the code makes the page huge, but I don't know how to enclose it in anything. I remember seeing comments before that have some sort of frame with scrollbars embedded in the page, and that contains their code, but I don't know how to do that. Any suggestions on that are greatly appreciated!


tell application "TextEdit"
	tell text of document frontmost
		
		-- this gets rid of the leading / in each path. It's much easier to do it this way than to do it when tabbing out each line, and using the TextEdit character handlers is much slower.
		repeat with dLoop from 1 to (count of paragraphs)
			if the first character of paragraph dLoop is "/" then
				delete the first character of paragraph dLoop
			end if
		end repeat
		
		-- so we can see whether the current item is in the same folder as the last item
		set lastFolder to "" as string
		
		-- where we are in the list of items in TextEdit
		set eachParagraph to 1
		
		-- go through the entire list of items
		repeat while eachParagraph is less than or equal to (count of paragraphs)
			
			-- get the number of slashes in a given line
			set slashCount to the count of (every character where it is "/") in paragraph eachParagraph
			
			-- we'll need this later for various things
			set pathCharsList to characters of paragraph eachParagraph
			
			-- this tells us the current *folder* path (i.e. not including the file name at the end)
			set folderCharCount to my listPosition("/", pathCharsList, slashCount)
			
			-- if the line contains no / marks, then listPosition for folderCharCount returns 0. If that's the case, setting the value of thisFolder would get an error because of the negative math, so we set thisFolder to 0 and do some error handling later on.
			if folderCharCount is not 0 then
				set thisFolder to ((characters 1 through folderCharCount) of paragraph eachParagraph) as string
			else
				set thisFolder to 0
			end if
			
			-- we can't really do anything to the line if it has no / marks...
			if thisFolder is not 0 then
				
				-- the first half of this "if" statement tabs out a new folder listing
				if thisFolder is not equal to lastFolder then
					
					-- preparing for the next iteration of the loop. There should not be a possibility of a corrupted value, but I'm setting it first because the path in the list is going to change. better safe than sorry.
					set lastFolder to thisFolder
					
					-- which slash are we going to work on right now (below)? that determines how many tabs to create.
					set thisSlashCount to 0
					
					-- create the tabs.
					-- this is tricky because the paragraph number changes each time you change a / to a return and tabs, and the character count for the new line changes, so both of those reference numbers have to be updated every time you change a /
					set cLoop to 1
					repeat while cLoop is less than (count of pathCharsList)
						if (character cLoop of paragraph eachParagraph) is "/" then
							-- we've encountered a slash
							set thisSlashCount to thisSlashCount + 1
							-- turn that slash into a return and the appropriate number of tabs (based on thisSlashCount)
							set character cLoop of paragraph eachParagraph to (return & my createTabs(thisSlashCount))
							-- recognizing that the rest of our data for this file's path is now on the next line
							set eachParagraph to eachParagraph + 1
							-- recognizing that the length of the new line is different from the last one
							set pathCharsList to characters of paragraph eachParagraph
							-- telling the loop to start checking from the beginning of the new line (skipping the number of slashes we've added)
							set cLoop to thisSlashCount
						else
							-- just go to the next character
							set cLoop to cLoop + 1
							
						end if
					end repeat
					
					-- we've finished with this file's path, so move on to the next one. We add only 1 because our repeat loop (immediately above) kept track of where we were.
					set eachParagraph to eachParagraph + 1
					
					-- else: if this folder is the same as the last one, i.e. we're displaying an item in the same folder, so just delete the path up to that point and tab out the file name
				else
					
					-- doing this ahead of time to prevent data corruption
					set lastFolder to thisFolder
					
					-- copy the path to the file
					set pathRemoved to pathCharsList as string
					-- remove everything but the name of the file and the preceding /
					set pathRemoved to characters folderCharCount through (count of pathCharsList) of pathRemoved as string
					-- set the line of text to the result
					set paragraph eachParagraph to pathRemoved
					-- replace the leading / with the appropriate number of tabs
					set the first character of paragraph eachParagraph to (my createTabs(slashCount))
					-- go to the next file's path
					set eachParagraph to eachParagraph + 1
					
				end if
				
				-- else: if the line has no / marks, just go to the next line
			else
				
				set eachParagraph to eachParagraph + 1
				
			end if
			
		end repeat
		
	end tell
end tell

-- create a string of tabs, depending on the input number
on createTabs(tabCount)
	set theResult to ""
	repeat with aLoop from 1 to tabCount
		set theResult to theResult & tab
	end repeat
end createTabs

-- where in the list is the last instance of a certain item?
on listPosition(thisItem, thisList, lastInstance)
	set howMany to "0"
	repeat with bLoop from the 1 to (count of thisList)
		if item bLoop of thisList is thisItem then
			set howMany to howMany + 1
			if howMany is equal to lastInstance then return bLoop
		end if
	end repeat
	return 0
end listPosition


[ Reply to This | # ]
copy, paste into plain text editor
Authored by: echo on Jul 26, '06 12:32:32PM

Select all, copy, then paste in a plain text editor window for a text listing of the folder contents.



[ Reply to This | # ]
copy, paste into plain text editor
Authored by: johnsawyercjs on Apr 18, '08 03:34:37PM

As mm2270 says above, pasting a Finder window's contents into a text editor, doesn't display filenames in the same order as they appear in the Finder window.



[ Reply to This | # ]
10.4: Print a folder's contents via Automator
Authored by: sapridyne on Jul 26, '06 02:39:19PM

Create a desktop printer, put it in the sidebar or dock, drag a folder to it, save as PDF.

Sapridyne



[ Reply to This | # ]
10.4: Print a folder's contents via Automator
Authored by: johnsawyercjs on Apr 18, '08 03:35:44PM

As guitarrgirl says above, this method doesn't include subfolders.



[ Reply to This | # ]
10.4: Print a folder's contents via Automator
Authored by: cagg on Jul 26, '06 03:57:04PM

Doesn't work for me. The text file just contains Chinese characters. I even tried opening the file manually with TextEdit and specifying the encoding as Western (Mac OS Roman).



[ Reply to This | # ]
10.4: Print a folder's contents via Automator
Authored by: Anonymous on Jul 27, '06 06:58:49AM
You can also use 'List Folder Contents' AppleScript script which will list a folder's contents returning full paths, including subfolders:

http://homepage.mac.com/jonn8/as/html/misc.html

[ Reply to This | # ]
10.4: Print a folder's contents via Automator
Authored by: Ferdinand on Nov 17, '06 02:14:38AM

I tried and it isn't working. When opening the text edit file saved on the desktop all you can see is some Japanese/Chinese/Korean stuff...



[ Reply to This | # ]
10.4: Print a folder's contents via Automator
Authored by: guitarrgirl on Mar 22, '07 10:52:04AM

Why hasn't Mac integrated this feature in the new OS? It used to be a standard feature.


:)
m



[ Reply to This | # ]
10.4: Print a folder's contents via Automator
Authored by: agou on Apr 14, '08 12:55:43AM

You can also use the Terminal: ls *.mp4 > myvideos.txt



[ Reply to This | # ]
Wonderful!
Authored by: roystonlodge on Jul 04, '09 03:09:54PM

Thanks for this tip!

Not only did it help me with a particular folder I needed the contents of, but it also served as a nice little intro to how automator works. I've had my Mac Mini for several years now, but I've never cracked open Automator before!

Thanks again!



[ Reply to This | # ]