tell block not functioning

I’m converting a large app to ASOC and find many confusing stumbling blocks. One segment involves BBEdit for a grep search later. I tried this segment using Applescript Editor (and display dialog) and it works. It gives a sequence of dialogs with the numbers 1,2,3.

on processInfo()
log "1"
tell application "BBEdit"
tell application "Finder" to set visible of process "BBEdit" to false
log "2"
set s_options_lt to {search mode:literal, starting at top:true, wrap around:false, backwards:false, case sensitive:false, match words:false, extend selection:false} -- literal, start at top
end tell
log "3"
end processInfo

Using ASOC in XCode, I only get the numbers 1,3. The tell block seems to be skipped. BBEdit is activated. I’ve removed the second “launch” and the Finder step and had no change. Any suggestions?

What happens if you cut out the launch and Finder stuff? According to the 10.5 release notes:

“AppleScript has always launched applications if it needed to in order to send them a command. However, they would always launch visibly, which could be visually disruptive. AppleScript now launches applications hidden by default. They will not be visible unless the script explicitly says otherwise using launch or activate.”

Thanks, Shane, for the valuable information about launching invisibly.

However, removing those three statements doesn’t do the trick. The items in the record s_options_lt are set correctly so my only problem that the log statement refuses to work within that block. should I tell some process or other to log “2”?

You can’t use log within a tell block unless you wrap it in its own “tell current application” block.

I still have a tell block problem that doesn’t occur in applescript editor:

	on countFiles(theFolder)
		local cc
		log "1"
		tell application "Finder" to set cc to (count every item of (folder (theFolder as alias)))
		log "2"
		return cc
	end countFiles

When running in ASOC, I get console output containing

It takes 24 seconds to get through the Finder step. What’s happening here? Is there another way to count objects in a folder? Thanks

I’m not sure what’s happening, but almost anything will be quicker than the Finder. You could use ls and do shell script, or better still, NSFileManger:

tell current application's class "NSFileManager"'s defaultManager() to set cc to contentsOfDirectoryAtPath_error_(POSIX path of theFolder, missing value)'s |count|()

That should be a tad faster…

Once again, thank you.
It counts all the items but includes all invisible files such as .DSStore
It takes less than a millisecond to do this as opposed to the 24 seconds for the Finder.
I don’t suppose there is an automatic way to exclude invisible items?

That’s the beauty of cutting out the middle man.

There are a couple of options. One is to filter the result with a predicate:

tell current application's class "NSFileManager"'s defaultManager() to set cc to contentsOfDirectoryAtPath_error_(POSIX path of theFolder, missing value) --'s |count|()
set aPredicate to current application's class "NSPredicate"'s predicateWithFormat_("not SELF  beginswith '.'")
set cc to cc's filteredArrayUsingPredicate_(aPredicate)'s |count|()

Another way is to use a different method of NSFileManager, one that requires a file URL:

set theURL to current application's class "NSURL"'s fileURLWithPath_(POSIX path of theFolder)
tell current application's class "NSFileManager"'s defaultManager() to set cc to contentsOfDirectoryAtURL_includingPropertiesForKeys_options_error_(theURL, missing value, current application's NSDirectoryEnumerationSkipsHiddenFiles, missing value)'s |count|()

More typing than the Finder, but also much faster…