Editing a variable that is a string/text?

set pathToScpt to path to me as text
set pathToScptPOSIX to POSIX file pathToScpt as string
-- i don't know where to go from here

So i was trying to have an applescript app do a shell script on other apps i put inside of it, but i want to edit the path to include backslashes as the application has spaces in it, and i want to keep spaces in it. I have also tried the trim_line subroutine but it didn’t work


set pathToScpt to path to me as text
set pathToScptPOSIX to POSIX file pathToScpt as string
display dialog pathToScptPOSIX
set shellScriptString to trim_line(pathToScptPOSIX, "(copy+paste of end part of result of display dialog pathToScpotPOSIX i wanted to get rid of)", 2)
display dialog pathToScptPOSIX

-- pathToScptPOSIX after being trimmed ends up being the same as pathToScptPOSIX before begin 
on trim_line(this_text, trim_chars, trim_indicator)
	set x to the length of the trim_chars
	if the trim_indicator is in {0, 2} then
		repeat while this_text begins with the trim_chars
			try
				set this_text to characters (x + 1) thru -1 of this_text as string
			on error
				return ""
			end try
		end repeat
	end if
	if the trim_indicator is in {1, 2} then
		repeat while this_text ends with the trim_chars
			try
				set this_text to characters 1 thru -(x + 1) of this_text as string
			on error
				return ""
			end try
		end repeat
	end if
	return this_text
end trim_line

Hi,

POSIX file as text is the other way around, it coerces a POSIX path to HFS path.
You need POSIX path

Assuming the path to your app is HD:Users:myUser:Desktop:myApp.app, you get the HFS path with


set pathToScpt to path to me as text -- HD:Users:myUser:Desktop:myApp.app

and the POSIX path with


set pathToScptPOSIX to POSIX path of pathToScpt -- /Users/myUser/Desktop/myApp.app

to use the path in a shell environment just put quoted form of in front of the variable.
The quotation does the same as adding backslashes


set quotedPathToScptPOSIX to quoted form of pathToScptPOSIX -- '/Users/myUser/Desktop/myApp.app'

okay thank you, but I’m just curious if there is any way to edit a string a in a little more detail, instead of just putting quotes on it, might prove to be useful at some point

also, curiously, when i put a couple of display dialogs above it that didn’t use any of the same variables, terminal was no longer able to find the directories, but after i Cmd+X, Cmd+V so that it was above the display dialogs it started working again, i have no idea why