5 Replies Latest reply on Jan 24, 2011 1:22 PM by twintern20

    [JS] I would like to find the date modifed of a link file

    twintern20

      I have a vbs script that updates my links. It checks if the file exists and then the modified date. I need to verify the link file had been updated within a few hours to verify it has today's info instead of yesterday's.

       

      I would like to convert this to use JavaScript but have had difficulties finding the link's modified date.

       

      I don't expect someone to rewrite my script but can someone give an example to find if a file exists and it's modified date?

       

      Terry W.

       

       

       

      Option Explicit

      msgbox "Press OK to continue!"

      'appliation object
      Dim objMyInDesign
      'document ofject
      Dim objMyDocument
      'table object
      Dim objTable
      'cell object
      Dim objCell
      Dim txtNewText
      'link object
      Dim objMyLink
      Dim txtFlag

      ' File from file system object
      Dim objFSO
      Dim objFileExistMessage
      Dim objFile
      Dim TimeDifference
      Dim FileDate

      ' Test for Message Box
      Dim MessageText

      ' myDocument will be the active document
      Dim myDocument

      'Windows Script object
      Dim WScript

       

      Set objFSO = CreateObject("Scripting.FileSystemObject")

      Set objMyInDesign = CreateObject("InDesign.Application.CS3")
        
      If objMyInDesign.Documents.Count = 0 Then
      MsgBox ("Please Open Document and Try again!")
      Else
      Set objMyDocument = objMyInDesign.ActiveDocument

      DoLinksExist()
      'ProcessLinks()
      End If

      Sub DoLinksExist
      For Each objMyLink In objMyDocument.Links
        If Not objFSO.FileExists(objMyLink.FilePath) And

      (TypeName(objMyLink.parent) <> "EPS") Then
         objFileExistMessage = MsgBox("File/Link does not Exist:"

      & vbCrLf _
         & vbCrLf & objMyLink.FilePath & vbCrLf & vbCrLf _
         & "Do you wish to continue with missing files??", _
         vbYesNo + vbCritical,"Stock Missing -- Continue?")
       

         If objFileExistMessage = vbYes then
          FilesAge()
          Exit Sub
         ElseIf objFileExistMessage = vbNo then
          MsgBox ("Script Quiting!!")
          Exit Sub
         End If
        End If
      Next
      FilesAge()
      End Sub

      Sub FilesAge()
      For Each objMyLink In objMyDocument.Links
        If objFSO.FileExists(objMyLink.FilePath) And

      (TypeName(objMyLink.parent) <> "EPS") Then
         Set objFile = objFSO.GetFile(objMyLink.FilePath)

         TimeDifference = abs(DateDiff("h", Now,

      objFile.DateLastModified))
         'If file is older than six hours then present error

      message
         If TimeDifference > 6 Then
          objFileExistMessage = MsgBox("ATTENTION!!!!" _
           & vbCrLf & "OLD!! OLD!! OLD!!" _
           & vbCrLf & vbCrLf _
           & "A Stock is very OLD" & vbCrLf _
           & vbCrLf & objMyLink.FilePath & vbCrLf &

      vbCrLf _
           & "Do you wish to flow old stocks onto

      page???", _
           vbYesNo + vbCritical,"STOCKS OLD --

      Continue?")
          If objFileExistMessage = vbYes then
           ProcessLinks()
           Exit Sub
          ElseIf objFileExistMessage = vbNo then
           MsgBox ("Script Quiting!!")
           Exit Sub
          End If
         End If
        End If
      Next
      ProcessLinks()
      End Sub

      Sub ProcessLinks
      For Each objMyLink In objMyDocument.Links
        If objFSO.FileExists(objMyLink.FilePath) And

      (TypeName(objMyLink.parent) <> "EPS") Then
         objMyLink.Update
        Else
         'objFileExistMessage = MsgBox("The following file/link

      DOES NOT exist" _
          '&  vbCrLf _
          '& vbCrLf & objMyLink.FilePath _
          '& vbCrLf & vbCrLf & "This link will not be

      processed" _
          '& vbCrLf & vbCrLf & "Click on 'OK' and" _
          '& " script will continue processing

      files/links")
        End If
      Next
      End Sub

        • 1. Re: [JS] I would like to find the date modifed of a link file
          twintern20 Level 1

          I corrected the script and I think I found how to find if the link is missing by using the "myLink.status."

           

          So my main concern is the date modified.

          • 2. Re: [JS] I would like to find the date modifed of a link file
            [Jongware]-9BC6tI Level 4

            The Javascript File object indeed does have a "modified" property -- it's a Date object, which ought to make comparisons to the current day and time quite easy.

             

            The Javascript property you should look for is "itemLink"; this is succinctly described in the Help as "A link to a placed file", and is a common property of all placed content (and non-placed content as well, but I'm thinking it would be null in such cases). The itemLink contains all data InDesign sees -- the status, for example, can tell if the file has been changed out of InDesign and let ID show the yellow "Update-me" icon --, but, more important to you, it also contains a property "filePath". This can be a File or a String (dontcha love JS variables chameleon properties), which means you have to test what it is first using "instanceOf". Given the file path, or a direct File reference, all you need to do is check File.exists -- a simple boolean, which does exactly what it says on the can. If it does, you can check the file's "modified" property against the current time.

             

            (Warning: Rambling Mode)

             

            I would not use your construction --

             

            TimeDifference = abs(DateDiff("h", Now,

            objFile.DateLastModified))
               'If file is older than six hours then present error

            message

             

            because it check if the difference is larger than 6 hours, not if the image is 6 hours or more older than the current time. OTOH ... hm. Can your image be 6 hours (or more) newer than InDesign, the moment that you check? I'll need to think about this -- what if the image is newer? Then you'd surely want to update immediately? Methinks that if the new file is less than 6 hours younger than the ID file it won't get updated.

            1 person found this helpful
            • 3. Re: [JS] I would like to find the date modifed of a link file
              [Jongware]-9BC6tI Level 4

              Oh, and all of the above information (with exception of the rambling thoughts) was found with straightforward index keyword searches.

               

              Can I interest you in a Friendlier Help (both to work with, and on the eyes)? Check out http://www.jongware.com/idjshelp.html.

              • 4. Re: [JS] I would like to find the date modifed of a link file
                twintern20 Level 1

                Thank you for the info but I don't understand that much about JavaScript.

                 

                How do I create a "Javascript File object?"

                 

                I found the file by using "app.documents.item(0).links.item(0).filePath" but I haven't been able figure how to find the modified info.

                 

                Terry w.

                • 5. Re: [JS] I would like to find the date modifed of a link file
                  twintern20 Level 1

                  Seems like I may have found the answer of the modified date by using:

                   

                  var file2 = new File(myLink.filePath)
                  alert("file2.name: " + file2.name)
                  alert("file2.name: " + file2.modified)

                   

                  For the date I use:

                  var currentTime = new Date() to get the time for Now and then subtract the file's date. If the difference is more then six hours I wan't to update the file. We link to text files created daily for lists of Dow stocks so must verify we're using today's stock info. I should change the time to one hour instead of six but it's worked for the past eight years.

                   

                  I'm now updating from InDesign CS to CS4 and from FreeHand?? to native Illustrator files for our graphs. I haven't figured out how to update the Illustrator graph's data using a script... well that's a different forum....

                   

                  Terry w.