Problem running AppleScript from BBEdit script menu

243 views
Skip to first unread message

jgill

unread,
May 14, 2014, 8:20:53 AM5/14/14
to bbe...@googlegroups.com
I have an Applescript that finds a placeholder comment in a web page and replaces it with a chunk of HTML. It works fine in Script Debugger, it works in AppleScript Editor and it works when I make an app from it placed on the desktop.

If I place the .scrp file into the BBEdit script folder, it shows up in the Script menu and runs when I select it but the HTML chunk is truncated with the first two lines missing and all the HTML tags stripped out.

What's going on here?

Christopher Stone

unread,
May 14, 2014, 1:36:46 PM5/14/14
to BBEdit-Talk
On May 14, 2014, at 07:20, jgill <joegille...@gmail.com> wrote:
If I place the .scrp file into the BBEdit script folder, it shows up in the Script menu and runs when I select it but the HTML chunk is truncated with the first two lines missing and all the HTML tags stripped out... What's going on here?
______________________________________________________________________

Hey Joe,

Well.  BBEdit does occasionally do weird things when you run scripts from its script menu, but it's impossible to diagnose without seeing the script.

I can tell you that this script has been test-run from BBEdit's script menu and works.

-------------------------------------------------------------------------------------------
tell application "BBEdit"
  tell front text window
    replace "(some)_text)" using "\\1" options {search mode:grep, case sensitive:false, starting at top:true}
  end tell
end tell
-------------------------------------------------------------------------------------------


jgill

unread,
May 14, 2014, 2:32:42 PM5/14/14
to bbe...@googlegroups.com
I started off by recording this script using Script Debugger using copy and paste. Then I switched to using variables.


tell application "BBEdit"
    activate
    make new text document with properties {contents:current clipboard}
-- raw text from email
    apply text factory file "iMac HD:Users:account:Library:Application Support:BBEdit:Text Filters:chogdayreport.textfactory" to text 1 of text document 1 saving no
-- text factory formats text to HTML
    set t to text 1 of project window 1
-- t set to HTML
    select text document 1 of project window "index.html"
    select insertion point before character 1 of document 1
    set bounds of project window 1 to {320, 125, 1674, 1399}
    set a to text 1 of project window "index.html"
-- display dialog (a) shows correct text except when using BBEdit script menu
-- get the offset of insertion placeholder
    set o to offset of "<!--insert-->" in a
    set characters o thru (o + 12) of document 1 to t
-- replace selection with HTML text
--BBEdit put the text in the correct place except that the text is corrupted
end tell

I am not using grep replace.


Christopher Stone

unread,
May 14, 2014, 5:14:41 PM5/14/14
to BBEdit-Talk
On May 14, 2014, at 13:32, jgill <joegille...@gmail.com> wrote:
I started off by recording this script using Script Debugger using copy and paste. Then I switched to using variables.
______________________________________________________________________

Hey Joe,

It's great that BBEdit is so recordable, but it doesn't always produce straightforward code.  From yours it appears you're producing a new document with text, processing it, and then replacing your designated token in another document with that newly processed text.

I would prefer to script the excision of text from Mail if possible, and I would probably run my script from FastScripts (in the Mail context) rather than from BBEdit's script menu - but your milage may vary.

When I first wrote this it did not properly grab the processed text, and I had to put in a short delay after the apply text factory action to get it to work correctly.

Working with front project window or project window 1 can become problematic as you switch windows, so it's often a good idea to give temp windows a name (see script).

It's always a good idea to have an error-handler in your scripts.

You'll have to change names and paths to get this to work, and there may be other tweaks required - but I think it's close.

--
Best Regards,
Chris

-------------------------------------------------------------------------------------------
try

  

  set textFactory to alias ((path to application support from user domain as text) & "BBEdit:Text Filters:Textfactories:UpperCaseAll.textfactory")
  set tempDocName to "Temp_Window_" & (do shell script "date \"+%Y-%m-%d_%H%M%S\"")
  set workingDocName to "WORKING.txt"

  

  tell application "Mail"
    set _sel to selection
    if length of _sel = 1 then
      set _msg to item 1 of _sel
      set _text to content of _msg
    end if
  end tell

  

  tell application "BBEdit"
    # Getting text directly from Mail
    set tempDoc to (name of (make new text document with properties {name:tempDocName, bounds:{303, 44, 1617, 1196}, text:_text}))

    

    # Getting text from the clipboard
    # set tempDoc to name of (make new text document with properties {name:tempDocName, bounds:{303, 44, 1617, 1196}, text:(get the clipboard)})

    

    apply text factory textFactory to text document tempDoc
    delay 0.1 # FAILS WITHOUT THE DELAY.
    set _text to text of text document tempDoc
    select (first project window whose name starts with workingDocName)

    

    tell text of text document "WORKING.txt"
      replace "<!--insert-->" using _text options {starting at top:true}
    end tell
  end tell

  

on error e number n
  set e to e & return & return & "Num: " & n
  tell me to set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
  if button returned of dDlg = "Copy" then set the clipboard to e
end try

-------------------------------------------------------------------------------------------

jgill

unread,
May 15, 2014, 4:46:21 AM5/15/14
to bbe...@googlegroups.com
Thank you for your help, Christopher. I couldn't get the Mail part of this script to work however, if I use the clipboard contents (I am only using a selection fromf the email anyway) it works fine. Thanks for the tip about FastScripts too, I hadn't come across this one before.

Christopher Stone

unread,
May 15, 2014, 10:04:20 PM5/15/14
to BBEdit-Talk
On May 15, 2014, at 03:46, jgill <joegille...@gmail.com> wrote:
Thank you for your help, Christopher.
______________________________________________________________________

Hey Joe,

You're welcome.

I couldn't get the Mail part of this script to work however, if I use the clipboard contents (I am only using a selection fromf the email anyway) it works fine.

Ah, I think you said raw text in your original query, so I made the wrong assumption.

No, that code won't work with selected-text (which is unfortunately not supported in Mail's AppleScript dictionary).

Thanks for the tip about FastScripts too, I hadn't come across this one before.

I use FastScripts to run nearly all of my AppleScripts (unlike Apple's AppleScript menu it can handle per application keyboard shortcuts), although I do use BBEdit's own script menu for BBEdit-specific stuff.

For macros that require actions other than AppleScript I use Keyboard Maestro.

In a case like yours it would be easy to make KM do the copy-to-clipboard in Mail and then perform all subsequent other actions.

You could do it all in AppleScript though.

-------------------------------------------------------------------------------------------
tell application "System Events"
  set quit delay to 0
  tell process "Mail"
    set frontmost to true
    keystroke "c" using {command down}
  end tell
end tell
tell application "BBEdit"
  activate
  # Do your thing.
end tell
-------------------------------------------------------------------------------------------

As you can see you have a fair amount of choice.

--
Best Regards,
Chris

jgill

unread,
May 16, 2014, 7:01:51 AM5/16/14
to bbe...@googlegroups.com
Thank you again, Christopher.

I don't have Keyboard Maestro but I do have QuickKeys which is similar. I'll have a play.

Cheers, Joe
Reply all
Reply to author
Forward
0 new messages