"read file" erroring "can't make current application into type file"

I’ve got an error I found a simple fix for, but I can’t understand the cause at all, if anyone could enlighten me.

The script involved and associated libraries run way too long to post here, so I need to try to abstract the bits I see as relevant… except I have no idea what’s relevant… so ask for further info.

There’s a handler. Inside the handler it reads the contents of a text file like this:

set wordList to read file ((path to home folder as text) & [rest of path here])

This works fine by itself. Or I can copy the whole handler out into another script and call it. It runs fine. Or I can copy a huge block of code around the call out into another script; still runs fine.

But when it’s run inside the actual script in question, the script errors on that line with:

“Can’t make current application into type file.”

So I figure something else is using the term “file” or something…

The handler it’s in doesn’t have that inside a tell or anything, but the entire handler is called inside a
tell application “Adobe Photoshop”
tell the current document

But in my experience, a handler called from inside a “tell” doesn’t carry over the tell. And I’ve tested it inside all the “tell” statements in a broken-out script and it still works.

Then I thought it might be something ASObjC related redefining “file” in this context. The script’s got the following headers:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
use script "Dialog Toolkit ROT"

But I added all that to my broken-out copy, and it still runs fine.

So, what makes it work is simply putting that line inside a tell…

tell application "System Events" to set wordList to read file ((path to home folder as text) & [rest of path here])

Now it works and doesn’t get the error… but why is this necessary? What other code in my script could be causing this to require being inside a “tell” to make it work?

One reason for that error is explained by Shane at:


Although, this should be encountered with the “broken-out” script which you say is not the case, so perhaps there’s another issue.

Use tell me when the command uses some classes of AppleScript inside the tell blocks of applications, like in your case. This way you indicate that you reffered to class file of AppleScript (which in your script becomes file object when loading the script for execution in the RAM) and not to class file of application “Adobe Photoshop”. Because they are different things with same class name.

tell me to set wordList to read file ((path to home folder as text) & [rest of path here])

Thanks peavine and KniazidisR.

peavine - that link sure sounds like exactly it, with the same error message also using “read file.” But as you noted, I did copy and paste the entirety of the “use” lines from the other script, and it still worked! No idea why it did work then.

KniazidisR - thanks, “tell me” is probably better than picking a random application like System Events to tell.