BBEdit AppleScript syntax error - why?

Hi
I’m early adept of AppleScript (still :frowning: ),

Can someone explain me why code like below doesn’t compile (Syntax Error: A class name can’t go after this identifier - in line set source language …)

tell application "BBEdit"
    set theDoc to get first document	
    documentDidOpen(theDoc)
end tell

on documentDidOpen(theDoc)

    tell theDoc
        set theExt to get name
        if theExt ends with ".popcliptxt" then
            set source language to "javascript"
        end if
    end tell	

end documentDidOpen
	

but code like this compiles and works correctly?

tell application "BBEdit"
	set theDoc to get first document
	
	tell theDoc
		set theExt to get name
		if theExt ends with ".popcliptxt" then
			set source language to "javascript"
		end if
	end tell	
end tell

I actually get, ‘A identifier can’t go after this identifier.’ as the error (2740).

The issue is that only BBEdit considers source language to be a meaningful term. So when you use them outside of a bbedit tell block, they’re treated the same as any two words. You would get the same error with any pair of non-applescript words.

Note that if you only used one word, then it wouldn’t be treated as a property, instead it would be treated as a variable and compile. You just can’t have two such words in a row.

Anyway, to get it to work, you need to put any handler lines depending upon bbedit inside an application tell block (or maybe a using terms from block. Apparently, telling only the document is insufficient.

Also, if you call a handler from within an application tell block, you will need to preface it with my, like below. The reason you haven’t seen the error yet is because it won’t happen until the script runs and reaches that line.

tell application "BBEdit"
	set theDoc to get first document
	my documentDidOpen(theDoc)
end tell
1 Like

Thanks!

Does it mean that AppleScript “sends” text inside tell block to dedicated application during compilation to check syntax?

If yes - I’m very confused - I’ve never seen programming language like AS (as long Term Unix programmer).
Seem to be not possible to test scripts for BBEdit outside of it.

AppleScript will use the BBEdit dictionary when compiling code within a BBEdit tell block. Your handler is outside the scope of the BBEdit tell block, hence the BBEdit dictionary won’t be used when compiling it. So as Mockman noted, “source language” doesn’t match up to anything the compiler knows about at that point in the compile.

1 Like

Thanks a lot - it’s hard to learn AppleScript today - it seems that many persons know so much because have years of practice.
I have a few books about AS (looking int them very often) but still don’t “fill” that language.

I found this example Opening other apps when opening a project on BBEdit Talk discussion list.

The phrase using terms is the solution!