11 Replies Latest reply on Jul 16, 2010 4:41 AM by sstanleyau

    Applescript for folder reading

    sudar1983_14 Level 2

      I need a applescript for the below script.

       

      var inputDirectory="D:\\IndFiles";
      var inddFiles=new Array();

       

      inddFiles=Folder(inputDirectory).getFiles();

       

      //Suppress all dialogs
      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;

       

      for(var indCount=0; indCount < inddFiles.length; indCount++)
      {
          if (String(inddFiles[indCount]).match(/\.indd$/i))
          {
              var aDoc=app.open(File(inddFiles[indCount]), true);
        
              //Save and close
              aDoc.close(2036691744);
          }
      }

       

       

       

      Arivu.

        • 1. Re: Applescript for folder reading
          sstanleyau Level 4

          I'd use something like this:

           

          set theFolder to (choose folder) as text

          set fileList to paragraphs of (do shell script "ls " & quoted form of POSIX path of theFolder & " | egrep '*.indd' | tr ':' '/'")

          tell application "Adobe InDesign CS5"

          set user interaction level of script preferences to never interact

          repeat with i from 1 to count of fileList

          open alias (theFolder & item i of fileList)

          -- do stuff

          close document 1 saving yes

          end repeat

          end tell

          1 person found this helpful
          • 2. Re: Applescript for folder reading
            sudar1983_14 Level 2

            Translate is not working here.

             

                 set fileList to paragraphs of (do shell script "ls " & quoted form of POSIX path of theFolder & " | egrep '*.indd' | tr ':' '/'")

             

            Still i am getting colon in the path.

             

            Arivu

            • 3. Re: Applescript for folder reading
              Muppet Mark-QAl63s Level 4

              I would have done something like this…

               

              set theFolder to (choose folder)

              --

              tell application "Finder"

              set fileList to (every file of theFolder whose name extension is "indd") as alias list

              end tell

              --

              tell application "Adobe InDesign CS2"

              if fileList {} then

              set user interaction level of script preferences to never interact

              repeat with i from 1 to count of fileList

              open (item i of fileList)

              -- do stuff

              close document 1 saving yes

              end repeat

              else

              display alert "No 'InDesign' files to open?" giving up after 2

              end if

              end tell

              • 4. Re: Applescript for folder reading
                sudar1983_14 Level 2

                Im getting the below error.

                 

                     Can't make every file of alias "MyPath" whose name extension="indd" into type <<class alst>>

                 

                Please assist me.

                 

                Arivu.

                • 5. Re: Applescript for folder reading
                  sstanleyau Level 4

                  > Still i am getting colon in the path.

                   

                  You want colons in the path. The tr is to change colons in names.

                  • 6. Re: Applescript for folder reading
                    sstanleyau Level 4

                    Yes, you can ask the Finder. The problem is that it's generally very slow (and getting slower), and depending on which version of the OS you're using, there are quirks to using alias list. Apple's advice is to use System Events instead, but that opens another whole can of worms. ls is efficient and robust.

                    • 7. Re: Applescript for folder reading
                      sudar1983_14 Level 2

                      Again one favour for me.

                       

                      Can you give me a script for reading the txt file (Path information stored here), Open each file and execute the process.

                       

                       

                      Arivu..

                      • 8. Re: Applescript for folder reading
                        sstanleyau Level 4

                        Are they HFS paths (using colons) or POSIX paths (using slashes)?

                        • 9. Re: Applescript for folder reading
                          sudar1983_14 Level 2

                          POSIX paths (using slashes).

                          • 10. Re: Applescript for folder reading
                            Muppet Mark-QAl63s Level 4

                            With AppleScript you can use either POSIX or HFS paths. You just need to specify which you are passing. Shell will use POSIX, AppleScript by default likes HFS if you are going to coerce to an alias specifier (which is what most apps expect when using AppleScript). Here is another example that uses spotlight to find your files.

                             

                            set theFolder to quoted form of POSIX path of (choose folder)

                            --

                            set fileList to paragraphs of (do shell script "mdfind -onlyin " & theFolder & " kMDItemDisplayName == '*.indd'")

                            --

                            tell application "Adobe InDesign CS2"

                            set user interaction level of script preferences to never interact

                            repeat with i from 1 to count of fileList

                            open (POSIX file (item i of fileList) as alias)

                            -- do stuff

                            close document 1 saving yes

                            end repeat

                            end tell

                             

                            I like 'mdfind' for its full POSIX paths and speed but it is recursive…

                             

                            If you have problems with 'your code' then you would do better to post it. And also what OS version  you are using?

                            • 11. Re: Applescript for folder reading
                              sstanleyau Level 4

                              In that case it would be something like:

                               

                              set theTextFile to choose file

                              set fileList to paragraphs of (read theTextFile)

                              tell application "Adobe InDesign CS5"

                              set user interaction level of script preferences to never interact

                              repeat with i from 1 to count of fileList

                              try -- to allow for blank paragraphs at end

                              open (POSIX file (item i of fileList) as alias)

                              -- do stuff

                              close document 1 saving yes

                              end try

                              end repeat

                              end tell