I’m unable to fix syntax errors when using folders such as “greg’s folder”
error “86:87: syntax error: Expected “”” but found unknown token. (-2741)" number 1
i’ve created an apple script to run python tool to extract “.rpa” files. the script works well except when selecting games that are in a folder with “ ‘ “.
any ideas to properly to this?
thank you.
part of the code:
-- Function to escape special characters in POSIX paths for safe shell execution
on escapePOSIXPath(originalPath)
set AppleScript's text item delimiters to "'"
set pathParts to text items of originalPath
set AppleScript's text item delimiters to "'\\''"
set escapedPath to pathParts as text
set AppleScript's text item delimiters to "" -- Reset delimiters
-- Wrap in single quotes
return "'" & escapedPath & "'"
end escapePOSIXPath
-- Get the current app's POSIX path
set currentAppPath to POSIX path of (path to me)
-- Construct the expected path for rpatool.py
set scriptPath to currentAppPath & "Contents/Resources/Scripts/rpatool.py”
-- Escape the path for safe execution
set scriptPath to escapePOSIXPath(scriptPath)
-- Prompt user to select the game application
set appFile to choose file with prompt "Select the Game app with RPA files" of type {"com.apple.application"}
set appPath to POSIX path of appFile
set gameFolderPath to appPath & "/Contents/Resources/autorun/game"
-- Open Terminal and change directory
set firstCommand to "cd " & quoted form of gameFolderPath
do shell script "osascript -e " & quoted form of ("tell application \"Terminal\" to do script \"" & firstCommand & "\"")
-- Process each .rpa file in Terminal
display dialog "Extraction will start in Terminal and will process " & (count of fileList) & " .rpa files."
repeat with aFile in fileList
set aFile to escapePOSIXPath(aFile)
set extractionCommand to "python3 " & scriptPath & " -x " & aFile
-- Execute in Terminal
try
do shell script "osascript -e " & quoted form of ("tell application \"Terminal\" to do script \"" & extractionCommand & "\" in front window")
delay 0.5
do shell script "osascript -e " & quoted form of "tell application \"Terminal\" to activate"
on error errMsg
display dialog "Error executing command: " & errMsg
end try
end repeat
The results with error:
tell current application
path to current application
--> alias "Macintosh HD:Users:Greg:Downloads:Ren'Py:RPA Extractor Mac 2.9.scpt"
do shell script "test -e '/Users/Greg/Downloads/Ren'\\''Py/RPA Extractor Mac 2.9.scptContents/Resources/Scripts/rpatool.py' && echo true || echo false"
--> "false"
end tell
tell application "Script Editor"
display dialog "rpatool.py was not found. Please select its location."
--> {button returned:"OK"}
choose file with prompt "Select the rpatool.py script" of type {"public.python-script"}
--> alias "Macintosh HD:Users:Greg:Downloads:Ren'Py:Test Game:rpatool.py"
choose file with prompt "Select the Game app with RPA files" of type {"com.apple.application"}
--> alias "Macintosh HD:Users:Greg:Downloads:Ren'Py:Test Game:Test Game.app:"
end tell
tell current application
do shell script "test -d '/Users/Greg/Downloads/Ren'\\''Py/Test Game/Test Game.app//Contents/Resources/autorun/game' && echo true || echo false"
--> "true"
do shell script "find '/Users/Greg/Downloads/Ren'\\''Py/Test Game/Test Game.app//Contents/Resources/autorun/game' -name '*.rpa' -type f"
--> "/Users/Greg/Downloads/Ren'Py/Test Game/Test Game.app//Contents/Resources/autorun/game/code.rpa
/Users/Greg/Downloads/Ren'Py/Test Game/Test Game.app//Contents/Resources/autorun/game/fonts.rpa
/Users/Greg/Downloads/Ren'Py/Test Game/Test Game.app//Contents/Resources/autorun/game/sounds.rpa"
do shell script "osascript -e 'tell application \"Terminal\" to do script \"cd '\\''/Users/Greg/Downloads/Ren'\\''\\'\\'''\\''Py/Test Game/Test Game.app//Contents/Resources/autorun/game'\\''\"'"
--> error "73:74: syntax error: Expected “\"” but found unknown token. (-2741)" number 1
Result:
error "73:74: syntax error: Expected “\"” but found unknown token. (-2741)" number 1
You are already using quoted form of
in your script which is the most advanced way to escape paths.
Even in a nested environment AppleScript looks always for a working syntax.
-- Get the current app's POSIX path
set currentAppPath to POSIX path of (path to me)
-- Construct the expected path for rpatool.py
set scriptPath to currentAppPath & "Contents/Resources/Scripts/rpatool.py"
-- Prompt user to select the game application
set appFile to choose file with prompt "Select the Game app with RPA files" of type {"com.apple.application"}
set appPath to POSIX path of appFile
set gameFolderPath to appPath & "/Contents/Resources/autorun/game"
-- Open Terminal and change directory
set firstCommand to "cd " & quoted form of gameFolderPath
do shell script "osascript -e " & quoted form of ("tell application \"Terminal\" to do script \"" & firstCommand & "\"")
-- Process each .rpa file in Terminal
display dialog "Extraction will start in Terminal and will process " & (count of fileList) & " .rpa files."
repeat with aFile in fileList
set extractionCommand to "python3 " & quoted form of scriptPath & " -x " & quoted form of aFile
-- Execute in Terminal
try
do shell script "osascript -e " & quoted form of ("tell application \"Terminal\" to do script \"" & extractionCommand & "\" in front window")
delay 0.5
do shell script "osascript -e " & quoted form of "tell application \"Terminal\" to activate"
on error errMsg
display dialog "Error executing command: " & errMsg
end try
end repeat
even after adding quoted form of to 1 line, i still get syntax error:
tell current application
path to current application
--> alias "Macintosh HD:Users:Greg:Downloads:Ren'Py:RPA Extractor Mac 2.9 copy.scpt"
do shell script "test -e '/Users/Greg/Downloads/Ren'\\''Py/RPA Extractor Mac 2.9 copy.scptContents/Resources/Scripts/rpatool.py' && echo true || echo false"
--> "false"
end tell
tell application "Script Editor"
display dialog "rpatool.py was not found. Please select its location."
--> {button returned:"OK"}
choose file with prompt "Select the rpatool.py script" of type {"public.python-script"}
--> alias "Macintosh HD:Users:Greg:Downloads:Ren'Py:Test Game:rpatool.py"
choose file with prompt "Select the Game app with RPA files" of type {"com.apple.application"}
--> alias "Macintosh HD:Users:Greg:Downloads:Ren'Py:Test Game:Test Game.app:"
end tell
tell current application
do shell script "test -d '/Users/Greg/Downloads/Ren'\\''Py/Test Game/Test Game.app//Contents/Resources/autorun/game' && echo true || echo false"
--> "true"
do shell script "find '/Users/Greg/Downloads/Ren'\\''Py/Test Game/Test Game.app//Contents/Resources/autorun/game' -name '*.rpa' -type f"
--> "/Users/Greg/Downloads/Ren'Py/Test Game/Test Game.app//Contents/Resources/autorun/game/code.rpa
/Users/Greg/Downloads/Ren'Py/Test Game/Test Game.app//Contents/Resources/autorun/game/fonts.rpa
/Users/Greg/Downloads/Ren'Py/Test Game/Test Game.app//Contents/Resources/autorun/game/sounds.rpa"
do shell script "osascript -e 'tell application \"Terminal\" to do script \"cd '\\''/Users/Greg/Downloads/Ren'\\''\\'\\'''\\''Py/Test Game/Test Game.app//Contents/Resources/autorun/game'\\''\"'"
--> error "73:74: syntax error: Expected “\"” but found unknown token. (-2741)" number 1
Result:
error "73:74: syntax error: Expected “\"” but found unknown token. (-2741)" number 1
That error typically means that you have unclosed quotes and the compiler is expecting them at this place (because the next word or character isn’t ordinary text).
Given that your target file is in a ‘bundle’, it might be easier to work with the path to resource
command. The code below assumes that your drive’s name is ‘MacHD’; edit as required.
I start with the alias of the ‘.app’. Then, use the path to resource
command to get the alias of the embedded python script rpatool.py
. Finally, I get the quoted form of its posix path.
Note that it reveals the syntax for handling abominations in a folder name– I mean atrocities– I mean apostrophes. It’s '\\'', or 1 apostrophe, 2 backslashes, 2 apostrophes. Hope it works.
-- alias of app bundle
set adu to alias "MacHD:Users:Greg:Downloads:Ren'Py:Test Game:Test Game.app"
-- use `path to resource` to get alias of python script
set rpa to path to resource "rpatool.py" in bundle adu in directory "Scripts"
--> set adu to alias "MacHD:Users:Greg:Downloads:Ren'Py:Test Game:Test Game.app:Contents:Resources:Scripts:rpatool.py"
-- get quoted posix path
set qprpa to quoted form of POSIX path of res
--> "'/Users/Greg/Downloads/Ren'\\''Py/Test Game/Test Game.app/Contents/Resources/Scripts/rpatool.py'"
thank you guys for your feedback.
the script i’m working on will not have set directory for the rpatool.py as well as the game.
I will export the script as an application and then run the script app, which will contain the rpatool.py in the script directory. The script app should be able to find the tool as long as im set the “path to me” for the script app and then set the location of the tool. I was able to implement the code for “path to resource” for this case.
I will also use the script app on other computers that I have, which will be used on various different games to extract .rpa files within them.
My main problem is handling folders with special characters and i’m not sure how to fix them.
The code where the error occurs seems not to be part of the shown code in the question
sorry for misleading you. The error would not trigger at the code you and Mockman helped correct after fixing it, however, the error would occur again later on during terminal execution lines due to too many “”, ‘\ and so on. I ended up rewriting the terminal execution code, which helped fix the syntax issue.
Instead of having
do shell script "osascript -e " & quoted form of ("tell application \"Terminal\" to do script \"" & firstCommand & "\”")
and
do shell script "osascript -e " & quoted form of ("tell application \"Terminal\" to do script \"" & extractionCommand & "\" in front window”)
I changed it to
tell application "Terminal"
activate
do script "cd " & quoted form of gameFolderPath
end tell
and
tell application "Terminal"
do script "python3 " & quoted form of scriptPath & " -x " & quoted form of aFile in front window
end tell
This helped with special characters.