Change the data file path for all graphs in all projects in one folder using Grapher automation

This article contains a sample script for changing the data file path for all plots in all graphs in all files of a specific directory.

 

To run this script:

  1. Copy the script below.
  2. Open Grapher and turn on the Script Manager by clicking View | Display | Script Manager.
  3. Press Ctrl+A to select all of the existing lines in the Script Manager and then press DELETE.
  4. Press Ctrl+V to paste it into the Script Manager.
  5. Click the green arrow Start/Resume icon in the Script Manager. 

'Change the data file path for all files in a specific directory
Sub Main

'Initialize Grapher
Dim Grapher, PlotDoc, Plot As Object
Set Grapher = CreateObject("Grapher.Application")
Grapher.Visible = True

'======================================
'User-defined variables
'======================================
Directory = "c:\temp\"
file$ = Dir$(Directory + "*.grf")
dataDir = Directory+"Data\"
'======================================

'Loop through all GRF files in the directory
While file$ <> ""

    'Open the file in the directory in order
    Set PlotDoc = Grapher.Documents.Open(Directory+file$)

	'Loop through all objects in the file
	For i =1 To PlotDoc.Shapes.Count

	  'If the object is a Graph then count the number of plots in the Graph
	  If PlotDoc.Shapes.Item(i).Type = grfShapeGraph Then

	  	'Loop through all the plots in the graph to change the data file path
	  	For j=1 To PlotDoc.Shapes.Item(i).Plots.Count
	  		'Store the existing file path and file name
			fulldata = PlotDoc.Shapes.Item(i).Plots.Item(j).worksheet
			path = Left(fulldata, Len(fulldata)-(Len(fulldata)-InStrRev(fulldata,"\")))
			dataname = Right(fulldata, Len(fulldata)-(Len(path)))
			'Change the file path
			PlotDoc.Shapes.Item(i).Plots.Item(j).worksheet = dataDir+dataname
	  	Next j

	  End If

	Next i

	'Save GRF as a new name
	filename = Left(file$, Len(file$)-(Len(file$)-InStrRev(file$,".")+1))
	outfile = filename+"_newdatapath.grf"
	Debug.Print outfile
	PlotDoc.SaveAs(Directory+outfile)
	file$ = Dir$()
Wend

End Sub

 

Updated August 13, 2019

Was this article helpful?
1 out of 1 found this helpful

Comments

0 comments

Please sign in to leave a comment.