Swap data buried in a file

Hi,
I’m trying to get a mac path out of a Lightwave scene file (just a plane text document with a .lws on the end) and exchange it with a unix path. The problem is that it is never in the same location. Its always near the bottom with the same information around it. But never at a specific line number. and the file path itself is never the same.

This is what the surrounding data looks like:

RenderMode 2
RayTraceEffects 0
DepthBufferAA 0
RayRecursionLimit 16
DataOverlayLabel
OutputFilenameFormat 5
SaveRGB 1
SaveRGBImagesPrefix STUDIO_SERVER:FARMTest:LW_Renders:Box:box ← This is what I’m trying to change
RGBImageSaver LW_PNG32(.png)
SaveAlpha 0

Any ideas on how to accomplish this?

Thanks

Phil

Two ideas:

it might be possible to test for a minimum number of colons per line to find the path (or will there be root level paths?)

or (if the paths we search for lead to existing files) … maybe you could validate every line containg colons (by trying to ‘alias’ it) - if it is a valid path then …

No unfortunately there wont be anything in the folder yet, it is the path to where the images (in this case box.png) will be saved when rendered. How ever It should be possible to check for the minimum number of colons, at a minimum there will be “STUDIO_SERVER: ’ content dir ’ :LW_Renders:”

But how would i go about doing this, I’m still very new to applescripting.

Thanks

Phil

Hi Phil,

asuming the first word of your path lines does not belong to the paths - try something like this:

set txt to "RenderMode 2
RayTraceEffects 0
DepthBufferAA 0
RayRecursionLimit 16
DataOverlayLabel  
OutputFilenameFormat 5
SaveRGB 1
SaveRGBImagesPrefix STUDIO_SERVER:FARMTest:LW_Renders:Box:box
RGBImageSaver LW_PNG32(.png)
SaveAlpha 0"

set convertedTxt to my convertPathsWithMoreColonsThan(2, txt) -- call the handler like this to convert your text


on convertPathsWithMoreColonsThan(nrOfColons, thetext)
	
	set {tid, text item delimiters} to {text item delimiters, ":"}
	set theResult to ""
	repeat with theLine in paragraphs of thetext
		set ti to text items of theLine -- get all colon separated parts of the line
		if ((count of ti) <= nrOfColons) then 
			set theResult to theResult & theLine & return
		else
			set separatedPath to my separateFirstWord(theLine)
			if ((count of separatedPath) > 0) then
				set theResult to theResult & (item 1 of separatedPath) & space & POSIX path of (item 2 of separatedPath) & return
			else
				set theResult to theResult & theLine & return
			end if
		end if
		
	end repeat
	set text item delimiters to tid
	return theResult
end convertPathsWithMoreColonsThan

on separateFirstWord(txt)
	set {tid, text item delimiters} to {text item delimiters, space}
	set parts to text items of txt
	if ((count of parts) is 1) then -- if there is no space in the string then return an empty list
		set theResult to {}
	else
		set theResult to {(item 1 of parts), ((items 2 thru -1 of parts) as text)} -- return a list containing the first word and the path
	end if
	set text item delimiters to tid
	return theResult
end separateFirstWord

the result looks like this - I hope that’s what you need … :wink:

RenderMode 2
RayTraceEffects 0
DepthBufferAA 0
RayRecursionLimit 16
DataOverlayLabel
OutputFilenameFormat 5
SaveRGB 1
SaveRGBImagesPrefix /STUDIO_SERVER/FARMTest/LW_Renders/Box/box
RGBImageSaver LW_PNG32(.png)
SaveAlpha 0

Hey Dominik

It worked like a charm! Thank you so much for your help!!

Phil