Please help me complete this script....

I have the following Applescript which I created by “recording” in BBEdit with one textfile open:

tell application “BBEdit”
activate
open find window
replace “§§§” using “\r” searching in text 1 of text document “BC_102.tab” options {search mode:grep, starting at top:true}
replace “\x{0B}” using “” searching in text 1 of text document “BC_102.tab” options {search mode:grep, starting at top:true}
save text document 1
end tell

**** This script does SOME OF WHAT I NEED.
It does 2 search-and-replace actions on a single named file. ****

Could some kind person please edit or rewrite this script so that:
(1) it gives a dialog allowing me to select a folder on my Mac (OSX 10.13.3)
(2) it goes through every file it finds in that folder, and performs the above 2 search-and-replace actions
(3) it removes the extension at the end of each filename (if there is one)
(4) It beeps and shows a dialog when all files have been processed.

Thanking you in anticipation. Sincerely, Philip

Model: MacBook Air
AppleScript: Script Editor 2.10
Browser: Firefox 65.0
Operating System: macOS 10.13

This will do what you want to all .txt files in a chosen folder, saving the modified versions by appending “-2” to the name. It assumes UTF-8 encoding.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

-- classes, constants, and enums used
property NSRegularExpressionSearch : a reference to 1024
property NSUTF8StringEncoding : a reference to 4
property NSDirectoryEnumerationSkipsHiddenFiles : a reference to 4

set theFolder to POSIX path of (choose folder)
set theURL to current application's |NSURL|'s fileURLWithPath:theFolder
set fileManager to current application's NSFileManager's defaultManager()
set theFiles to fileManager's contentsOfDirectoryAtURL:theURL includingPropertiesForKeys:{} options:NSDirectoryEnumerationSkipsHiddenFiles |error|:(missing value)
repeat with aFile in theFiles
	if aFile's pathExtension() as text = "txt" then
		set theString to (current application's NSString's stringWithContentsOfURL:aFile encoding:NSUTF8StringEncoding |error|:(missing value))
		if theString is not missing value then
			set theString to (theString's stringByReplacingOccurrencesOfString:"§§§" withString:"\\r" options:NSRegularExpressionSearch range:{0, theString's |length|()})
			set theString to (theString's stringByReplacingOccurrencesOfString:"\\x{0B}" withString:"" options:NSRegularExpressionSearch range:{0, theString's |length|()})
			set newPath to (aFile's |path|()'s stringByDeletingPathExtension()'s stringByAppendingString:"-2.txt")
			(theString's writeToFile:newPath atomically:true encoding:NSUTF8StringEncoding |error|:(missing value))
		end if
	end if
end repeat

Thanks for this Shane.
I copied-and-pasted the script into “Script Editor v2.10” (MacOS X 10.13.3) and it worked!!
But I really need a version that removes any extension (not just .txt) from all filenames.
Your advice on changing the script to do that would be much appreciated.
Many thanks once again.
Philip Caplan

Also, 2 small problems:

(1) a new file with the extension is created, but I wanted the old file to also be deleted.
(2) the §§§ is being replaced in each file by the letter “r” but it should be replaced by a “return” (in BBEdit’s Find: typing “\r” represents “return”).

Thank you. Philip Caplan

Correction: should have said “a new file WITHOUT the extension is being created”
:rolleyes:

When you say removes any extension, what do you mean?

(1) a new file with the extension is created, but I wanted the old file to also be deleted.

To overwrite the old file, change the writeToFile: line like this:

           (theString's writeToURL:aFile atomically:true encoding:NSUTF8StringEncoding |error|:(missing value))

But you want to make sure it’s working exactly as you want first, because there’s no undo.

Yes, remove one of the back-slashes.

Hi both.

Here’s a version using BBEdit, with a commented-out section at the end to leave just the edited files with their original names and without the extensions.

Grep isn’t necessary for replacing “§§§”. BBEdit treats backslash-r as the default line ending set in its user preferences, which isn’t necessarily a return. That’s why I’ve used BBEdit itself here.

set editSuffix to " (edited)"

set theFolder to (choose folder)

tell application "Finder" to set theFiles to (theFolder's files) as alias list

tell application "BBEdit"
	activate
	set theDocuments to (open theFiles)
	repeat with thisDocument in theDocuments
		tell thisDocument
			replace "§§§" using "\\r" options {starting at top:true}
			replace "\\x{0B}" using "" options {search mode:grep, starting at top:true}
			set oldPath to (its file) as text
		end tell
		set newPath to (replace "(\\.[^.:]*+)?$" using editSuffix searchingString oldPath options {search mode:grep})
		save thisDocument to file newPath
	end repeat
end tell

-- If desired, and when you're happy with the rest of the script, uncomment the code below to have the original files deleted and the suffix removed from the new versions' names.
(*
set suffixCut to -((count editSuffix) + 1)
tell application "Finder"
	delete theFiles
	set editedFiles to theFolder's files
	repeat with thisFile in editedFiles
		set editName to thisFile's name
		set thisFile's name to text 1 thru suffixCut of editName
	end repeat
end tell
*)

Sorry Stanley, it’s not working now!!!

Below is what I put into Script Editor, running which gives an error
saying: "Syntax Error: Expected “with” but found “if”
(and the penultimate “end if” is highlighted).

Also: Regarding my request for “any file extension” what I meant is:
I changed “.txt” to “.tab” because at present all the files end .tab – but it would be future-proofed if the script worked with any file extension (such as .pdf) and not only “.tab” [obviously I will only use it on a folder containing files I want to have converted, so I wouldn’t use it on a PDF!!!]

If this is tricky, don’t worry about it, I’ll just change the script before running it if I need to work with a different extension!!!

Anyway, here’s the current script:

use AppleScript version “2.4” – Yosemite (10.10) or later
use framework “Foundation”
use scripting additions
– classes, constants, and enums used
property NSRegularExpressionSearch : a reference to 1024
property NSUTF8StringEncoding : a reference to 4
property NSDirectoryEnumerationSkipsHiddenFiles : a reference to 4

set theFolder to POSIX path of (choose folder)
set theURL to current application’s |NSURL|'s fileURLWithPath:theFolder
set fileManager to current application’s NSFileManager’s defaultManager()
set theFiles to fileManager’s contentsOfDirectoryAtURL:theURL includingPropertiesForKeys:{} options:NSDirectoryEnumerationSkipsHiddenFiles |error|:(missing value)
repeat with aFile in theFiles
if aFile’s pathExtension() as text = “tab” then
set theString to (current application’s NSString’s stringWithContentsOfURL:aFile encoding:NSUTF8StringEncoding |error|:(missing value))
if theString is not missing value then
set theString to (theString’s stringByReplacingOccurrencesOfString:“§§§” withString:“\r” options:NSRegularExpressionSearch range:{0, theString’s |length|()})
set theString to (theString’s stringByReplacingOccurrencesOfString:“\x{0B}” withString:“” options:NSRegularExpressionSearch range:{0, theString’s |length|()})
set newPath to (aFile’s |path|()'s stringByDeletingPathExtension()'s stringByAppendingString:“”)
(theString’s writeToURL:aFile atomically:true encoding:NSUTF8StringEncoding |error|:(missing value)) end if
end if
end repeat

Model: MacBook Air
AppleScript: Script Editor 2.10
Browser: Firefox 65.0
Operating System: macOS 10.13

Nigel. Thank you for your BBEdit script. It works OK, including the “delete original files” part.

But… (isn’t there always a But???)

It leaves BBEdit at front with all the converted files listed in the “edited files” list. Is it possible to have BBEdit close/remove every file, or whatever is needed so this doesn’t happen??

And is it possible to end with an onscreen dialog saying “All files converted”?? So I don’t have to keep watching Script Editor until it stops saying “Running”…

Many thanks, Philip

Here’s an updated version:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
-- classes, constants, and enums used
property NSRegularExpressionSearch : a reference to 1024
property NSUTF8StringEncoding : a reference to 4
property NSDirectoryEnumerationSkipsHiddenFiles : a reference to 4

####### Warning!
####### This script overwrites existing files
####### Don't even test it unless you're sure you have backup copies of all the files in the chosen folder

set theFolder to POSIX path of (choose folder)
set theURL to current application's |NSURL|'s fileURLWithPath:theFolder
set fileManager to current application's NSFileManager's defaultManager()
set theFiles to fileManager's contentsOfDirectoryAtURL:theURL includingPropertiesForKeys:{} options:NSDirectoryEnumerationSkipsHiddenFiles |error|:(missing value)
repeat with aFile in theFiles
	set theString to (current application's NSString's stringWithContentsOfURL:aFile encoding:NSUTF8StringEncoding |error|:(missing value))
	if theString is not missing value then
		set theString to (theString's stringByReplacingOccurrencesOfString:"§§§" withString:"\r")
		set theString to (theString's stringByReplacingOccurrencesOfString:"\\x{0B}" withString:"" options:NSRegularExpressionSearch range:{0, theString's |length|()})
		(theString's writeToURL:aFile atomically:true encoding:NSUTF8StringEncoding |error|:(missing value))
	end if
end repeat