Script help - Convert untitled text to notes

54 views
Skip to first unread message

Jim Straus

unread,
Jul 23, 2021, 11:04:26 AM7/23/21
to BBEdit Talk
I've created a script to convert all my untitled text documents to notes (kind of surprised something like this wasn't provided with the release of BBEdit 14).  It generally works, but for some notes the body is empty.  If I put in a dialog with the text, it works, but that means I have to respond to each dialog.

Any thoughts on how to fix this?

tell application "BBEdit"
activate
set doctitles to "" --  collect every document we handled
repeat with wnd in every text window
repeat with doc in every text document of wnd
if doc is modifiable and doc is modified and name of doc starts with "untitled text" then
set doctext to (text of doc) as string
if doctext is not equal to "" then
tell application "System Events" -- create a new note
tell process "BBEdit"
click menu item "Note" of menu of menu item "New" of menu "File" of menu bar 1
end tell
end tell
display dialog "text: " & doctext -- WITHOUT THIS SOME NOTES ARE EMPTY?!?
set contents of text document 1 to doctext
set doctitles to doctitles & name of doc & return
end if
delay 1
end if
end repeat
end repeat
-- put the list of handled documents into a new note.
tell application "System Events"
tell process "BBEdit"
click menu item "Note" of menu of menu item "New" of menu "File" of menu bar 1
end tell
end tell
set the clipboard to doctitles
paste
end tell

Christopher Stone

unread,
Jul 23, 2021, 12:06:57 PM7/23/21
to BBEdit-Talk
On Jul 23, 2021, at 10:03, Jim Straus <str...@gmail.com> wrote:

I've created a script to convert all my untitled text documents to notes (kind of surprised something like this wasn't provided with the release of BBEdit 14).  It generally works, but for some notes the body is empty.  If I put in a dialog with the text, it works, but that means I have to respond to each dialog.

Any thoughts on how to fix this?


Hey Jim,

It would be nice if Rich added a make new note command to the AppleScript dictionary, but this should work for now.

I recommend commenting-out close theDoc until you're confident the script works as intended.

--
Best Regards,
Chris



--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2021/07/23 10:57
# dMod: 2021/07/23 10:57 
# Appl: BBEdit, System Events
# Task: Convert Unsaved Documents into Notes.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @System_Events, @Convert, @Unsaved, @Documents, @Notes
# Test: Only on macOS 10.14.6 Mojave
# Vers: 1.00
--------------------------------------------------------

tell application "BBEdit"

    

    set docList to text documents whose name starts with "untitled" and on disk is false

    

    repeat with theDoc in docList
        if (length of theDoc's text > 0) then
            select theDoc
            select theDoc's text
            saveSelectedTextAsNote() of me
            close theDoc
        end if
    end repeat

    

end tell

--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on saveSelectedTextAsNote()
    tell application "System Events"
        tell application process "BBEdit"
            tell menu bar 1
                tell menu bar item "File"
                    tell menu 1
                        tell menu item "Save as Note"
                            perform action "AXPress"
                        end tell
                    end tell
                end tell
            end tell
        end tell
    end tell
end saveSelectedTextAsNote
--------------------------------------------------------

Christopher Waterman

unread,
Jul 23, 2021, 9:04:15 PM7/23/21
to bbe...@googlegroups.com
Another option of note … 😃 … 😶…  anyway. The BBEdit command line utility has a note flag.
So this should work

tell application "BBEdit"


set docList to text documents whose name starts with "untitled" and on disk is false


repeat with theDoc in docList
if (length of theDoc's text > 0) then
set theContent to theDoc's text
do shell script "echo " & quoted form of theContent & " | /usr/local/bin/bbedit --note"
close theDoc
end if
end repeat


end tell

This will leave a bunch of open notes.
Mostly I mention it because it’s good to know that `bbedit --note` exists. 

--
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/B908AB4C-47D5-48D6-9033-3ED67CED4584%40gmail.com.

Reply all
Reply to author
Forward
0 new messages