Big problem with a UI Script. It works from Editor, not from BBEdit

Hi,
These short UI scripts work perfectly running from Editor or from BBEdit’s AS Menu:


tell application "BBEdit" to activate
tell application "System Events" to tell process "BBEdit"
	keystroke "B" using {command down, shift down}
end tell

The above works, and also the similar


tell application "BBEdit" to activate
tell application "System Events" to tell process "BBEdit"
	click menu item "Balance & Fold" of menu "View" of menu bar 1
end tell

And these UI blocks work both perfectly in a script I’ve made where I can find a tag in sequence (

,

and so on), then perform in sequence the “Fold” UI action on the found tags.
Basically it finds the first desired tag, then run the UI action, then repeat: finds the next desired tag and so on.
And it works, but. it works only running from AppleScript Editor :frowning:

If I run the same script from BBEdit’s AS Menu the UI doesn’t work. Not even using (a not necessary in the Editor.) delay in the repeat iter. I can’t understand why yes from Editor, not from BBEdit Menu. I had the UI block in an Handler and I’ve tried to use the block in the main script without the Handler,… no change.
Any idea? thanks.

BTW I simply hate UI Script .

I don’t own BBEdit but with its brother TextWrangler, the correct syntax is :

tell application "TextWrangler" to activate
tell application "System Events" to tell process "TextWrangler"
	click menu item "Balance & Fold" of menu 1 of menu bar item "View" of menu bar 1
end tell

An alternate way would be :

tell application "TextWrangler" to activate
tell application "System Events" to tell process "TextWrangler"
	keystroke "b" using {command down, shift down}
end tell

Yvan KOENIG (VALLAURIS, France) jeudi 26 décembre 2013 11:57:16

Thanks for your advice, actually there is more than one way to write it. One could even be


tell application "BBEdit" to activate
tell application "System Events"
    click menu item 12 of ¬
        ((process "BBEdit")'s (menu bar 1)'s ¬
             (menu bar item 6)'s (menu "View"))
end tell

But anyway your suggestion doesn’t fix my problem :frowning:
Problem which occurs only running the script from the BBEdit’s AS Menu.

My script is rather longer (with other settings and dialog to fit the different cases and so on) but in short could be as below (note it runs a “format mode gentle hierarchical” before these steps). Here to fold every

 tags without worrying about the nesting :


tell application "BBEdit"
	select insertion point before line 1 of front window
	
	set _an to "\\t*"
	set theEtag to "pre"
	set theStag to theEtag & "([^>]*)"
	
	set fnd to ¬
		(find ("^" & _an & "</" & theEtag & ">") searching in {text 1 of window 1} ¬
			options {search mode:grep, returning results:true, starting at top:true})
	if found of fnd then
		set nrpt_ to count of item of found matches of the result
	end if
	
	set _sf to "^" & _an & "<" & theStag & ">(?s)(.+?)" & "^" & _an & "</" & theEtag & ">"
	
	repeat nrpt_ times
		find _sf searching in text 1 of window 1 ¬
			options {search mode:grep} with selecting match
		set Sl to startLine of selection
		set El to endLine of selection
		select (lines (Sl + 1) thru (El - 1)) of front window
		
		activate
		tell application "System Events"
			tell process "BBEdit"
				click menu item "Balance & Fold" of menu 1 of menu bar item "View" of menu bar 1
				delay 0.5  -- with or without delay, from AS Editor it works always
			end tell
		end tell
		
		select insertion point after (last character of selection) of front window
	end repeat
	
end tell

As I don’t own BBEdit and have no plan to buy it I am unable to find what is wrong.
I just pointed the fact that the original code was wrong.

You may try to install the System’s Script menu
then in the folder Macintosh HD:Users::Library:Scripts:

create a folder named Applications embedding a folder named BBEdit (double check the case)

Store in this subfolder the scripts dedicated to BBEdit.
With a bit of luck this way they will behave flawlessly.

Yvan KOENIG (VALLAURIS, France) jeudi 26 décembre 2013 21:11:05

Hello.

First of all, when dealing with problems like this, you should really have everything inside a try on error end try block, and catch any errors if any in your script. It has surprised me on several occasions . :slight_smile:

If it runs without throwing any errors, then one thing that may be worth to try, is to split up that big tell application “BBEdit” block into one that ends before the repeat loop, then one inside the repeat loop, that ends before the System Events block, and finally one at the end of the repeat loop.

The reason for the different behaviour, may be that BBEdit provides its own script runner, or invokes it differently, (I haven’t looked into the matter.)

Right!!. You are so right, I was so sad I didn’t think about the try block :wink:

But I didn’t get any message error.
And not only.
First of all I try to split up the big tell application “BBEdit” but nothing change.
Then I’ve seen that…

. if I use this action:


tell application "System Events"
	click menu item "Balance & Fold" of menu 1 of menu bar item "View" of menu bar 1 of application process "BBEdit"
end tell

My script as expected finds and selects sequentially all the contents of my

 tag, but it folds only the last one (no matter if I have 2 or 50 
 tags, I get Folded only the last one) and then at the end, I see the “Apple Menu” flashing (like fast open/close) as many times as the number of my 
.

.if I use this other action:


tell application "System Events"
	tell application process "BBedit" to ¬
		keystroke "b" using {command down, shift down}
end tell

My script as expected finds and selects sequentially all the contents of my

 tag, but then after the last 
, it folds the whole content of the  tag. This because I guess it runs the folding action outside the 
 tag.

In short it seems the UI action works only at the end of the whole repeat. And no changes adding a delay.
Of course, I have to repeat, not so if I run from AppleScript Editor (!!!) :frowning:

Hello.

Well, it seems to me that your only option is to tell AppleScript to run your script from BBEdit, while you wait for reply, or find a work around in the BBEdit fora.

Be sure to mention that you want a way to fold everything by shortcut, while you ask for a fix, subsidiary that the commands fold, balance and fold be added to their dictionary.

BareBones has always taken great pride in BBEdit’s scriptability, so I believe they’ll help you out on this one.

They at least had a fora at google groups, (but be sure to tell BareBones about your issue).

I’m sorry, but this is the only help I can give you.

Well, at the end it seems a BBEdit bug :frowning:

I don’t know if a bug of the last version I’m using (10.5.7) or not.
Anyway I’ll contact the BBEdit support about it.

Thanks to all for your replies.

10.5.7 is the current BBEdit version.

Did you tried to store the script as I wrote in the folder
“~/Library/Scripts/Applications/BBEdit/”

Yvan KOENIG (VALLAURIS, France) lundi 30 décembre 2013 19:20:05

Your idea to store the script in that folder was perfect, but I have not tried in that folder. And I have not tried because running the script “outside” BBEdit Menu using FastScript folder is the same and it works.

Well, I hope they will fix the BBEdit bug (and TextWrangler as well), and they will add the Fold commands scriptables because UI scripting is not exactly the best, you know :slight_smile: