Problem with Writing Files on Big Sur?

I just realized I don’t know if my friend has given BBEdit full-disk-access or accessibility privileges. I’ll find out and report back, but I’m going to post this in the meantime – since it’s written already.


Hey Folks,

Frustrating when you can’t test things directly…

This script is supposed to write each line of the front window in BBEdit to a separate file in the ~/Downloads folder.

Each file is named the same as the line, and the content is the same. This request came through on BBEdit-Talk.

When run from the BBEdit Script Menu, it works fine on my Sierra Mac and on my Mojave Mac (although SIP is turned off on it).

Is there an obvious reason this won’t work on Big Sur?

I’ve got a friend who runs a little non-profit who wants a copy, and I’d like to get it running for him.

TIA.

-Chris

--------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2020/12/19 07:34
# dMod: 2020/12/21 13:07
# Appl: BBEdit
# Task: Create Files in the Finder from Lines in the Front Document.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @BBEdit, @Create, @Write, @Files, @Finder
--------------------------------------------------------

try
   
   --------------------------------------------------------
   
   --» USER SETTING - Path to the destination folder
   --		May be an alias or a POSIX Path string or a $HOME-based POSIX Path.
   
   # set destinationFolder to path to downloads folder
   
   set destinationFolder to "~/Downloads/"
   
   --------------------------------------------------------
   
   
   tell application "System Events"
      if class of destinationFolder ≠ text then
         set destinationFolder to destinationFolder as text
      end if
      set destinationFolder to POSIX path of disk item destinationFolder
      if character -1 of destinationFolder ≠ "/" then
         set destinationFolder to destinationFolder & "/"
      end if
   end tell
   
   tell application "BBEdit"
      tell front text window to set lineList to contents of its lines where contents is not ""
   end tell
   
   repeat with i in lineList
      writeUTF8(i, destinationFolder & i & ".txt")
   end repeat
   
   set shCMD to "
      osascript -e 'display notification \"Finished!\" with title \"BBEdit\" subtitle \"Write Files Script\" sound name \"Tink\"'
   "
   do shell script shCMD
   
on error errMsg number errNum
   stdErr(errMsg, errNum, true, true) of me
end try

--------------------------------------------------------
--» HANDLERS
--------------------------------------------------------
on stdErr(errMsg, errNum, beepFlag, ddFlag)
   set errMsg to errMsg & return & return & "Num: " & errNum
   if beepFlag = true then
      beep
   end if
   if ddFlag = true then
      if errNum ≠ -128 then
         try
            tell application (path to frontmost application as text) to set ddButton to button returned of ¬
               (display dialog errMsg with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
                  default button "OK" giving up after 30)
            if ddButton = "Copy Error Message" then set the clipboard to errMsg
         end try
      end if
   else
      return errMsg
   end if
end stdErr
--------------------------------------------------------
on writeUTF8(dataStr, targetFilePath)
   try
      if targetFilePath starts with "~/" then
         set targetFilePath to POSIX path of (path to home folder as text) & text 3 thru -1 of targetFilePath
      end if
      set fRef to open for access targetFilePath with write permission
      set eof of fRef to 0
      write dataStr to fRef as «class utf8»
      close access fRef
      
      if targetFilePath contains ":" then
         return alias targetFilePath
      else if targetFilePath starts with "/" then
         return alias POSIX file targetFilePath
      end if
      
   on error errMsg number errNum
      try
         close access fRef
      on error errMsg number errNum
         error "Error in writeUTF8() handler of library: gen.lib" & return & return & errMsg
      end try
   end try
end writeUTF8
--------------------------------------------------------

That would be my first guess.

It was true indeed, but there’s still a problem.

I’ll probably have to do a screen share to suss it out.

-Chris