I’ll explain the current steps I take, and I’d like AppleScript to do the same thing,
I open a file called project.prj with TextEdit, and choose replace: “3117” with “3118”, then hit save.
How would I be able to do the same in an AppleScript?
Depending on the size and text encoding of the *.prj files, I would not use TextEdit at all, but read, manipulate and save the files directly with AppleScript. Maybe you can tell us a little bit more about the text encoding (UTF-8, Mac Roman, etc.), size and location of the *.prj files?
Hi, the file is Mac Roman encoding, the folder path is stored in the property called “destination”. I would like to indeed read, manipulate and save the files directly with AppleScript if this is possible?
This is not a ready-for-use script, but might point you in the right direction:
try
set chosenfile to (choose file with prompt "Please choose a PRJ file:" without multiple selections allowed and invisibles)
set filecont to read chosenfile
set filecont to my searchnreplace("3117", "3118", filecont)
set newfile to (choose file name with prompt "Where do you want to save the new file?")
my writetofile(filecont, newfile)
on error errmsg number errnum
if errnum is not equal to -128 then
tell me
activate
display dialog "Sorry, an error occured:" & return & return & errmsg & return & "(" & errnum & ")" buttons {"OK"} default button 1 with icon stop
end tell
end if
end try
-- I am a very old search & replace function...
on searchnreplace(searchstr, replacestr, txt)
considering case, diacriticals and punctuation
if txt contains searchstr then
set olddelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {searchstr}
set txtitems to text items of txt
set AppleScript's text item delimiters to {replacestr}
set txt to txtitems as Unicode text
set AppleScript's text item delimiters to olddelims
end if
end considering
return txt
end searchnreplace
-- I am writing given content to a given file using UTF-8 text encoding
on writetofile(cont, filepath)
try
set openfile to open for access filepath with write permission
set eof of openfile to 0
write cont to openfile
close access openfile
return true
on error
try
close access openfile
end try
return false
end try
end writetofile
set destination to the text returned of (display dialog "What is the project name" default answer "")
tell application "Finder"
activate
duplicate folder "SADiE Template" of disk "Thecus Audio"
set name of folder "SADiE Template copy" of disk "Thecus Audio" to destination
set name of document file "SADiE Template.bkC" of folder destination of disk "Thecus Audio" to destination & ".bkC"
set name of document file "SADiE Template.bkP" of folder destination of disk "Thecus Audio" to destination & ".bkP"
set name of document file "SADiE Template.cls" of folder destination of disk "Thecus Audio" to destination & ".cls"
set name of document file "SADiE Template.msu" of folder destination of disk "Thecus Audio" to destination & ".msu"
set name of document file "SADiE Template.prj" of folder destination of disk "Thecus Audio" to destination & ".prj"
end tell
set chosenfile to "Thecus Audio:" & destination & ":" & destination & ".pjr"
set newFile to "Thecus Audio:" & destination & ":" & destination & ".pjr"
try
set filecont to read chosenfile
set filecont to my searchnreplace("3117 Doug Major", destination, filecont)
my writetofile(filecont, newFile)
on error errmsg number errnum
if errnum is not equal to -128 then
tell me
activate
display dialog "Sorry, an error occured:" & return & return & errmsg & return & "(" & errnum & ")" buttons {"OK"} default button 1 with icon stop
end tell
end if
end try
-- I am a very old search & replace function...
on searchnreplace(searchstr, replacestr, txt)
considering case, diacriticals and punctuation
if txt contains searchstr then
set olddelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {searchstr}
set txtitems to text items of txt
set AppleScript's text item delimiters to {replacestr}
set txt to txtitems as Unicode text
set AppleScript's text item delimiters to olddelims
end if
end considering
return txt
end searchnreplace
-- I am writing given content to a given file using UTF-8 text encoding
on writetofile(cont, filepath)
try
set openfile to open for access filepath with write permission
set eof of openfile to 0
write cont to openfile
close access openfile
return true
on error
try
close access openfile
end try
return false
end try
end writetofile
Thanks again, that seemed to run it through without any error, however, it did not replace my string. Here is the original file where I’m trying to replace the string.
If you just type “3117 Doug Major” into the Script Editor, then it does not work. You need to open the original PRJ file in a text editor (I am using TextWrangler), select the part you want to replace and copy and paste it into the AppleScript. This way I was able to run it successfully.
That seemed to work, thanks again. The only thing is that the encoding of the final file is still not good, can you please check what the encoding is of my original script and adapt the code so it will write the same encoding?
Both TextWrangler and SubEthaEdit display «Western (Mac OS Roman)» as the text encoding of your file. But I guess the problem are the invisible characters in your file. I think you also need to include them in your replace string, if you don’t want to break the file. By default, AppleScript writes files as Mac OS Roman, so I don’t think we are running into problems with the «writetofile» handler.
When I open the «SADiE Template.prj» in a text editor and copy/paste the «3117 Doug Major» as the search string and another part of the file (e.g. «capt 2444») as the replacement string into the AppleScript in Script Editor, then it works here on my Mac.
I don’t understand what you mean with «bad characters»
The problem is that $ signs get converted to ^, ` to @, . (this is not correct as I can’t paste the original sign in here). So them when the file gets saved, all those invisible signs are wrong, that I think is the main problem, no?
I have played around with the prj file in Python and the problem is, that it is not a text file at all, but a binary file, so it must be processed in a special way. Maybe you want to try the tools from Satimage, if you want to stick with AppleScript for this task.
That makes sense now, didn’t know that, I tried playing with Satimage but when I use:
set openfile to (read binary alias filepath as integer), I get the error: Expected ", " but found class name.
I think I’ll have to pass this as “too hard for me” =)