AppleScript question

122 views
Skip to first unread message

Greg Raven

unread,
Oct 10, 2020, 6:14:35 PM10/10/20
to BBEdit Talk
I -- the worst programmer in the world -- have whipped up an AppleScript to grab the code from the front Safari window and open it in BBEdit. Having gotten that far, however, I can't figure out how to include these two BBEdit AppleScript commands:
  • normalize tag case
  • normalize line endings
The documents do nothing for me, but then, I am the worst programmer in the world.

P.S. I realize there exists a Service that does this, but I wanted to try my hand at rolling my own.

jj

unread,
Oct 11, 2020, 3:53:16 AM10/11/20
to BBEdit Talk
Hi Greg,

Here is a script that might do what you want

tell application "Safari"
set vContents to source of first document
set vName to name of first document
end tell

tell application "BBEdit"
tell first window
set vDocument to make new document at beginning with properties {name:vName, contents:vContents}
tell vDocument
normalize line endings
format mode no reorganization with normalizing tag case
end tell
end tell
end tell

Best regards,

Jean Jourdain

Greg Raven

unread,
Oct 11, 2020, 7:46:59 AM10/11/20
to BBEdit Talk
Ah, so. Thank you! It never occurred to me that I needed a nested tell block.

Greg Raven

unread,
Oct 11, 2020, 8:09:30 AM10/11/20
to BBEdit Talk
Actually, this isn't working for me either. This is what I have so far:

```
tell application "Safari"
set sourceCode to source of first document
end tell

tell application "BBEdit"
activate
make new text window with properties {contents:sourceCode, source language:"HTML"}
optimize
# normalize line endings
format mode gentle hierarchical with normalizing tag case
copy time of (current date) to latest
save text document 1 to file ("Macintosh HD:Users:greg:Desktop:tossme-" & latest & ".html")
end tell
```
The "normalize line endings" command is commented out because it throws an error so the script does not run to completion. I tried wrapping it in it's own tell block, but I'm still missing something.

Greg Raven

unread,
Oct 11, 2020, 8:41:31 AM10/11/20
to BBEdit Talk
Here's another data point:

If I add this script to an Automator workflow (as Run AppleScript), I can then drag in the BBEdit Automator action Normalize Line Breaks (yes, "breaks" and not "endings"), and it works.

However, it does not work to change the main script itself from "normalize" to "normalizing", nor from "endings" to "breaks".

Greg Raven

unread,
Oct 11, 2020, 1:34:44 PM10/11/20
to BBEdit Talk
I got it. I don't know why this works, but I put the normalize command inside a try/catch loop in hopes of getting better debugging information. It ran fine. I deleted the catch and it still ran as expected. At this point, I'm claiming the win.

```
tell application "Safari"
set sourceCode to source of document 1
end tell

tell application "BBEdit"
activate
make new text window with properties {contents:sourceCode, source language:"HTML"}
select insertion point before character 1 of text window 1
optimize
format mode gentle hierarchical with normalizing tag case
try
normalize line endings of text window 1
end try
copy time of (current date) to latest
save text document 1 to file ("Macintosh HD:Users:greg:Desktop:tossme-" & latest & ".html")
end tell
```

jj

unread,
Oct 11, 2020, 1:36:47 PM10/11/20
to BBEdit Talk
Greg,

The BBEdit dictionary mentions that the "normalize line endings" command takes a text specifier as a parameter.

---
normalize line endingsv : convert any CRLF pairs and LF characters remaining after line-break conversion

normalize line endings specifier : text to be modified

→ text : the modified text

---

Your script as written gives to the "normalize line endings" command an implicit parameter of  type application "BBEdit" which apparently is not automatically convertible to a text specifier.
Try to give it the contents of your document which is a text specifier (something of the form: characters x thru y of ...).

normalize line endings contents of first document of first window

or use a tell block to specify the implicit parameter:

tell  first document of first window
normalize line endings
end tell

HTH,

Jean Jourdain

Greg Raven

unread,
Oct 12, 2020, 8:30:18 AM10/12/20
to BBEdit Talk
Thanks for supplying this final piece. Trying to learn AppleScript by reading the dictionaries is much like trying to learn a foreign language just from reading a bilingual dictionary for that language. Why Apple decided not to include any code samples in AppleScript dictionaries is beyond me.

John Delacour

unread,
Oct 12, 2020, 9:26:32 AM10/12/20
to bbe...@googlegroups.com


On 12 Oct 2020, at 13:30, Greg Raven <greg...@gmail.com> wrote:

Thanks for supplying this final piece. Trying to learn AppleScript by reading the dictionaries is much like trying to learn a foreign language just from reading a bilingual dictionary for that language. Why Apple decided not to include any code samples in AppleScript dictionaries is beyond me.

Here’s another way to do it:

tell application "Safari" to set _src to source of front document
tell application "BBEdit"
activate
set _doc to make document with properties {text:_src, linebreaks:Unix}
tell _doc
format mode gentle hierarchical with normalizing tag case
set _t to time of (current date)
set _f to (path to desktop as string) & _t & ".html"
save to file _f
end tell
end tell

Greg Raven

unread,
Oct 12, 2020, 10:13:49 AM10/12/20
to BBEdit Talk
This looks slick, but it doesn't run for me. I get an error on save, and the (path to desktop as string) seems not to resolve to anything.

John Delacour

unread,
Oct 12, 2020, 10:51:01 AM10/12/20
to bbe...@googlegroups.com

On 12 Oct 2020, at 15:13, Greg Raven <greg...@gmail.com> wrote:

This looks slick, but it doesn't run for me. I get an error on save, and the (path to desktop as string) seems not to resolve to anything.

That’s very odd!  I am running MacOS 10.13.6.  Hard to imagine they have made  that go away; but try instead

path to desktop as Unicode text


If that doesn’t work, goodness knows!

Obviously you will get an error on save if you have no path.

JD


Greg Raven

unread,
Oct 12, 2020, 11:06:06 AM10/12/20
to BBEdit Talk
No, each form gives the same resulting path, but neither works in the save command that follows.

I appreciate your help, but I already have a version of this script that works.

TJ Luoma

unread,
Oct 12, 2020, 9:16:13 PM10/12/20
to BBEdit MailingList

> Trying to learn AppleScript by reading the dictionaries is much like trying to learn a foreign language just from reading a bilingual dictionary for that language.

That reminds me of trying to learn how to use Unix commands and having people tell you to "Read the `man` page!"

Years ago (Usenet days, I think) I had a ".sig" file (remember those?) which included this: "If I understood the man page, I wouldn't have asked the question".

Real-world examples are always good. Even with some 'textbook examples' I find myself wondering what is placeholder text and what is not.

TJ





--
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/7d391789-98d8-43d7-9cea-3b313f75eb88n%40googlegroups.com.

MediaMouth

unread,
Oct 12, 2020, 9:23:39 PM10/12/20
to 'Jeffrey Jones' via BBEdit Talk
Or like learning how to drive by reading the auto parts manual.

In some ways JXA (JS version of AppleScript) is slightly (but only slightly) more intuitive, at a cost of being even less documented.
Reply all
Reply to author
Forward
0 new messages