[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-users] error when using .gui method on an Instr



Hey James,

This seems useful. I've sometimes wanted something like Document.current when executing code in files, for purposes of dynamic path generation, etc.

A couple of thoughts: Perhaps currentFile should return Document.current.path when you're not in executeFile? Also maybe currentFilePath or something like this would be a better name, as currentFile suggests that it returns a File object.

S.

On 20 Oct 2007, at 14:54, James Harkins wrote:

On Oct 19, 2007, at 10:49 AM, James Harkins wrote:
Yeah, this is something I complained about a couple of months ago. For
some reason, an Instr's path variable never gets populated anymore. I
might be able to fix it over the weekend if I can't, it will be up to
felix.

Bug 1816592 is now logged for this.

hjh

I have a proof of concept to fix this, but felt I should run it by the list before committing anything.

The main issue is that formerly, there was only one directory for Instr definitions, ./Instr. Since Instr now also looks in quark directories, the old method of assigning the path -- using the one and only directory plus the first entry in the symbolized name -- won't work because you can't assume the location.

That leads to something I've wanted for quite some time -- the ability to know in the middle of code execution, if you're in a file, what file it is. (That would be very helpful to me in chucklib also.)

Just a few simple changes:

In Main.sc:

// proof-of-concept: the interpreter can set this variable when executing code in a file
// should be nil most of the time
classvar <>currentFile;

---

In Kernel.sc (Interpreter class):

executeFile { arg pathName ... args;
var result;
if (File.exists(pathName).not) { ["file \"",pathName,"\" does not exist."].join.postln; ^nil };
Main.currentFile = pathName;
protect { result = this.compileFile(pathName).valueArray(args) }
{ Main.currentFile = nil };
^result
}

---

Then, to fix Instr:

init { arg specs,outsp;
path = Main.currentFile; // this line is new
this.makeSpecs(specs ? #[]);
if(outsp.isNil,{
outSpec = nil;
},{
outSpec = outsp.asSpec;
});
this.class.put(this);
}

Or maybe Instr:init should have

path = Main.currentFile ?? { Document.current.path };

Any issues with this? I think it's generally useful.

Oh yes, and guiBody needs to check path.notNil before calling File.exists(path) -- because it's legit to have an Instr with no path (if you create it in an unsaved document).

hjh


: H. James Harkins
.::!:.:.......:.::........:..!.::.::...:..:...:.:.:.:..:

"Come said the Muse,
Sing me a song no poet has yet chanted,
Sing me the universal."  -- Whitman

_______________________________________________
sc-users mailing list