Need help simplifying apple script with grep

76 views
Skip to first unread message

Mathias

unread,
Mar 17, 2023, 6:04:16 AM3/17/23
to BBEdit Talk
OK, so I'm working on an apple script to replace Localizable (apple) comments on the /* ... */ format to <!-- and -->

I got it working, but I'm using two commands because I couldn't figure the grep out... can this be simplified with only one "replace" ? 

pointers appreciated, script here:

tell application "BBEdit"

tell front text window

replace "(/\\*[^\\*]*\\*/)" using "<!--\\1-->" options ¬

{search mode:grep, case sensitive:false, starting at top:true

replace "(/\\*|\\*/)" using "" options {search mode:grep, case sensitive:false, starting at top:true

replace "%@" using "%1$s" options {case sensitive:false, starting at top:true

end tell

end tell

Bruce Van Allen

unread,
Mar 17, 2023, 9:08:57 AM3/17/23
to bbe...@googlegroups.com
Here’s a slightly amended version of your script:

tell application “BBEdit”
tell front text window
replace "/\\*([^+]*?)\\*/" using "<!--\\1-->" options ¬
{search mode:grep, case sensitive:false, starting at top:true}
end tell
end tell

The differences are:

- no parentheses () surrounding the whole find pattern because that becomes the first capture, which you use in the replacement pattern as ‘\1’.

- added parens around the inner part of the find pattern, in order to capture what’s between the JS comment markers.

- if you needed to keep that outer parens capture for some other reason, then change the replacement pattern to “<!—\\2—>” (assuming that part stays the second parens-capture).

- I omitted the replace “%@“ line to keep this part of the script clear.

Using the script as I wrote it above

/* some commented stuff */ /*some more*/

And /* some more*/

is transformed in one step into

<!-- some commented stuff --> <!--some more-->

And <!-- some more-->


HTH

— Bruce

_bruce__van_allen__santa_cruz_ca_
> --
> This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
> ---
> You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/65afd9cc-0a86-4821-b302-20b2948c3cdcn%40googlegroups.com.

Bruce Van Allen

unread,
Mar 17, 2023, 9:36:51 AM3/17/23
to bbe...@googlegroups.com
Oops - I mis-typed one bit: the inner part of the find pattern should be “[^*]” - asterisk instead of plus sign.

tell application “BBEdit"
tell front text window
replace "/\\*([^*]*?)\\*/" using "<!--\\1-->" options ¬
{search mode:grep, case sensitive:false, starting at top:true}
end tell
end tell


> To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/B37085B8-A95C-4105-B396-5E089AADFEFB%40cruzio.com.

Kaveh Bazargan

unread,
Mar 17, 2023, 10:23:35 AM3/17/23
to bbe...@googlegroups.com
Quick question: For AppleScript you need to double escape, right? so:
  • in AppleScript: \\*
  • becomes \* in BBEdit
  • to find "*"



--
Kaveh Bazargan PhD
Director
Accelerating the Communication of Research
  

Bruce Van Allen

unread,
Mar 17, 2023, 10:30:02 AM3/17/23
to bbe...@googlegroups.com
Yes, double escapes are needed. As handy as Applescript is, quoting is a major pain point.

‘*’ has special meaning within regular expressions, so it must be escaped when trying to match a literal ‘*’. So that's the first ‘\’. Then Applescript needs that ‘\’ escaped, so we get ‘\\*’.

The BBEdit User Manual (PDF download via the Help menu) has a good discussion of this.
> To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/CAJ2R9pgTRLttsekBAWXHFUW7NUFRcH%2BbhOEjDpsr3axsXEQhGA%40mail.gmail.com.

Kaveh Bazargan

unread,
Mar 17, 2023, 11:18:21 AM3/17/23
to bbe...@googlegroups.com
Thanks Bruce. I am familiar with grep and use it a lot. Also used a lot of AppleScript in the past but no regex in that. 

It's just that the extra escape and all the verbosity in AppleScript makes it painful to decode!



--
Kaveh Bazargan PhD
Director
Accelerating the Communication of Research
  

Mathias

unread,
Mar 18, 2023, 7:45:55 AM3/18/23
to BBEdit Talk
Huge thanks Bruce. You guys are insane at grep'ing, sed'ing etc. The answers I've gotten in here are like machine code sometimes. :)

Kaveh, the reason I use AppleScript is because I've used it outside BBEdit in the past, so I'm most familiar with that setup. I've gotten solid evidence in here previously that it's probably not the best tool for the job, but when you deal with this stuff once every three years you kind of don't have time to learn something new... (I probably should I guess). Again, thanks.

Christopher Stone

unread,
Mar 18, 2023, 2:41:38 PM3/18/23
to BBEdit-Talk
On Mar 17, 2023, at 09:29, Bruce Van Allen <b...@cruzio.com> wrote:
Yes, double escapes are needed. As handy as Applescript is, quoting is a major pain point.


Not if you use Script Debugger.

SD has a “Paste as String Literal” command that automates the double-quoting as necessary.

I don't know for certain if the freeware "lite" version has this command, but I think it likely – since it's not listed in the feature comparison chart:


To go the other direction you can display the string in a result-viewer and select “Best View” and copy – or you can just set the clipboard to the string.

With just a little set-up this issue is trivial to manage.

-Chris

Bruce Van Allen

unread,
Mar 18, 2023, 2:46:39 PM3/18/23
to bbe...@googlegroups.com
> On Mar 17, 2023, at 09:29, you wrote:
>> Yes, double escapes are needed. As handy as Applescript is, quoting is a major pain point.
>
>
> Not if you use Script Debugger.


Thanks for the reminder! I use Script Debugger, but hadn’t been using that capability.

— Bruce

_bruce__van_allen__santa_cruz_ca_


Kaveh Bazargan

unread,
Mar 18, 2023, 4:24:40 PM3/18/23
to bbe...@googlegroups.com
All sounds good Mathias. I miss AppleScript. I wrote a lot of it to help editing videos. It's just that grep is hard to read as it is!!

Perhaps not relevant, but I recently found Text Factory in BBEdit that lets you string lots of greps together...

Regards
Kaveh

Mathias

unread,
Mar 19, 2023, 4:17:10 PM3/19/23
to BBEdit Talk
OK folks, I feel like I'm nearly there, but I have a small outstanding issue that perhaps you can find it in your heart to help me with.

To recap. I'm trying to write an all-encompassing script that converts IOS/Xcode localisation files to Android's format. This includes the strings as well as comment lines. 
Examples of rows:

IOS:
/* LOGIN */
"login_welcome" = "Welcome to My app";

ANDROID:
<!-- LOGIN -->
<string name="login_welcome">Welcome to My App</string>

CURRENT SCRIPT, BASED ON YOU COOL GUYS GREAT FEEDBACK:

tell application "BBEdit"

tell front text window

replace "(\"[^\"]+?\") = \"([^\"]+?)\";" using "<string name=\\1>\\2</string>" options ¬

{search mode:grep, case sensitive:false, starting at top:true} -- RegEx replacement.

replace "/\\*([^*]*?)\\*/" using "<!--\\1-->" options ¬

{search mode:grep, case sensitive:false, starting at top:true}

replace "%@" using "%1$s" options {case sensitive:false, starting at top:true} -- Literal replacement.

end tell

end tell


OK, but I have one outstanding issue.

What I would like is take *everything* between the last to quote signs as the body of the xml node. However, it doesn't work with escaped characters.


As an example, if I take the above row and change it to

"login_welcome" = "Welcome to \"My App\" "; - it doesn't work. 


What I would like is basically "between quote one and two, use in <string name="">, then everything between quote number three and the ending ' "; ' use as body, then end with </string>.


Does this name sense?



Christopher Stone

unread,
Mar 20, 2023, 6:28:56 PM3/20/23
to BBEdit-Talk
On Mar 19, 2023, at 15:17, Mathias <mat...@lightlabs.se> wrote:
OK folks, I feel like I'm nearly there, but I have a small outstanding issue that perhaps you can find it in your heart to help me with.


Hey Mathias,

What about something like this?

("[^"]+?") = "(.+?)";


-Chris

Mathias

unread,
Mar 23, 2023, 2:21:41 AM3/23/23
to BBEdit Talk
Chris, worked great, you're the best. Thanks!
Reply all
Reply to author
Forward
0 new messages