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

ajaxodessa

macrumors newbie
Original poster
Feb 27, 2011
22
2
Odessa, UA
I'm trying to automatize opening of certain documents from a folder. Here is the code below which does the job however if there is a sub-folder in a folder it opens it too.
If there is a way to "filter out" folder from the list of files I'm getting in the second line of code?

Code:
set doc_path to (path to documents folder) & "Server" as string
set list_of_documents to list folder doc_path without invisibles

tell application "TextEdit"
	
	repeat with a_file in list_of_documents
		set a_doc to doc_path & ":" & a_file
		open a_doc
	end repeat

	set miniaturized of window 1 to true
	
end tell
 
Well that's a bit different. I know that files are there but folder as well. And when list is built I make without invisible files but also need to throw away folders from the list.
 
Try this:
Code:
set doc_path to (path to documents folder) & "Server" as string
set list_of_documents to (list folder doc_path without invisibles)
set list_of_files_no_folders to {}

tell application "Finder"
   repeat with a_file in list_of_documents
      set a_doc to doc_path & ":" & a_file
      set a_file to alias a_doc
      if (class of a_file) is not folder then
         copy a_file to end of list_of_files_no_folders
      end if
   end repeat
end tell

tell application "TextEdit"
   repeat with a_file in list_of_files_no_folders
      open a_file
   end repeat
   
   set miniaturized of window 1 to true
end tell
 
McGordon, thank you! But there is another problem — open always run within Finder's "end tell" scope. How this could be omitted?
Code:
...
	open document file "_SAMPLE!!!" of folder "documents" of folder "Serve" of folder "Documents" of folder "ajax" of folder "Users" of startup disk
	open document file "_SAMPLE!!! 2" of folder "documents" of folder "Serve" of folder "Documents" of folder "ajax" of folder "Users" of startup disk
end tell
tell application "TextEdit"
	set miniaturized of window 1 to true
end tell
 
McGordon, thank you! But there is another problem — open always run within Finder's "end tell" scope. How this could be omitted?
Code:
...
	open document file "_SAMPLE!!!" of folder "documents" of folder "Serve" of folder "Documents" of folder "ajax" of folder "Users" of startup disk
	open document file "_SAMPLE!!! 2" of folder "documents" of folder "Serve" of folder "Documents" of folder "ajax" of folder "Users" of startup disk
end tell
tell application "TextEdit"
	set miniaturized of window 1 to true
end tell

I noticed that, but didn't know if it would matter to you. It depends on whether the document types you're opening are set to open in TextEdit. I'm not sure why it does this, but there's a way around it using shell commands instead.

This script no longer creates a list_of_files_no_folders, but you could put it back that way if you prefer.

Even though its within the tell Finder scope, its using a shell command to make TextEdit open the file. It no longer relies on TextEdit being the default application for the file type.
e.g. you could open .html files with TextEdit which would normally open in your default browser if opened in the Finder.

Code:
set doc_path to (path to documents folder) & "Server" as string
set list_of_documents to (list folder doc_path without invisibles)
set openTextEdit to "open -b com.apple.TextEdit " -- note the space after TextEdit

tell application "Finder"
   repeat with a_file in list_of_documents
      set a_doc to doc_path & ":" & a_file
      set a_file to alias a_doc
      if (class of a_file) is not folder then
         set posixFile to quoted form of POSIX path of a_doc
         set theCommand to openTextEdit & posixFile
         tell current application to do shell script theCommand
      end if
   end repeat
end tell

tell application "TextEdit"
   activate
   set miniaturized of window 1 to true
end tell
 
McGordon, thank you mate! That worked.

But still confused that there is no elegant way to sort out folder when say invisibles can be filtered easily :confused:
 
I don't know about elegant, but you can do it like this:
Code:
   tell application "System Events" to get name of every disk item of doc_path_alias whose class is not folder

In fact, you probably should as "list folder" is deprecated now. The only property you could do "without" was invisibles anyway.
http://developer.apple.com/library/...oc/uid/TP40000983-CH216-DontLinkElementID_739

Now the "if" statement can be removed.

Code:
set doc_path to (path to documents folder) & "Server" as string
set doc_path_alias to alias doc_path
set openTextEdit to "open -b com.apple.TextEdit "

tell application "System Events"
   set list_of_documents to POSIX path of every disk item of doc_path_alias whose class is not folder
end tell

repeat with a_file in list_of_documents
   set quotedFile to quoted form of a_file
   set theCommand to openTextEdit & quotedFile
   do shell script theCommand
end repeat

tell application "TextEdit"
   activate
   set miniaturized of window 1 to true
end tell
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.