Extract File name and path from image

Hello

I recorded a module which opened up an image. I wanted to then extract the filename and path so that this could be associated with analysis results when they are sent to excel but none of the variables appear to hold the filename except for the manually entered part which isn't really what I want as I need this to be automatic. The code is below:

Public Function NewMacro() As SimpleScript
        NewMacro = New SimpleScript
        Dim docList1 = New List(1), doc1

        With Application.DocumentCommands.Open(NewMacro)
            .Filenames = New String() {"C:\Users\dkelly2\Desktop\NET47Chr19z_1_w2FITC.TIF"}
            .Run(docList1)
        End With

        With Application.DocumentCommands.Activate(NewMacro)
            .Run(docList1(0), doc1)
        End With

    End Function

I added a watch on docList1, docList(0), doc1, List1 and none of them appear to hold the info. Does anyone know how I would extract this
Regards
David

Best Answers

  • Accepted Answer
    Hi David,

    FileName can be extracted from File operator of McImage, so you can get it using:

        Public Function PrintImageFileName() As SimpleScript
            PrintImageFileName = New SimpleScript
            Dim docList1 = New List(1), doc1
    
            With Application.DocumentCommands.OpenImage(PrintImageFileName)
                .Filenames = New String() {""}
                .Run(docList1)
            End With
    
            With Application.DocumentCommands.Activate(PrintImageFileName)
                .Run(docList1(0), doc1)
            End With
    
            With Automate.ScriptingCommands.CodeCommand(PrintImageFileName)
                If .Run() Then
                    'print file name'
                    Dim im As McImage=doc1.Data
                    Dim fileName As String=im.File.FullPathName
                    Debug.Print (fileName)
                End If
            End With
    
        End Function
    
    To have intellisense for File object in the scripting workbench you have to add reference to MediaCy.IQL.IO.dll.

    Regards,

    Yuri
  • Accepted Answer

    Dave,

    If I leave Yuri's code exactly as supplied, it prompts me for the file to open, and if I open the file, the output gets printed out to the Immediate window in the Project Workbench. Note that if you are looking at your script in design mode, you won't by default see the Immediate window - I always switch to Code view, where the Immediate, Watch, etc. windows are displayed at the top.

    BR,

    John

Answers

  • It is also exposed from doc1.Filename.

            With Automate.ScriptingCommands.CodeCommand(NewMacro4)
                If .Run() Then
                    ' User Code Here
                    Debug.Print doc1.Filename
                End If
            End With
    
  • Hello Yuri/Pierre,

    Neither answer worked until I modified them as below. The If .run statements are always false so it never runs so I deleted this. The code below works but for reasons I can't follow it loops 3 times on the Dim im As McImage=doc1.Data then on the 4th loop it opens the image and goes on to print the filename.

    Public Function NewMacro() As SimpleScript
            NewMacro = New SimpleScript
            Dim docList1 = New List(1), doc1
    
             With Application.DocumentCommands.OpenImage(NewMacro)
                 .Filenames = New String() {"C:\Users\dkelly2\Desktop\NET47Chr19z_1_w2FITC.TIF"}
                 .Run(docList1)
             End With
    
             With Application.DocumentCommands.Activate(NewMacro)
                 .Run(docList1(0), doc1)
             '    If .Run() Then
                     'print file name'
                     Dim im As McImage=doc1.Data
                     Dim fileName As String=im.File.FullPathName
                     Debug.Print (fileName)
             '    End If
             End With
    
        End Function
  • Hi David,

    The macros defined as SimpleScript support designer mode and get executed several times even on starting of the project (without running the macro explicitly). So our examples used a special command "ScriptingCommands.CodeCommand" that allows inserting user-defined code into SimpleScript. Your macro doesn't have it, therefore you get unexpected results. Please use CodeCommand for the code that prints file name and check if it works properly.

    Yuri
  • Hello Yuri

    How would you use CodeCommand I can't declare it in Public Function NewMacro and if you declare it anywhere else in the function it doesn't work or throws an error.

    Regards

     

    David 

  • CodeCommand can be dropped to your macro in the designer view of Project Workbench from the Toolbox (Scripting Tools section). Or simply copied from one macro to another, in that case you must replace Macro name parameter in the command constructor to your own macro name:

    With Automate.ScriptingCommands.CodeCommand(MyMacroName)
  • Hello Yuri

    I've inserted the code as suggested (my code below) but it still loops 4 times before opening the image and then crashes when it attempts to find the file name.

    Public Function NewMacro() As SimpleScript
            NewMacro = New SimpleScript
            Dim docList1 = New List(1), doc1
    
            With Application.DocumentCommands.OpenImage(NewMacro)
                .Filenames = New String() {"D:\UserData\Natalia\Eric\23siMEN1+PolyIC_1_w1DAPI.TIF"}
                .Run(docList1)
            End With
    
    
            With Automate.ScriptingCommands.CodeCommand(NewMacro)
    
                .Run(docList1(0), doc1)
               ' If .Run() Then
                    Dim im As McImage=doc1.Data
                    Dim fileName As String=im.File.FullPathName
                    Debug.Print (fileName)
               ' End If
            End With
    
        End Function
  • edited September 2013
    Hi David,

    It's still not right, you mixed 2 commands in one and skipped Activate command that defines doc1, which causes the crash. Please try to use the macro exactly as I posted (Sept 23) and let me know if it's working.

  • Hello Yuri,

    I was using that code when I posted this morning, it doesn't work at all if you leave the if statement in, it starts runs through once doesn't open anything and stops. When you remove the if statement it loops 4 times before opening the image and getting its filename for future use. It was at that point you suggested using With Automate ScriptingCommands CodeCommand to stop the looping which seems to have caused a bigger problem.

    Regards

    David

     

  • Dave,

    The if statement is necessary to have your custom code in the CodeCommand execute ONCE and only once, at the right time. What exactly happens when you run it as Yuri originally supplied it?

    BR,

    John

  • Hello John

    I was using it in code view and debug mode to try and figure out the scripting language which is when I noticed the looping and it didn't make sense to me. Running it in Design mode works but its slow so I suspect its still up to something in the background.

    I'm putting this down to the very steep learning curve between swapping between Image Pro and Premier

    Regards

    David

  • davek604 (and other interested parties) --

    The following IPP9 variables are the ones that I've found and used for tasks similar to the one that I think that you are working on:

        ThisApplication.ActiveDocument.FileName
        ThisApplication.ActiveDocument.DisplayName
        ThisApplication.ActiveImage.Name
        ThisApplication.ActiveWindow.Name

    These variables and the values they produce are demonstrated in the FRED.IPX file that is attached to this contribution to this question.  The BARNEY routine (within the WILMA module within the FRED project) sends various versions of the name of the current image to the PREMIER OUTPUT WINDOW.

    I hope this information is helpful.

    -- Matt
Sign In or Register to comment.