This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Custom Command Button: Start Program with Parameter (selected File)

I'm trying to program a Button in Task Area which give me various options when selecting a .PDF file, e.g. open the PDF File with a specific program.

I tried but when clicking the button I get an Error in Line 84.. Can you help me out?

"use strict";

function OnNewShellUI( shellUI ) {
	/// <summary>The entry point of ShellUI module.</summary>
	/// <param name="shellUI" type="MFiles.ShellUI">The new shell UI object.</param>

	// Register to listen new shell frame creation event.
	shellUI.Events.Register( Event_NewNormalShellFrame, newShellFrameHandler );
}

function newShellFrameHandler( shellFrame ) {
	/// <summary>Handles the OnNewShellFrame event.</summary>
	/// <param name="shellFrame" type="MFiles.ShellFrame">The new shell frame object.</param>

	// Register to listen the started event.
	shellFrame.Events.Register( Event_Started, getShellFrameStartedHandler( shellFrame ) );
}

function getShellFrameStartedHandler( shellFrame ) {
	/// <summary>Gets a function to handle the Started event for shell frame.</summary>
	/// <param name="shellFrame" type="MFiles.ShellFrame">The current shell frame object.</param>
	/// <returns type="MFiles.Events.OnStarted">The event handler.</returns>

	// Return the handler function for Started event.
	return function() {
	
		// Shell frame object is now started.
		
		// Create some commands.
		var commandShow1 = shellFrame.Commands.CreateCustomCommand( "Nitro Pro" );
		var commandShow2 = shellFrame.Commands.CreateCustomCommand( "FlexiPDF" );
		var commandShow3 = shellFrame.Commands.CreateCustomCommand( "PDF Annotator" );

		// Set command icons.
		shellFrame.Commands.SetIconFromPath( commandShow1, "png/nitropro.ico" );
		shellFrame.Commands.SetIconFromPath( commandShow2, "png/flexipdf.ico" );
		shellFrame.Commands.SetIconFromPath( commandShow3, "png/pdfannotator.ico" );
		
		// Add a command to the context menu.
		// shellFrame.Commands.AddCustomCommandToMenu( commandShow1, MenuLocation_ContextMenu_Bottom, 0 );
		
		// Add a commands to the task pane.
		shellFrame.TaskPane.AddCustomCommandToGroup( commandShow1, TaskPaneGroup_ViewAndModify, -110 );
		shellFrame.TaskPane.AddCustomCommandToGroup( commandShow2, TaskPaneGroup_ViewAndModify, -110 );
		shellFrame.TaskPane.AddCustomCommandToGroup( commandShow3, TaskPaneGroup_ViewAndModify, -110 );
		
		// Hide the command.
		shellFrame.Commands.SetCommandState( commandShow1, CommandLocation_All, CommandState_Hidden );
		shellFrame.Commands.SetCommandState( commandShow2, CommandLocation_All, CommandState_Hidden );
		shellFrame.Commands.SetCommandState( commandShow3, CommandLocation_All, CommandState_Hidden );		
		
		// Register to listen to when new shell listings are created.
		shellFrame.Events.Register(
			Event_NewShellListing,
			getNewShellListingHandler( shellFrame, commandShow1 ) );
		shellFrame.Events.Register(
			Event_NewShellListing,
			getNewShellListingHandler( shellFrame, commandShow2 ) );
		shellFrame.Events.Register(
			Event_NewShellListing,
			getNewShellListingHandler( shellFrame, commandShow3 ) );

		// Is there already a listing?  If so then we need to hook into it as well.
		if (null != shellFrame.Listing)
		{
			getNewShellListingHandler( shellFrame, commandShow1 )( shellFrame.Listing );
			getNewShellListingHandler( shellFrame, commandShow2 )( shellFrame.Listing );
			getNewShellListingHandler( shellFrame, commandShow3 )( shellFrame.Listing );
		}
		
		// Set the command handler function.
		shellFrame.Commands.Events.Register( Event_CustomCommand, function( command ) {
		
			// Branch by command.
			if( command == commandShow2 ) {
				
				//Get vault object
				var Vault = shellFrame.ShellUI.Vault;
				
				//Get select items
				
				var selectedItems = shellFrame.Listing.CurrentSelection;
				
				var selectedpath = Vault.ObjectFileOperations.GetPathInDefaultView( selectedItems.ObjectVersions.Item(0).ObjVer.ObjID, -1, -1, -1 );
				
				var oShell = new ActiveXObject("Shell.Application");
				var commandtoRun = "C:\\Program Files (x86)\\SoftMaker\\FlexiPDF 2022\\flexipdf.exe"; 
				oShell.ShellExecute(commandtoRun,selectedpath,"","open","1");
			}
		} );
	};
}

function getNewShellListingHandler(shellFrame, commandId)
{
	/// <summary>Gets a function to handle the NewShellListing event for shell frame.</summary>
	/// <param name="shellFrame" type="MFiles.ShellFrame">The current shell frame object.</param>
	/// <returns type="MFiles.Events.OnNewShellListing">The event handler.</returns>

	// Return the handler function for NewShellListing event.
	return function(shellListing)
	{
		// Listen for selection change events on the listing.
		shellListing.Events.Register(
			Event_SelectionChanged,
			function(selectedItems)
			{
				// Sanity.
				if (false == shellListing.IsActive)
				{
					return false;
				}

				// Has the user got any object versions selected?
				if (selectedItems.ObjectVersions.Count == 0)
				{
					// Hide the menu item - there's nothing selected.
					shellFrame.Commands.SetCommandState( commandId, CommandLocation_All, CommandState_Hidden );
					return false;
				}

				// Show the menu item.
				shellFrame.Commands.SetCommandState( commandId, CommandLocation_All, CommandState_Active );

				// We succeeded; return true.
				return true;
			} );
	};
}

Parents
  • Hello,

    What is the error that you receive?

    At the first glance, without knowing the error maybe you need to replace this:

    selectedItems.ObjectVersions.Item(0).ObjVer.ObjID with this:

    selectedItems.ObjectVersions[0].ObjVer.ObjID

  • I got it to work:

    var Vault = shellFrame.ShellUI.Vault;
    // var selectedItems = shellFrame.Listing.CurrentSelection;
    var selectedItems = shellFrame.ActiveListing.CurrentSelection;
    var objectversion = selectedItems.ObjectVersions.Item( 1 );
    var selectedpath = Vault.ObjectFileOperations.GetPathInDefaultView( objectversion.ObjVer.ObjID, -1, -1, -1 );
    var oShell = new ActiveXObject("Shell.Application");
    var commandtoRun = "C:\\Program Files (x86)\\SoftMaker\\FlexiPDF 2022\\flexipdf.exe"; 
    oShell.ShellExecute(commandtoRun,selectedpath,"","open","1");

    The commented line was the culprit, had to change "shellFrameListing" to "shellFrame.ActiveListing".. Additionally I had to do another step

    var objectversion = selectedItems.ObjectVersions.Item( 1 );

    Now I can finish my script to open a selected pdf file with different PDF-Tools, as one PDF tool to rule them all has yet to be invented ;) When double clicking the pdf I use Sumatra for quick and clear viewing..

    Update:

    Button 1 - CheckOut & Show Program Selector made with AutoHotkey

    Button 2 - Call Sumatra PDF with Print Arguments (send to Printer X with Print Size A4)

    Button 3 - Call Sumatra PDF with Print Arguments (send to Printer Y with Print Size A3)

Reply
  • I got it to work:

    var Vault = shellFrame.ShellUI.Vault;
    // var selectedItems = shellFrame.Listing.CurrentSelection;
    var selectedItems = shellFrame.ActiveListing.CurrentSelection;
    var objectversion = selectedItems.ObjectVersions.Item( 1 );
    var selectedpath = Vault.ObjectFileOperations.GetPathInDefaultView( objectversion.ObjVer.ObjID, -1, -1, -1 );
    var oShell = new ActiveXObject("Shell.Application");
    var commandtoRun = "C:\\Program Files (x86)\\SoftMaker\\FlexiPDF 2022\\flexipdf.exe"; 
    oShell.ShellExecute(commandtoRun,selectedpath,"","open","1");

    The commented line was the culprit, had to change "shellFrameListing" to "shellFrame.ActiveListing".. Additionally I had to do another step

    var objectversion = selectedItems.ObjectVersions.Item( 1 );

    Now I can finish my script to open a selected pdf file with different PDF-Tools, as one PDF tool to rule them all has yet to be invented ;) When double clicking the pdf I use Sumatra for quick and clear viewing..

    Update:

    Button 1 - CheckOut & Show Program Selector made with AutoHotkey

    Button 2 - Call Sumatra PDF with Print Arguments (send to Printer X with Print Size A4)

    Button 3 - Call Sumatra PDF with Print Arguments (send to Printer Y with Print Size A3)

Children
No Data