How can one synchronize an external application with a DigitalMicrograph script?

守給你的承諾、 提交于 2019-12-23 01:43:27

问题


This question has been inspired by the question 'Call script from command line'.

How can one write a script which acts on 'trigger events' in an application other than DigitalMicrograph?

i.e. some script functionality should be triggered by an external application.


回答1:


The scripting language does not offer many 'external' interfaces in its current state. It is possible to call out to an external process with the command LaunchExternalProcess and wait for the process to complete, but there is no straight-forward way for an external process to call in, i.e. to start a script-action within DigitalMicrograph.

However, it is possible to work around that issue by using the system's file-system as a message queue. To do this, have a script running in the background which regularly checks if a certain file exists, and have the external application create such a file when it wants to trigger a scripting-action in DigitalMicrograph. The file content - if it is a simple text file - can also be used to transport information between the two applications.

Here is an example script which will wait until the file Trigger.txt appears in the root folder. The check is performed every 10seconds.

class WaitForExternal
{
    string triggerFilePath
    number taskID
    void WaitOnTrigger( object self )
    {
        if ( DoesFileExist( triggerFilePath ) )
        {
            Result( GetTime(1) + ": Triggered! Now act..." )
            If ( TwoButtonDialog( "Do you want to reset the trigger?", "Reset", "Stop" ) )
            {
                DeleteFile( triggerFilePath )
            }
            else
            {
                RemoveMainThreadTask( taskID )
            }
        }
        else
        {
            Result( GetTime(1) + ": not yet\n" )
        }
    }

    object Init( object self, string triggerPath, number waitSec ) 
    { 
        triggerFilePath = triggerPath
        taskID = self.AddMainThreadPeriodicTask( "WaitOnTrigger", waitSec )
        return self
    }
}

// Main script
{
    string triggerPath = "C:\\Trigger.txt"
    number pollingWait = 10
    Alloc(WaitForExternal).Init( triggerPath, pollingWait )
}

Note that the periodic task waits idle in the background without interfering with the CPU, but the actual check is then performed on the main thread.



来源:https://stackoverflow.com/questions/28536623/how-can-one-synchronize-an-external-application-with-a-digitalmicrograph-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!