Retrieve fully qualified .lnk file's target in script?

Hi Leo,
in this item :
How to open a shortcut(.lnk) from a command - #10 by Leo
you mentioned that

No filename code, but scripts can look up shortcut targets, fwiw

I'm trying to create a column-script that will
- return a shortcut's link if that is the file extension
- and the file's path if the current tab is a collection

I've attached the script, inline, below. I was trying to use the ScriptColumnData.columns object, perhaps erroneously, on line 68+1 of the script.

Could you pls provide a code snipped by which a script can return that information?

Many thx, Cheers, Brian


// jsFileHome
// 
// 
// This is a script for Directory Opus.
// See http://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information.
// 
// 
// 
    // The OnInit function is called by Directory Opus to initialize the script add-in
    function OnInit(initData) {


        // Provide basic information about the script by initializing the properties of the ScriptInitData object
        initData.name = "nmFileHome";
        initData.desc = "Adds a column that counts the length of the FullyQualified FileName.";
        initData.copyright = "(c) 2016 Brian Moloney";
        initData.default_enable = true;
	//	initData.multicol = true;

        // Create a new ScriptColumn object and initialize it to add the column to Opus
        var cmd = initData.AddColumn();
        cmd.name = "nmFileHome";
        cmd.method = "OnnmFileHome";
        cmd.label = "nmFileHome";
        cmd.autogroup = false;       // we provide our own grouping information

        cmd.autorefresh = true;      // refresh column when files change
        cmd.justify = "left";
	//cmd.multicol = true;

        cmd.match.push_back("Yes");  // when searching, these are the only two options

    }


    // Implement the IsModified column (this entry point is an OnScriptColumn event).

    // The name of this function must correspond to the value specified for the method property when the column was added in OnInit
    function OnnmFileHome(scriptColData) 
    {
        // scriptColData is a ScriptColumnData object. first check that this is the right column (it should be since we only added one)
		scriptColData.value = '<notSet_01>';
        if (scriptColData.col != "nmFileHome")
        {   return; }
	
	
		scriptColData.value = '<notSet_02>';
		var tabPath = new String(scriptColData.tab.path.longpath);
	//	DOpus.Output ('file dtl is ... ' + scriptColData.item.ext);
	//Opus.Output ('file startOfPath is ... ' + tabPath.substring(0,7));
		
		var soughtValue = new String('');
		
	 
		if (scriptColData.item.ext == '.lnk')
		{	DOpus.Output ('file is .lnk');
	

			DOpus.Output("count: " + scriptColData.item.columns.count);
/*
			for (var e = new Enumerator(scriptColData.columns); !scriptColData.columns.atEnd(); scriptColData.columns.moveNext())
			{
			 var key = e.item();
			 var value = map(key);
			 DOpus.Output(key + " -> " + value);
			}
			*/
		//	soughtValue = scriptColData.item.columns('Name').value; // 'sv'; //
			soughtValue = scriptColData.columns('target').value; // 'sv'; //
			scriptColData.value = soughtValue;
			DOpus.Output ('file target is ... ' + scriptColData.value);
		} else if (tabPath.substring(0,7) == 'coll://')  // is a collection
		{ //	DOpus.Output ('file is in a coll://');
		  //	DOpus.Output ('...' +  scriptColData.item.realpath.longpath);
			scriptColData.value = scriptColData.item.realpath.longpath;
		}
	
        scriptColData.group = scriptColData.value;
        scriptColData.sort = scriptColData.value;
    }
soughtValue = scriptColData.columns('target').value;

scriptColData.columns is the columns which your script has added and can fill out. You can't use it to look up the values of other columns. (It's essentially an output of your script, not an input.)

To get the target of a shortcut (.lnk file) in JScript, you can do this:

var sh = new ActiveXObject("WScript.Shell");
var sc = sh.CreateShortcut(scriptColData.item.realpath);
var targetPath = sc.TargetPath;

A fuller example can be found here:

It's also worth having a read of the Editing & Formatting Tips link above the post editor, to learn how to post code blocks and other useful things:

2022-08-10 14-07-42 Clipboard Image

1 Like

Isn't that the same as item.metadata.other.target?

Should be the same result. Not sure which is faster without testing it.