AppleScript application support: 'duplicate' command fails - command

I am trying to support the 'duplicate' command. It works fine for duplicating the top class:
tell application "SpellAnalysis" to duplicate level 1
This, however, crashes:
tell application "SpellAnalysis" to duplicate (get unit 1 of (get level 1))
I have provided index specifiers for both classes, where the outer container for 'unit' is 'level'. Oddly, I can specify a property of the unit class like this:
tell application "SpellAnalysis" to (get general rule of unit 1 of (get level 1))
The culprit seems to be that the 'objectsByEvaluatingSpecifier' always returns a null value when used within NSCloneCommand subclass, as well as the unit class' objectSpecifier method, where its needed.

I was finally able to support AppleScript 'duplicate' command. Although the normal way to support the command is by sub-classing 'NSCloneCommand', with this method, your direct parameter results in a specifier that specifies the containing object and not the direct object--as a special provision of the 'NSCloneCommand'. I suppose this is meant to facilitate the case when you would be duplicating an object belonging to a document, where the document class provides the means for creating new constituent objects. Unfortunately, in my case, my constituent objects take part in their own duplication and need to know their specifiers to do so. The solution was to support the 'duplicate' command by the optional technique of creating a custom 'duplicate' command within my application's suite that entailed subclassing the more general 'NSScriptCommand'. This alternative avoids any redirection of the specifier returned from the 'directParameter' method of the 'NSScriptCommand' class. I was able to work from there to derive all the child class-objects for cloning.

Related

Make the compiler tell me that two variables can’t have the same value

I have a list of objects that need to be loaded into my app. But the load order matters. I’ve created a var which is a part of each object that indicates the load order where the lowest number gets loaded in first. Then I’ve set the default to be 9999. I can manually set this value on each object and override the default. Currently this is where I’m at.
Should I add more objects which need to be loaded in a specific order in the future, I want to make sure there are no conflicts. It seems to me like I could do that by making each load order number on the objects unique, (i.e. no duplicate numbers in all the load order variables).
Is there a way I can make Xcode throw an error or warning if it detects that anything conforming to a protocol has the same value on a variable from that protocol as any of the other conforming objects?
Well, you could list all the objects in an array literal in the order you want them loaded, and forgo the load order property entirely:
let objectsToLoad = [
ObjectDescriptor("hello"),
ObjectDescriptor("world"),
]
But if you have objects defined in disparate places and don't want a single unified array literal that lists them all, then I don't think you can get help from the compiler (or the linker).
In a few months, when Swift will have macros, you'll probably be able to get help from the compiler or the linker. The strategy is to use a macro to wrap each load-order constant in a macro that also generates some type definition whose name includes the constant, e.g. enum _LoadOrder_1 {}, enum _LoadOrder_357 {}, etc. Then, if you have a duplicate, either the compiler or the linker will fail due to the multiple definitions for a single identifier.

More advanced filtering system (LHS brackets | RHS colon) in FastAPI

Is it possible to create a filtering system in FastAPI like:
?something[lte]=120&something2[in]=12,34
or
?something=lte:120&something2=in:12,34
And how to make OpenAPI documentation for it?
Imagine if there are many such endpoints and even more fields? I do not want to describe a set of fields with the same type of Depends classes with a certain logic parse (I don't know how to dynamically pass type annotating for it).
It is also not possible to subclass fastapi.Query and add extra logic to it.
Also there was an idea in the implementation of the f key as a query parameter:
?f=<field_name>:<operator>:<value>&f=<field_name N>:<operator>:<value>
But in this case, the documentation is not complete and you have to fill it in description (it will also be the same for all endpoints). As a result, it will not be clear which fields can be filtered and which operators are used for them.

Is it possible to get the type of a variable while computing completion suggestions?

I'm thinking of creating a VSCode extension to help people use a library I am creating. I've looked in 50 places and can only see documentation on getting the plain text of the document in which the completion suggestions are triggered.
My library exposes a function with two parameters, both objects with the same keys, but the first one will be defined already and the 2nd one will be passed in as an object literal. For example
const obj1 = {a:1, b:2};
doLibraryThing(obj1, {a:[], b:[]});
I want to provide code completion, or a snippet, or anything that can detect that doLibraryThing is being called, and know what properties were defined in obj1, even though usually it will be imported from another file; and then use those properties to generate the suggestion with the same properties, {a:[], b:[]}.
Is this possible?

How can I use the .getServicedEntity() method on a Resource Unit without casting the agent type?

I built a custom block that, among other things, tells the resource entering portIn of the block to move to the resource's seizing unit. I use a moveTo block with the destination node as: (Node)((Cart)agent).getServicedEntity().getNetworkNode(); but I need to know the agent type and cast it into the method. I would like to make this a generic block that can be used in other models and with any Resource Unit.
I've tried using generic parameters in the custom block
then selecting the agent type:
and then trying: (Node)T.getServicedEntity().getNetworkNode(); but this results in compiling error: The method getServicedEntity() is undefined for the type T. Is there a way to do what I'm looking to do? I'm fine if the user has to select the agent type using the generic parameter pulldown, but I'd like to avoid having to change the code every time to add in all of the resource types available in the model using the instanceof command and then duplicating the code. Seems inefficient.
Well, your "T" extends Agent, and Agent does not know about getServicedEntity.
What your T extends needs to know it is a resource unit. Easiest solution I can see:
Create a parent class MyResourceUnit (but do not instantiate it)
Make sure it is "used in flowcharts as Resource Unit"
Make all agent types that should ever use your custom block to extend MyResourceUnit
Now in your custom block, you should make T extends MyResourceUnit

lua - capturing variable assignments

Ruby has this very interesting functionality in which when you create a class with 'Class.new' and assign it to a constant (uppercase), the language "magically" sets up the name of the class so it matches the constant.
# This is ruby code
MyRubyClass = Class.new(SuperClass)
puts MyRubyClass.name # "MyRubyClass"
It seems ruby "captures" the assignment and inserts sets the name on the anonymous class.
I'd like to know if there's a way to do something similar in Lua.
I've implemented my own class system, but for it to work I've got to specify the same name twice:
-- This is Lua code
MyLuaClass = class('MyLuaClass', SuperClass)
print(MyLuaClass.name) -- MyLuaClass
I'd like to get rid of that 'MyLuaClass' string. Is there any way to do this in Lua?
When assigning to global variables you can set a __newindex metamethod for the table of globals to catch assignments of class variables and do whatever is needed.
You can eliminate one of the mentions of MyLuaClass...
> function class(name,superclass) _G[name] = {superclass=superclass} end
> class('MyLuaClass',33)
> =MyLuaClass
table: 0x10010b900
> =MyLuaClass.superclass
33
>
Not really. Lua is not an object-orientated language. It can behave like one sometimes. But far from every time. Classes are not special values in Lua. A table has the value you put in it, no more. The best you can do is manually set the key in _G from the class function and eliminate having to take the return value.
I guess that if it REALLY, REALLY bothers you, you could use debug.traceback(), get a stack trace, find the calling file, and parse it to find the variable name. Then set that. But that's more than a little overkill.
With respect at least to Lua 5.2: You can capture assignments to A) the global table of a Lua State, as mentioned in a previous reply, and also B) to any other Lua Object whose __index and __newindex metamethods have been substituted (by replacing the metatable), this I can confirm as I'm currently using both these techniques to hook and redirect assignments made by Lua scripts to external C/C++ resource management.
There is a gotcha with regards to reading them back though, the trick is to NOT let the values be set in a Lua State.
As soon as they exist there, your hooks will fail to be called, so if you want to go down this path, you need to capture ALL get/set attempts, and NEVER store the values in a Lua State.