Help trying to delete last string from a file

Hi everyone I know the internet’s full of scripts to find and replace text but I haven’t come across something that actually helps me with this.

I’m trying to write a small applescript to help install a preset effect into a motion graphics app. (After Effects) I have to add an entry to a file called presetEffects.xml.

I’ve got the entry code on a separate file. The issue is I can’t seem to find a way to specify where I want my entry to be written. I thought I could delete the final xml tag and add my entry uising eof with another ending tag but I can’t delete the last tag. I don’t know how.

What I need is to remove that last 10 characters of theFileReference (More specifically the string “”) but there are two occurrences of this string. The first one should not be touched. I’m using this:


tell application "Finder"
	
	set presetEffects to "/Volumes/Inertial Bounce/PresetEffects.xml"
	set entryText to "/Volumes/Inertial Bounce/Entry.txt"
	set theFileReference to open for access presetEffects with write permission
	set theEntryReference to open for access entryText with write permission
	set entryContents to read theEntryReference
	write entryContents to theFileReference starting at eof
	close access theFileReference
	close access theEntryReference
	read presetEffects
	
end tell

I looked into parsing xml and adding entries but it seemed much more complex.

Also, the reason I’m going to all this trouble is so that other people with no programming/scripting experience can install the effect without having to dive into the package contents and manually adding xml entries as this can be cumbersome, scary and dangerous (the PresetEffects.xml file handles all the effects used within the app. One unclosed tag and the app crashes) for someone who doesn’t understand code.

Any and all help will be greatly appreciated.
Thank you for your time

I finally found an answer. (Really a fiddled and googled and cried and the fiddled some more and now I have something that works. So for anyone that comes looking for something similar.

Oddly enough this doesn’t solve my problem as I’m trying to write to a file that resides inside an app and I’m having permission issues and issues with the path. So I’ll probably post a finished version of the code here and in Code Exchange once I work out the kinks. If you don’t need to write to a protected hidden file then this code works like a charm.

here’s my code. It’s a combination of stuff I found with a few hints of guesswork thrown in. (There’s an explanation at the bottom just in case.

tell application "Finder"
	set presetEffects to "/Volumes/Inertial Bounce/PresetEffects.xml"
	set entryText to "/Volumes/Inertial Bounce/Entry.txt"
	set theFileReference to open for access presetEffects with write permission
	set theEntryReference to open for access entryText with write permission
	set entryContents to read theEntryReference
	set presetContents to read presetEffects
	set theLines to paragraphs of presetContents
	repeat with i from 1 to (count theLines)
		set thisLine to item i of theLines
		if (thisLine begins with "</Effects>") then
			set item i of theLines to entryContents
		end if
	end repeat
	
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to return & linefeed
	set newConfText to theLines as text
	set AppleScript's text item delimiters to astid
	set eof of theFileReference to 0
	write newConfText to theFileReference
	close access theFileReference
	close access theEntryReference
	return newConfText
	
end tell

Here’s an explanation. (Mind you I’m no expert so don’t take my word for it)


tell application "Finder"

-- I'll do my best to explain. Though honestly I'm not sure I understand everything myself.

-- These first lines create variables that get two files and then store their contents in another variable.

-- presetEffects is a variable that stores the path of the file I want to modify
	set presetEffects to "/Volumes/Inertial Bounce/PresetEffects.xml" --PresetEffects.xml is the file I want to modify

-- entryText is a variable that stores the path of the file with the text meant to replace part of PresetEffects.xml
	set entryText to "/Volumes/Inertial Bounce/Entry.txt"

-- makes theFileReference. A variable that acts as a representative of the contents of the file stored in presetEffects
-- and gives me permission to write to it.
	set theFileReference to open for access presetEffects with write permission

-- makes entryReference. A variable that acts as a representative of the contents of the file stored in entryText
-- and gives me permission to write to it.
	set theEntryReference to open for access entryText with write permission

-- entryContents is a variable with the contents of the file at the entryText path
	set entryContents to read theEntryReference

-- presetContents is a variable with the contents of the file at the presetEffects path
	set presetContents to read presetEffects

--This line makes theLines. A variable with all the paragraphs in presetContents
	set theLines to paragraphs of presetContents

-- This makes a variable called i and runs all the code en between the "repeat" and "end repeat" several times
-- Examining every line of text in theLines one by one
	repeat with i from 1 to (count theLines)
-- Makes a variable called this line that holds the line of text that is being examined
		set thisLine to item i of theLines
-- This is a conditional statement that only runs the code after the word "then" if the line being examined starts with
-- the string "</Effects>"
		if (thisLine begins with "</Effects>") then
-- Replaces the current line of text with the contents of entryContents
			set item i of theLines to entryContents
-- tells the computer the conditions statement is over
		end if
-- This is evidently the end of the repeat loop. The computer will do everything from "repeat" to this point over and 
-- over until it has done it for every line of text in theLines. 
	end repeat
	
-- Makes a astid a variable that holds the AppleScripts text item delimiters. These are markers that ApplesScript uses 
-- to mark the end and begining of words and paragraphs. The default value is " ". Meaning it's just a space.
	set astid to AppleScript's text item delimiters

-- I've no clue as to what this line does but it seems to change the default text delimiter to what ever return & 
-- linefeed is
	set AppleScript's text item delimiters to return & linefeed

-- Makes newConfText a variable that stores the content of theLines which is my old file + the modifications as a text 
-- file.
	set newConfText to theLines as text

-- This uses astid to restore AppleScript's text delimiters to it's default. (I think)
	set AppleScript's text item delimiters to astid

-- This erases the file in presetEffects path by setting the eof (end of file to 0 lines). 
	set eof of theFileReference to 0

-- This writes the newConfText content to my old file.
	write newConfText to theFileReference

-- this removes my writing permissions for PresetEffects.xml
	close access theFileReference

-- this removes my writing permissions for Entry.txt
	close access theEntryReference

-- this return newConfText in the results panel of AppleScript Editor so I can tell what the hell I'm doing. 
	return newConfText
	
end tell