Can't get it

Discussion in 'Mac Programming' started by ajaxodessa, Mar 2, 2011.

  1. ajaxodessa macrumors newbie

    ajaxodessa

    Joined:
    Feb 27, 2011
    Location:
    Odessa, UA
    #1
    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
     
  2. ajaxodessa thread starter macrumors newbie

    ajaxodessa

    Joined:
    Feb 27, 2011
    Location:
    Odessa, UA
    #3
    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.
     
  3. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #4
    I think what danwilliams was implying was: Check that each a_doc is a file that exists before you open it.

    Another link that might be of assistance: http://macscripter.net/viewtopic.php?id=11388

    B
     
  4. McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #5
    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
    
     
  5. ajaxodessa thread starter macrumors newbie

    ajaxodessa

    Joined:
    Feb 27, 2011
    Location:
    Odessa, UA
    #6
    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
     
  6. McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #7
    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
    
     
  7. ajaxodessa thread starter macrumors newbie

    ajaxodessa

    Joined:
    Feb 27, 2011
    Location:
    Odessa, UA
    #8
    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:
     
  8. McGordon macrumors member

    Joined:
    Dec 28, 2010
    Location:
    Scotland
    #9
    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
    
    
     

Share This Page