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)
View all comments
Show parent comment
1
u/lightbox_glow Feb 26 '26
Thanks for the response!
Sorry. Not a batch. Just trying to do one (manually opened) file at a time (which I will then manually save at some point). What I meant by multiple documents is that the operation fails if (manually) triggered on any document other than a first-and-only document open in a fresh run of BBEdit.
Basically, I just want to be able to manually run this script any time I want on the currently active document, but the operation fails unless the document being processed is the first opened file on a fresh launch of BBEdit. It delivers the expected results if thosee conditions are met, but gives an error (fixed by closing and relaunching BBEdit) on any subsequent file.