r/applescript • u/lightbox_glow • Feb 26 '26
Help with AppleScript for BBEdit
I'm trying to write an AppleScript to use BBEdit to straighten "educated"/typographer's quotes and to replace certain punctuation [see below]. Running the script works as expected, but only on the first document after BBEdit opens (i.e., when launching BBEdit by double-clicking a text file; in order to successfully run the script again, I need to close all documents and quit BBEdit). I would like it to work on whatever text document is active/frontmost any time I run the script.
On compile, returns a syntax error of: Expected variable name, class name or property but found application constant or consideration.
When running after first launch, BBEdit returns a scripting error of: BBEdit got an error: text 1 of text document id 2 doesn’t understand the “straighten quotes" message.
I've developed this with the help of Claude and Gemini AIs, but they've been useless in resolving these problems. Suggestions?
tell application "BBEdit"
tell text 1 of front document
straighten quotes
end tell
end tell
on doReplace(searchChar, replaceStr)
tell application "BBEdit"
try
find searchChar options {search mode: literal, wrap around: false, backwards: false, case sensitive: true, grep: false, replace all: true, returning results: false} replacing replaceStr in front document
end try
end tell
end doReplace
\-- Dashes and Ellipsis
doReplace(character id 8212, "—") -- — EM DASH (U+2014)
doReplace(character id 8211, "–") -- – EN DASH (U+2013)
doReplace(character id 8230, "…") -- … HORIZONTAL ELLIPSIS (U+2026)
3
u/Monkey_Launderer Feb 26 '26
This won't compile on my machine, running BBEdit 14 on macOS 12.7.6. That was initially because the
findcommand options list contains parameters that don't exist for the command:grep and replace allgrepis not a valid parameter and also not required because the search mode is set toliteral-grepis the other option.replace allis not a valid parameter in BBEdit at all according to my search of its scripting dictionary.Once these removed it still won't compile because
replacingis not a parameter of thefindcommand either.The handler calls don't make sense to me either as you seem to be passing the same characters for
searchStrandreplaceStrI don't have the bandwidth to rewrite and test this as is, but if you're going to attempt it you should use the
replacecommand, which is used for what you're trying to do. And maybe try using Script Debugger, which is excellent and now free.2
u/copperdomebodha Feb 27 '26
1
u/erodas Feb 26 '26
find and replace of bbedit with RegEx. two one-liners and you're done. applescript is wrong tool.
-1
u/bliprock Feb 26 '26
Claude can kinda do AppleScript. But ya gotta hold its hand. A lot. When you get say certain rules worked out and parts of syntax working tell Claude to make a .MD file and hooks. This will help Claude make a rule set if you like.
Ok so you describe it as a batch process yet see zero batch working. I’m no expert here by the way but explains why it only does one. It’s only gonna do it to the first file.
Also there might be a library or BBEdit reference for how to close and save file. That’ll help make sure it closes.
on my tell application "Finder" set selectedFiles to selection as alias list end tell
tell application "BBEdit" activate repeat with aFile in selectedFiles open aFile end repeat end tell
There’s what Claude spat out for an example of batch processing files you have selected. So not seeing anything similar in your example so only one gets processed. . Try building in small clear steps on top of what ya asking for. You can do by path folder and more.
You won’t usually be able to one shot AppleScript with Claude but gets close. Keep at it you’ll get it meh maybe someone can do one for you too. It’s just batch processing part you seem to be missing. Also add closing file after and this will be a loop in the tell block